示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class A {} class B {} class C { /** * C constructor. * @param A $objA * @param B $objB */ public function __construct(A $objA, B $objB) { $this->a = $objA; $this->b = $objB; } } function getConcrete($name, $params){ $reflector = new ReflectionClass($name); $constructor = $reflector->getConstructor(); $dependencies = $constructor->getParameters(); var_dump($params); var_dump($dependencies); foreach ($params as $key => $value) { if (is_numeric($key)) { unset($params[$key]); $params[$dependencies[$key]->name] = $value; } } var_dump($params); $args = []; foreach ($dependencies as $parameter) { $dependency = $parameter->getClass(); $args[] = $params[$parameter->name]; } $c = $reflector->newInstanceArgs($args); var_dump($c); return $c; } getConcrete('C', [ 'objB' => new B, new A, ]); |
di容器的应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
public function build($concrete, array $parameters = []) $reflector = new ReflectionClass($concrete); $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); // If there are no constructors, that means there are no dependencies then // we can just resolve the instances of the objects right away, without // resolving any other types or dependencies out of these containers. if (is_null($constructor)) { array_pop($this->buildStack); return new $concrete; } $dependencies = $constructor->getParameters(); // Once we have all the constructor's parameters we can create each of the // dependency instances and then use the reflection instances to make a // new instance of this class, injecting the created dependencies in. $parameters = $this->keyParametersByArgument( $dependencies, $parameters ); $instances = $this->getDependencies( $dependencies, $parameters ); array_pop($this->buildStack); return $reflector->newInstanceArgs($instances); } protected function keyParametersByArgument(array $dependencies, array $parameters) { foreach ($parameters as $key => $value) { if (is_numeric($key)) { unset($parameters[$key]); $parameters[$dependencies[$key]->name] = $value; } } return $parameters; } protected function getDependencies(array $parameters, array $primitives = []) { $dependencies = []; foreach ($parameters as $parameter) { $dependency = $parameter->getClass(); // If the class is null, it means the dependency is a string or some other // primitive type which we can not resolve since it is not a class and // we will just bomb out with an error since we have no-where to go. if (array_key_exists($parameter->name, $primitives)) { $dependencies[] = $primitives[$parameter->name]; } elseif (is_null($dependency)) { $dependencies[] = $this->resolveNonClass($parameter); } else { $dependencies[] = $this->resolveClass($parameter); } } return $dependencies; } protected function resolveClass(ReflectionParameter $parameter) { try { return $this->make($parameter->getClass()->name); } // If we can not resolve the class instance, we will check to see if the value // is optional, and if it is we will return the optional parameter value as // the value of the dependency, similarly to how we do this with scalars. catch (BindingResolutionException $e) { if ($parameter->isOptional()) { return $parameter->getDefaultValue(); } throw $e; } } |