{"id":234,"date":"2016-05-03T17:17:56","date_gmt":"2016-05-03T09:17:56","guid":{"rendered":"http:\/\/blog.inforere.com\/?p=234"},"modified":"2016-05-05T11:16:42","modified_gmt":"2016-05-05T03:16:42","slug":"di%e5%ae%b9%e5%99%a8%e5%8f%82%e6%95%b0%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/blog.inforere.com\/?p=234","title":{"rendered":"\u53cd\u5c04\u5e94\u7528\u4e8e\u6784\u9020\u51fd\u6570\u53c2\u6570\u5904\u7406"},"content":{"rendered":"<p>\u793a\u4f8b\u4ee3\u7801<\/p>\n<pre class=\"lang:php decode:true \">\r\nclass A {}\r\nclass B {}\r\nclass C {\r\n    \/**\r\n     * C constructor.\r\n     * @param A $objA\r\n     * @param B $objB\r\n     *\/\r\n    public function __construct(A $objA, B $objB) {\r\n        $this-&gt;a = $objA;\r\n        $this-&gt;b = $objB;\r\n    }\r\n}\r\n\r\nfunction getConcrete($name, $params){\r\n    $reflector = new ReflectionClass($name);\r\n    $constructor = $reflector-&gt;getConstructor();\r\n    $dependencies = $constructor-&gt;getParameters();\r\n    var_dump($params);\r\n    var_dump($dependencies);\r\n\r\n    foreach ($params as $key =&gt; $value) {\r\n        if (is_numeric($key)) {\r\n            unset($params[$key]);\r\n            $params[$dependencies[$key]-&gt;name] = $value;\r\n        }\r\n    }\r\n    var_dump($params);\r\n\r\n    $args = [];\r\n    foreach ($dependencies as $parameter) {\r\n        $dependency = $parameter-&gt;getClass();\r\n        $args[] = $params[$parameter-&gt;name];\r\n    }\r\n\r\n    $c = $reflector-&gt;newInstanceArgs($args);\r\n    var_dump($c);\r\n\r\n    return $c;\r\n}\r\n\r\ngetConcrete('C', [\r\n    'objB' =&gt; new B,\r\n    new A,\r\n]);\r\n<\/pre>\n<p>di\u5bb9\u5668\u7684\u5e94\u7528<\/p>\n<pre class=\"lang:php decode:true \">    public function build($concrete, array $parameters = [])\r\n        $reflector = new ReflectionClass($concrete);\r\n        $this-&gt;buildStack[] = $concrete;\r\n\r\n        $constructor = $reflector-&gt;getConstructor();\r\n\r\n        \/\/ If there are no constructors, that means there are no dependencies then\r\n        \/\/ we can just resolve the instances of the objects right away, without\r\n        \/\/ resolving any other types or dependencies out of these containers.\r\n        if (is_null($constructor)) {\r\n            array_pop($this-&gt;buildStack);\r\n\r\n            return new $concrete;\r\n        }\r\n\r\n        $dependencies = $constructor-&gt;getParameters();\r\n\r\n        \/\/ Once we have all the constructor's parameters we can create each of the\r\n        \/\/ dependency instances and then use the reflection instances to make a\r\n        \/\/ new instance of this class, injecting the created dependencies in.\r\n        $parameters = $this-&gt;keyParametersByArgument(\r\n            $dependencies, $parameters\r\n        );\r\n\r\n        $instances = $this-&gt;getDependencies(\r\n            $dependencies, $parameters\r\n        );\r\n\r\n        array_pop($this-&gt;buildStack);\r\n\r\n        return $reflector-&gt;newInstanceArgs($instances);\r\n    }\r\n\r\n    protected function keyParametersByArgument(array $dependencies, array $parameters)\r\n    {\r\n        foreach ($parameters as $key =&gt; $value) {\r\n            if (is_numeric($key)) {\r\n                unset($parameters[$key]);\r\n\r\n                $parameters[$dependencies[$key]-&gt;name] = $value;\r\n            }\r\n        }\r\n\r\n        return $parameters;\r\n    }\r\n\r\n    protected function getDependencies(array $parameters, array $primitives = [])\r\n    {\r\n        $dependencies = [];\r\n\r\n        foreach ($parameters as $parameter) {\r\n            $dependency = $parameter-&gt;getClass();\r\n\r\n            \/\/ If the class is null, it means the dependency is a string or some other\r\n            \/\/ primitive type which we can not resolve since it is not a class and\r\n            \/\/ we will just bomb out with an error since we have no-where to go.\r\n            if (array_key_exists($parameter-&gt;name, $primitives)) {\r\n                $dependencies[] = $primitives[$parameter-&gt;name];\r\n            } elseif (is_null($dependency)) {\r\n                $dependencies[] = $this-&gt;resolveNonClass($parameter);\r\n            } else {\r\n                $dependencies[] = $this-&gt;resolveClass($parameter);\r\n            }\r\n        }\r\n\r\n        return $dependencies;\r\n    }\r\n\r\n    protected function resolveClass(ReflectionParameter $parameter)\r\n    {\r\n        try {\r\n            return $this-&gt;make($parameter-&gt;getClass()-&gt;name);\r\n        }\r\n\r\n        \/\/ If we can not resolve the class instance, we will check to see if the value\r\n        \/\/ is optional, and if it is we will return the optional parameter value as\r\n        \/\/ the value of the dependency, similarly to how we do this with scalars.\r\n        catch (BindingResolutionException $e) {\r\n            if ($parameter-&gt;isOptional()) {\r\n                return $parameter-&gt;getDefaultValue();\r\n            }\r\n\r\n            throw $e;\r\n        }\r\n    }\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u793a\u4f8b\u4ee3\u7801 class A {} class B {} class C { \/** * C constructo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/234"}],"collection":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=234"}],"version-history":[{"count":7,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}