1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Container extends Nette\Object
19: {
20: const TAGS = 'tags';
21: const TYPES = 'types';
22:
23:
24: public $parameters = array();
25:
26:
27: private $registry = array();
28:
29:
30: protected $meta = array();
31:
32:
33: private $creating;
34:
35:
36: public function __construct(array $params = array())
37: {
38: $this->parameters = $params + $this->parameters;
39: }
40:
41:
42: 43: 44:
45: public function getParameters()
46: {
47: return $this->parameters;
48: }
49:
50:
51: 52: 53: 54: 55: 56:
57: public function addService($name, $service)
58: {
59: if (func_num_args() > 2) {
60: throw new Nette\DeprecatedException('Parameter $meta has been removed.');
61:
62: } elseif (!is_string($name) || !$name) {
63: throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));
64:
65: } elseif (isset($this->registry[$name])) {
66: throw new Nette\InvalidStateException("Service '$name' already exists.");
67:
68: } elseif (is_string($service) || is_array($service) || $service instanceof \Closure || $service instanceof Nette\Callback) {
69: trigger_error(sprintf('Passing factories to %s() is deprecated; pass the object itself.', __METHOD__), E_USER_DEPRECATED);
70: $service = is_string($service) && !preg_match('#\x00|:#', $service) ? new $service : call_user_func($service, $this);
71: }
72:
73: if (!is_object($service)) {
74: throw new Nette\InvalidArgumentException(sprintf('Service must be a object, %s given.', gettype($service)));
75: }
76:
77: $this->registry[$name] = $service;
78: return $this;
79: }
80:
81:
82: 83: 84: 85: 86:
87: public function removeService($name)
88: {
89: unset($this->registry[$name]);
90: }
91:
92:
93: 94: 95: 96: 97: 98:
99: public function getService($name)
100: {
101: if (!isset($this->registry[$name])) {
102: $this->registry[$name] = $this->createService($name);
103: }
104: return $this->registry[$name];
105: }
106:
107:
108: 109: 110: 111: 112:
113: public function hasService($name)
114: {
115: return isset($this->registry[$name])
116: || method_exists($this, $method = Container::getMethodName($name)) && $this->getReflection()->getMethod($method)->getName() === $method;
117: }
118:
119:
120: 121: 122: 123: 124:
125: public function isCreated($name)
126: {
127: if (!$this->hasService($name)) {
128: throw new MissingServiceException("Service '$name' not found.");
129: }
130: return isset($this->registry[$name]);
131: }
132:
133:
134: 135: 136: 137: 138: 139:
140: public function createService($name, array $args = array())
141: {
142: $method = Container::getMethodName($name);
143: if (isset($this->creating[$name])) {
144: throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating))));
145:
146: } elseif (!method_exists($this, $method) || $this->getReflection()->getMethod($method)->getName() !== $method) {
147: throw new MissingServiceException("Service '$name' not found.");
148: }
149:
150: $this->creating[$name] = TRUE;
151: try {
152: $service = call_user_func_array(array($this, $method), $args);
153: } catch (\Exception $e) {
154: unset($this->creating[$name]);
155: throw $e;
156: }
157: unset($this->creating[$name]);
158:
159: if (!is_object($service)) {
160: throw new Nette\UnexpectedValueException("Unable to create service '$name', value returned by method $method() is not object.");
161: }
162:
163: return $service;
164: }
165:
166:
167: 168: 169: 170: 171: 172: 173:
174: public function getByType($class, $need = TRUE)
175: {
176: $names = $this->findByType($class);
177: if (!$names) {
178: if ($need) {
179: throw new MissingServiceException("Service of type $class not found.");
180: }
181: } elseif (count($names) > 1) {
182: throw new MissingServiceException("Multiple services of type $class found: " . implode(', ', $names) . '.');
183: } else {
184: return $this->getService($names[0]);
185: }
186: }
187:
188:
189: 190: 191: 192: 193:
194: public function findByType($class)
195: {
196: $class = ltrim(strtolower($class), '\\');
197: return isset($this->meta[self::TYPES][$class]) ? $this->meta[self::TYPES][$class] : array();
198: }
199:
200:
201: 202: 203: 204: 205:
206: public function findByTag($tag)
207: {
208: return isset($this->meta[self::TAGS][$tag]) ? $this->meta[self::TAGS][$tag] : array();
209: }
210:
211:
212:
213:
214:
215: 216: 217: 218: 219: 220: 221:
222: public function createInstance($class, array $args = array())
223: {
224: $rc = Nette\Reflection\ClassType::from($class);
225: if (!$rc->isInstantiable()) {
226: throw new ServiceCreationException("Class $class is not instantiable.");
227:
228: } elseif ($constructor = $rc->getConstructor()) {
229: return $rc->newInstanceArgs(Helpers::autowireArguments($constructor, $args, $this));
230:
231: } elseif ($args) {
232: throw new ServiceCreationException("Unable to pass arguments, class $class has no constructor.");
233: }
234: return new $class;
235: }
236:
237:
238: 239: 240: 241: 242:
243: public function callInjects($service)
244: {
245: if (!is_object($service)) {
246: throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service)));
247: }
248:
249: foreach (array_reverse(get_class_methods($service)) as $method) {
250: if (substr($method, 0, 6) === 'inject') {
251: $this->callMethod(array($service, $method));
252: }
253: }
254:
255: foreach (Helpers::getInjectProperties(Nette\Reflection\ClassType::from($service), $this) as $property => $type) {
256: $service->$property = $this->getByType($type);
257: }
258: }
259:
260:
261: 262: 263: 264: 265: 266:
267: public function callMethod($function, array $args = array())
268: {
269: return call_user_func_array(
270: $function,
271: Helpers::autowireArguments(Nette\Utils\Callback::toReflection($function), $args, $this)
272: );
273: }
274:
275:
276:
277:
278:
279: 280: 281: 282: 283:
284: public function expand($s)
285: {
286: return Helpers::expand($s, $this->parameters);
287: }
288:
289:
290:
291: public function &__get($name)
292: {
293: $this->error(__METHOD__, 'getService');
294: $tmp = $this->getService($name);
295: return $tmp;
296: }
297:
298:
299:
300: public function __set($name, $service)
301: {
302: $this->error(__METHOD__, 'addService');
303: $this->addService($name, $service);
304: }
305:
306:
307:
308: public function __isset($name)
309: {
310: $this->error(__METHOD__, 'hasService');
311: return $this->hasService($name);
312: }
313:
314:
315:
316: public function __unset($name)
317: {
318: $this->error(__METHOD__, 'removeService');
319: $this->removeService($name);
320: }
321:
322:
323: private function error($oldName, $newName)
324: {
325: if (empty($this->parameters['container']['accessors'])) {
326: trigger_error("$oldName() is deprecated; use $newName() or enable nette.container.accessors in configuration.", E_USER_DEPRECATED);
327: }
328: }
329:
330:
331: public static function getMethodName($name)
332: {
333: $uname = ucfirst($name);
334: return 'createService' . ((string) $name === $uname ? '__' : '') . str_replace('.', '__', $uname);
335: }
336:
337: }
338: