@@ -43,144 +43,144 @@ |
||
43 | 43 | class SimpleContainer extends Container implements IContainer { |
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * @param ReflectionClass $class the class to instantiate |
|
48 | - * @return \stdClass the created class |
|
49 | - * @suppress PhanUndeclaredClassInstanceof |
|
50 | - */ |
|
51 | - private function buildClass(ReflectionClass $class) { |
|
52 | - $constructor = $class->getConstructor(); |
|
53 | - if ($constructor === null) { |
|
54 | - return $class->newInstance(); |
|
55 | - } else { |
|
56 | - $parameters = []; |
|
57 | - foreach ($constructor->getParameters() as $parameter) { |
|
58 | - $parameterClass = $parameter->getClass(); |
|
59 | - |
|
60 | - // try to find out if it is a class or a simple parameter |
|
61 | - if ($parameterClass === null) { |
|
62 | - $resolveName = $parameter->getName(); |
|
63 | - } else { |
|
64 | - $resolveName = $parameterClass->name; |
|
65 | - } |
|
66 | - |
|
67 | - try { |
|
68 | - $parameters[] = $this->query($resolveName); |
|
69 | - } catch (\Exception $e) { |
|
70 | - if (class_exists('PHPUnit_Framework_AssertionFailedError', false) && |
|
71 | - $e instanceof \PHPUnit_Framework_AssertionFailedError) { |
|
72 | - // Easier debugging of "Your test case is not allowed to access the database." |
|
73 | - throw $e; |
|
74 | - } |
|
75 | - |
|
76 | - // Service not found, use the default value when available |
|
77 | - if ($parameter->isDefaultValueAvailable()) { |
|
78 | - $parameters[] = $parameter->getDefaultValue(); |
|
79 | - } else if ($parameterClass !== null) { |
|
80 | - $resolveName = $parameter->getName(); |
|
81 | - $parameters[] = $this->query($resolveName); |
|
82 | - } else { |
|
83 | - throw $e; |
|
84 | - } |
|
85 | - } |
|
86 | - } |
|
87 | - return $class->newInstanceArgs($parameters); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * If a parameter is not registered in the container try to instantiate it |
|
94 | - * by using reflection to find out how to build the class |
|
95 | - * @param string $name the class name to resolve |
|
96 | - * @return \stdClass |
|
97 | - * @throws QueryException if the class could not be found or instantiated |
|
98 | - */ |
|
99 | - public function resolve($name) { |
|
100 | - $baseMsg = 'Could not resolve ' . $name . '!'; |
|
101 | - try { |
|
102 | - $class = new ReflectionClass($name); |
|
103 | - if ($class->isInstantiable()) { |
|
104 | - return $this->buildClass($class); |
|
105 | - } else { |
|
106 | - throw new QueryException($baseMsg . |
|
107 | - ' Class can not be instantiated'); |
|
108 | - } |
|
109 | - } catch(ReflectionException $e) { |
|
110 | - throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * @param string $name name of the service to query for |
|
117 | - * @return mixed registered service for the given $name |
|
118 | - * @throws QueryException if the query could not be resolved |
|
119 | - */ |
|
120 | - public function query($name) { |
|
121 | - $name = $this->sanitizeName($name); |
|
122 | - if ($this->offsetExists($name)) { |
|
123 | - return $this->offsetGet($name); |
|
124 | - } else { |
|
125 | - $object = $this->resolve($name); |
|
126 | - $this->registerService($name, function () use ($object) { |
|
127 | - return $object; |
|
128 | - }); |
|
129 | - return $object; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param string $name |
|
135 | - * @param mixed $value |
|
136 | - */ |
|
137 | - public function registerParameter($name, $value) { |
|
138 | - $this[$name] = $value; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * The given closure is call the first time the given service is queried. |
|
143 | - * The closure has to return the instance for the given service. |
|
144 | - * Created instance will be cached in case $shared is true. |
|
145 | - * |
|
146 | - * @param string $name name of the service to register another backend for |
|
147 | - * @param Closure $closure the closure to be called on service creation |
|
148 | - * @param bool $shared |
|
149 | - */ |
|
150 | - public function registerService($name, Closure $closure, $shared = true) { |
|
151 | - $name = $this->sanitizeName($name); |
|
152 | - if (isset($this[$name])) { |
|
153 | - unset($this[$name]); |
|
154 | - } |
|
155 | - if ($shared) { |
|
156 | - $this[$name] = $closure; |
|
157 | - } else { |
|
158 | - $this[$name] = parent::factory($closure); |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Shortcut for returning a service from a service under a different key, |
|
164 | - * e.g. to tell the container to return a class when queried for an |
|
165 | - * interface |
|
166 | - * @param string $alias the alias that should be registered |
|
167 | - * @param string $target the target that should be resolved instead |
|
168 | - */ |
|
169 | - public function registerAlias($alias, $target) { |
|
170 | - $this->registerService($alias, function (IContainer $container) use ($target) { |
|
171 | - return $container->query($target); |
|
172 | - }, false); |
|
173 | - } |
|
174 | - |
|
175 | - /* |
|
46 | + /** |
|
47 | + * @param ReflectionClass $class the class to instantiate |
|
48 | + * @return \stdClass the created class |
|
49 | + * @suppress PhanUndeclaredClassInstanceof |
|
50 | + */ |
|
51 | + private function buildClass(ReflectionClass $class) { |
|
52 | + $constructor = $class->getConstructor(); |
|
53 | + if ($constructor === null) { |
|
54 | + return $class->newInstance(); |
|
55 | + } else { |
|
56 | + $parameters = []; |
|
57 | + foreach ($constructor->getParameters() as $parameter) { |
|
58 | + $parameterClass = $parameter->getClass(); |
|
59 | + |
|
60 | + // try to find out if it is a class or a simple parameter |
|
61 | + if ($parameterClass === null) { |
|
62 | + $resolveName = $parameter->getName(); |
|
63 | + } else { |
|
64 | + $resolveName = $parameterClass->name; |
|
65 | + } |
|
66 | + |
|
67 | + try { |
|
68 | + $parameters[] = $this->query($resolveName); |
|
69 | + } catch (\Exception $e) { |
|
70 | + if (class_exists('PHPUnit_Framework_AssertionFailedError', false) && |
|
71 | + $e instanceof \PHPUnit_Framework_AssertionFailedError) { |
|
72 | + // Easier debugging of "Your test case is not allowed to access the database." |
|
73 | + throw $e; |
|
74 | + } |
|
75 | + |
|
76 | + // Service not found, use the default value when available |
|
77 | + if ($parameter->isDefaultValueAvailable()) { |
|
78 | + $parameters[] = $parameter->getDefaultValue(); |
|
79 | + } else if ($parameterClass !== null) { |
|
80 | + $resolveName = $parameter->getName(); |
|
81 | + $parameters[] = $this->query($resolveName); |
|
82 | + } else { |
|
83 | + throw $e; |
|
84 | + } |
|
85 | + } |
|
86 | + } |
|
87 | + return $class->newInstanceArgs($parameters); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * If a parameter is not registered in the container try to instantiate it |
|
94 | + * by using reflection to find out how to build the class |
|
95 | + * @param string $name the class name to resolve |
|
96 | + * @return \stdClass |
|
97 | + * @throws QueryException if the class could not be found or instantiated |
|
98 | + */ |
|
99 | + public function resolve($name) { |
|
100 | + $baseMsg = 'Could not resolve ' . $name . '!'; |
|
101 | + try { |
|
102 | + $class = new ReflectionClass($name); |
|
103 | + if ($class->isInstantiable()) { |
|
104 | + return $this->buildClass($class); |
|
105 | + } else { |
|
106 | + throw new QueryException($baseMsg . |
|
107 | + ' Class can not be instantiated'); |
|
108 | + } |
|
109 | + } catch(ReflectionException $e) { |
|
110 | + throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * @param string $name name of the service to query for |
|
117 | + * @return mixed registered service for the given $name |
|
118 | + * @throws QueryException if the query could not be resolved |
|
119 | + */ |
|
120 | + public function query($name) { |
|
121 | + $name = $this->sanitizeName($name); |
|
122 | + if ($this->offsetExists($name)) { |
|
123 | + return $this->offsetGet($name); |
|
124 | + } else { |
|
125 | + $object = $this->resolve($name); |
|
126 | + $this->registerService($name, function () use ($object) { |
|
127 | + return $object; |
|
128 | + }); |
|
129 | + return $object; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param string $name |
|
135 | + * @param mixed $value |
|
136 | + */ |
|
137 | + public function registerParameter($name, $value) { |
|
138 | + $this[$name] = $value; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * The given closure is call the first time the given service is queried. |
|
143 | + * The closure has to return the instance for the given service. |
|
144 | + * Created instance will be cached in case $shared is true. |
|
145 | + * |
|
146 | + * @param string $name name of the service to register another backend for |
|
147 | + * @param Closure $closure the closure to be called on service creation |
|
148 | + * @param bool $shared |
|
149 | + */ |
|
150 | + public function registerService($name, Closure $closure, $shared = true) { |
|
151 | + $name = $this->sanitizeName($name); |
|
152 | + if (isset($this[$name])) { |
|
153 | + unset($this[$name]); |
|
154 | + } |
|
155 | + if ($shared) { |
|
156 | + $this[$name] = $closure; |
|
157 | + } else { |
|
158 | + $this[$name] = parent::factory($closure); |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Shortcut for returning a service from a service under a different key, |
|
164 | + * e.g. to tell the container to return a class when queried for an |
|
165 | + * interface |
|
166 | + * @param string $alias the alias that should be registered |
|
167 | + * @param string $target the target that should be resolved instead |
|
168 | + */ |
|
169 | + public function registerAlias($alias, $target) { |
|
170 | + $this->registerService($alias, function (IContainer $container) use ($target) { |
|
171 | + return $container->query($target); |
|
172 | + }, false); |
|
173 | + } |
|
174 | + |
|
175 | + /* |
|
176 | 176 | * @param string $name |
177 | 177 | * @return string |
178 | 178 | */ |
179 | - protected function sanitizeName($name) { |
|
180 | - if (isset($name[0]) && $name[0] === '\\') { |
|
181 | - return ltrim($name, '\\'); |
|
182 | - } |
|
183 | - return $name; |
|
184 | - } |
|
179 | + protected function sanitizeName($name) { |
|
180 | + if (isset($name[0]) && $name[0] === '\\') { |
|
181 | + return ltrim($name, '\\'); |
|
182 | + } |
|
183 | + return $name; |
|
184 | + } |
|
185 | 185 | |
186 | 186 | } |