ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Injector.php
Go to the documentation of this file.
1<?php
2
4
8use ReflectionParameter;
9
20{
21
25 protected $dic;
29 protected $dependencyMap;
30
31
39 {
40 $this->dic = $dic;
41 $this->dependencyMap = $dependencyMap;
42 }
43
44
54 public function createInstance($fullyQualifiedClassName, $requireFile = false, callable $with = null)
55 {
56 if ($requireFile) {
57 require_once($requireFile);
58 }
59
60 // The reflection classes needed.
61 $reflectionClass = new \ReflectionClass($fullyQualifiedClassName);
62 $constructor = $reflectionClass->getConstructor();
63 if (!$constructor) {
64 return $reflectionClass->newInstance();
65 }
66
67 $parameters = $constructor->getParameters();
68
69 // we get the arguments to construct the object from the DIC and Typehinting.
70 $constructorArguments = $this->createConstructorArguments($fullyQualifiedClassName, $parameters, $with);
71
72 // Crate the instance with the arguments.
73 return $reflectionClass->newInstanceArgs($constructorArguments);
74 }
75
76
84 protected function createConstructorArguments($fullyQualifiedClassName, $parameters, $with)
85 {
86 $constructorArguments = [];
87
88 foreach ($parameters as $parameter) {
89 // As long as there are given arguments we take those.
90 $constructorArguments[] = $this->getDependency($fullyQualifiedClassName, $parameter, $with);
91 }
92
93 return $constructorArguments;
94 }
95
96
106 protected function getDependency($fullyQualifiedClassName, $parameter, callable $with = null)
107 {
108 // These Lines are currently commented while we cant use $parameter->getType() which will be part of PHP7
109 // if (!$parameter->getType()) {
110 // throw new InvalidClassException("The constructor of $fullyQualifiedClassName is not fully type hinted, or the type hints cannot be resolved.");
111 // }
112
113 // $type = $parameter->getType()->__toString();
114 $type = $parameter->getClass()->getName();
115
116 // if ($parameter->getType()->isBuiltin()) {
117 // throw new InvalidClassException("The DI cannot instantiate $fullyQualifiedClassName because some of the constructors arguments are built in types. Only interfaces (and objects) are stored in the DI-Container.");
118 // }
119
120 if (!$type) {
121 throw new InvalidClassException("The DI cannot instantiate $fullyQualifiedClassName because some of the constructors arguments are not type hinted. Make sure all parameters in the constructor have type hinting.");
122 }
123
124 if ($with) {
125 return $this->dependencyMap->getDependencyWith($this->dic, $type, $fullyQualifiedClassName, $with);
126 } else {
127 return $this->dependencyMap->getDependency($this->dic, $type, $fullyQualifiedClassName);
128 }
129 }
130}
An exception for terminatinating execution or to throw for unit testing.
createInstance($fullyQualifiedClassName, $requireFile=false, callable $with=null)
Definition: Injector.php:54
createConstructorArguments($fullyQualifiedClassName, $parameters, $with)
Definition: Injector.php:84
__construct(Container $dic, DependencyMap $dependencyMap)
Factory constructor.
Definition: Injector.php:38
getDependency($fullyQualifiedClassName, $parameter, callable $with=null)
Definition: Injector.php:106
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:17
$type