1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI;
9:
10: use Nette;
11: use Nette\PhpGenerator\Helpers as PhpHelpers;
12: use Nette\Utils\Reflection;
13: use Nette\Utils\Strings;
14: use Nette\Utils\Validators;
15: use ReflectionClass;
16:
17:
18: 19: 20:
21: class ContainerBuilder
22: {
23: use Nette\SmartObject;
24:
25: const THIS_SERVICE = 'self',
26: THIS_CONTAINER = 'container';
27:
28:
29: public $parameters = [];
30:
31:
32: private $definitions = [];
33:
34:
35: private $aliases = [];
36:
37:
38: private $classList = [];
39:
40:
41: private $classListNeedsRefresh = true;
42:
43:
44: private $excludedClasses = [];
45:
46:
47: private $dependencies = [];
48:
49:
50: private $currentService;
51:
52:
53: 54: 55: 56: 57:
58: public function addDefinition($name, ServiceDefinition $definition = null)
59: {
60: $this->classListNeedsRefresh = true;
61: if (!is_string($name) || !$name) {
62: throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));
63: }
64: $name = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
65: if (isset($this->definitions[$name])) {
66: throw new Nette\InvalidStateException("Service '$name' has already been added.");
67: }
68: if (!$definition) {
69: $definition = new ServiceDefinition;
70: }
71: $definition->setNotifier(function () {
72: $this->classListNeedsRefresh = true;
73: });
74: return $this->definitions[$name] = $definition;
75: }
76:
77:
78: 79: 80: 81: 82:
83: public function removeDefinition($name)
84: {
85: $this->classListNeedsRefresh = true;
86: $name = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
87: unset($this->definitions[$name]);
88: }
89:
90:
91: 92: 93: 94: 95:
96: public function getDefinition($name)
97: {
98: $service = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
99: if (!isset($this->definitions[$service])) {
100: throw new MissingServiceException("Service '$name' not found.");
101: }
102: return $this->definitions[$service];
103: }
104:
105:
106: 107: 108: 109:
110: public function getDefinitions()
111: {
112: return $this->definitions;
113: }
114:
115:
116: 117: 118: 119: 120:
121: public function hasDefinition($name)
122: {
123: $name = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
124: return isset($this->definitions[$name]);
125: }
126:
127:
128: 129: 130: 131:
132: public function addAlias($alias, $service)
133: {
134: if (!is_string($alias) || !$alias) {
135: throw new Nette\InvalidArgumentException(sprintf('Alias name must be a non-empty string, %s given.', gettype($alias)));
136:
137: } elseif (!is_string($service) || !$service) {
138: throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($service)));
139:
140: } elseif (isset($this->aliases[$alias])) {
141: throw new Nette\InvalidStateException("Alias '$alias' has already been added.");
142:
143: } elseif (isset($this->definitions[$alias])) {
144: throw new Nette\InvalidStateException("Service '$alias' has already been added.");
145: }
146: $this->aliases[$alias] = $service;
147: }
148:
149:
150: 151: 152: 153:
154: public function removeAlias($alias)
155: {
156: unset($this->aliases[$alias]);
157: }
158:
159:
160: 161: 162: 163:
164: public function getAliases()
165: {
166: return $this->aliases;
167: }
168:
169:
170: 171: 172: 173:
174: public function addExcludedClasses(array $types)
175: {
176: foreach ($types as $type) {
177: if (class_exists($type) || interface_exists($type)) {
178: $type = Helpers::normalizeClass($type);
179: $this->excludedClasses += class_parents($type) + class_implements($type) + [$type => $type];
180: }
181: }
182: return $this;
183: }
184:
185:
186: 187: 188:
189: public function setClassName($name)
190: {
191: trigger_error(__METHOD__ . ' has been deprecated', E_USER_DEPRECATED);
192: return $this;
193: }
194:
195:
196: 197: 198:
199: public function getClassName()
200: {
201: trigger_error(__METHOD__ . ' has been deprecated', E_USER_DEPRECATED);
202: }
203:
204:
205:
206:
207:
208: 209: 210: 211: 212: 213: 214:
215: public function getByType($type, $throw = false)
216: {
217: $type = Helpers::normalizeClass($type);
218:
219: if (
220: $this->currentService !== null
221: && is_a($this->definitions[$this->currentService]->getType(), $type, true)
222: ) {
223: return $this->currentService;
224: }
225:
226: $types = $this->getClassList();
227: if (empty($types[$type][true])) {
228: if ($throw) {
229: throw new MissingServiceException("Service of type '$type' not found.");
230: }
231: return;
232:
233: } elseif (count($types[$type][true]) === 1) {
234: return $types[$type][true][0];
235:
236: } else {
237: $list = $types[$type][true];
238: natsort($list);
239: $hint = count($list) === 2 && ($tmp = strpos($list[0], '.') xor strpos($list[1], '.'))
240: ? '. If you want to overwrite service ' . $list[$tmp ? 0 : 1] . ', give it proper name.'
241: : '';
242: throw new ServiceCreationException("Multiple services of type $type found: " . implode(', ', $list) . $hint);
243: }
244: }
245:
246:
247: 248: 249: 250: 251:
252: public function getDefinitionByType($type)
253: {
254: return $this->getDefinition($this->getByType($type, true));
255: }
256:
257:
258: 259: 260: 261: 262:
263: public function findByType($type)
264: {
265: $type = Helpers::normalizeClass($type);
266: $found = [];
267: $types = $this->getClassList();
268: if (!empty($types[$type])) {
269: foreach (array_merge(...array_values($types[$type])) as $name) {
270: $found[$name] = $this->definitions[$name];
271: }
272: }
273: return $found;
274: }
275:
276:
277: 278: 279: 280: 281:
282: public function findByTag($tag)
283: {
284: $found = [];
285: foreach ($this->definitions as $name => $def) {
286: if (($tmp = $def->getTag($tag)) !== null) {
287: $found[$name] = $tmp;
288: }
289: }
290: return $found;
291: }
292:
293:
294: 295: 296:
297: public function getClassList()
298: {
299: if ($this->classList !== false && $this->classListNeedsRefresh) {
300: $this->prepareClassList();
301: $this->classListNeedsRefresh = false;
302: }
303: return $this->classList ?: [];
304: }
305:
306:
307: 308: 309: 310: 311:
312: public function prepareClassList()
313: {
314: unset($this->definitions[self::THIS_CONTAINER]);
315: $this->addDefinition(self::THIS_CONTAINER)->setType(Container::class);
316:
317: $this->classList = false;
318:
319: foreach ($this->definitions as $name => $def) {
320:
321: if ($def->getImplement()) {
322: $this->resolveImplement($def, $name);
323: }
324:
325: if ($def->isDynamic()) {
326: if (!$def->getType()) {
327: throw new ServiceCreationException("Type is missing in definition of service '$name'.");
328: }
329: $def->setFactory(null);
330: continue;
331: }
332:
333:
334: if (!$def->getEntity()) {
335: if (!$def->getType()) {
336: throw new ServiceCreationException("Factory and type are missing in definition of service '$name'.");
337: }
338: $def->setFactory($def->getType(), ($factory = $def->getFactory()) ? $factory->arguments : []);
339: }
340:
341:
342: if (
343: $def->getAutowired() === true
344: && ($alias = $this->getServiceName($def->getFactory()->getEntity()))
345: && (!$def->getImplement() || (!Strings::contains($alias, '\\') && $this->definitions[$alias]->getImplement()))
346: ) {
347: $def->setAutowired(false);
348: }
349: }
350:
351:
352: foreach ($this->definitions as $name => $def) {
353: $this->resolveServiceType($name);
354: }
355:
356:
357: $this->classList = $preferred = [];
358: foreach ($this->definitions as $name => $def) {
359: if ($type = $def->getImplement() ?: $def->getType()) {
360: $defAutowired = $def->getAutowired();
361: if (is_array($defAutowired)) {
362: foreach ($defAutowired as $k => $autowiredType) {
363: if ($autowiredType === self::THIS_SERVICE) {
364: $defAutowired[$k] = $type;
365: } elseif (!is_a($type, $autowiredType, true)) {
366: throw new ServiceCreationException("Incompatible class $autowiredType in autowiring definition of service '$name'.");
367: }
368: }
369: }
370:
371: foreach (class_parents($type) + class_implements($type) + [$type] as $parent) {
372: $autowired = $defAutowired && empty($this->excludedClasses[$parent]);
373: if ($autowired && is_array($defAutowired)) {
374: $autowired = false;
375: foreach ($defAutowired as $autowiredType) {
376: if (is_a($parent, $autowiredType, true)) {
377: if (empty($preferred[$parent]) && isset($this->classList[$parent][true])) {
378: $this->classList[$parent][false] = array_merge(...$this->classList[$parent]);
379: $this->classList[$parent][true] = [];
380: }
381: $preferred[$parent] = $autowired = true;
382: break;
383: }
384: }
385: } elseif (isset($preferred[$parent])) {
386: $autowired = false;
387: }
388: $this->classList[$parent][$autowired][] = (string) $name;
389: }
390: }
391: }
392: }
393:
394:
395: private function resolveImplement(ServiceDefinition $def, $name)
396: {
397: $interface = $def->getImplement();
398: if (!interface_exists($interface)) {
399: throw new ServiceCreationException("Interface $interface used in service '$name' not found.");
400: }
401: $interface = Helpers::normalizeClass($interface);
402: $def->setImplement($interface);
403:
404: $rc = new ReflectionClass($interface);
405: $this->addDependency($rc);
406: $method = $rc->hasMethod('create')
407: ? $rc->getMethod('create')
408: : ($rc->hasMethod('get') ? $rc->getMethod('get') : null);
409:
410: if (count($rc->getMethods()) !== 1 || !$method || $method->isStatic()) {
411: throw new ServiceCreationException("Interface $interface used in service '$name' must have just one non-static method create() or get().");
412: }
413: $def->setImplementMode($rc->hasMethod('create') ? $def::IMPLEMENT_MODE_CREATE : $def::IMPLEMENT_MODE_GET);
414: $methodName = Reflection::toString($method) . '()';
415:
416: if (!$def->getType() && !$def->getEntity()) {
417: $returnType = Helpers::getReturnType($method);
418: if (!$returnType) {
419: throw new ServiceCreationException("Method $methodName used in service '$name' has not return type hint or annotation @return.");
420: } elseif (!class_exists($returnType)) {
421: throw new ServiceCreationException("Check a type hint or annotation @return of the $methodName method used in service '$name', class '$returnType' cannot be found.");
422: }
423: $def->setType($returnType);
424: }
425:
426: if ($rc->hasMethod('get')) {
427: if ($method->getParameters()) {
428: throw new ServiceCreationException("Method $methodName used in service '$name' must have no arguments.");
429: } elseif ($def->getSetup()) {
430: throw new ServiceCreationException("Service accessor '$name' must have no setup.");
431: }
432: if (!$def->getEntity()) {
433: $def->setFactory('@\\' . ltrim($def->getType(), '\\'));
434: } elseif (!$this->getServiceName($def->getFactory()->getEntity())) {
435: throw new ServiceCreationException("Invalid factory in service '$name' definition.");
436: }
437: }
438:
439: if (!$def->parameters) {
440: $ctorParams = [];
441: if (!$def->getEntity()) {
442: $def->setFactory($def->getType(), $def->getFactory() ? $def->getFactory()->arguments : []);
443: }
444: if (
445: ($class = $this->resolveEntityType($def->getFactory(), [$name => 1]))
446: && ($ctor = (new ReflectionClass($class))->getConstructor())
447: ) {
448: foreach ($ctor->getParameters() as $param) {
449: $ctorParams[$param->getName()] = $param;
450: }
451: }
452:
453: foreach ($method->getParameters() as $param) {
454: $hint = Reflection::getParameterType($param);
455: if (isset($ctorParams[$param->getName()])) {
456: $arg = $ctorParams[$param->getName()];
457: if ($hint !== Reflection::getParameterType($arg)) {
458: throw new ServiceCreationException("Type hint for \${$param->getName()} in $methodName doesn't match type hint in $class constructor.");
459: }
460: $def->getFactory()->arguments[$arg->getPosition()] = self::literal('$' . $arg->getName());
461: } elseif (!$def->getSetup()) {
462: $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($ctorParams), $param->getName());
463: throw new ServiceCreationException("Unused parameter \${$param->getName()} when implementing method $methodName" . ($hint ? ", did you mean \${$hint}?" : '.'));
464: }
465: $nullable = $hint && $param->allowsNull() && (!$param->isDefaultValueAvailable() || $param->getDefaultValue() !== null);
466: $paramDef = ($nullable ? '?' : '') . $hint . ' ' . $param->getName();
467: if ($param->isDefaultValueAvailable()) {
468: $def->parameters[$paramDef] = Reflection::getParameterDefaultValue($param);
469: } else {
470: $def->parameters[] = $paramDef;
471: }
472: }
473: }
474: }
475:
476:
477:
478: private function resolveServiceType($name, $recursive = [])
479: {
480: if (isset($recursive[$name])) {
481: throw new ServiceCreationException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($recursive))));
482: }
483: $recursive[$name] = true;
484:
485: $def = $this->definitions[$name];
486: $factoryClass = $def->getFactory() ? $this->resolveEntityType($def->getFactory()->getEntity(), $recursive) : null;
487: if ($type = $def->getType() ?: $factoryClass) {
488: if (!class_exists($type) && !interface_exists($type)) {
489: throw new ServiceCreationException("Class or interface '$type' used in service '$name' not found.");
490: }
491: $type = Helpers::normalizeClass($type);
492: $def->setType($type);
493: if (count($recursive) === 1) {
494: $this->addDependency(new ReflectionClass($factoryClass ?: $type));
495: }
496:
497: } elseif ($def->getAutowired()) {
498: throw new ServiceCreationException("Unknown type of service '$name', declare return type of factory method (for PHP 5 use annotation @return)");
499: }
500: return $type;
501: }
502:
503:
504:
505: private function resolveEntityType($entity, $recursive = [])
506: {
507: $entity = $this->normalizeEntity($entity instanceof Statement ? $entity->getEntity() : $entity);
508: $serviceName = current(array_slice(array_keys($recursive), -1));
509:
510: if (is_array($entity)) {
511: if (($service = $this->getServiceName($entity[0])) || $entity[0] instanceof Statement) {
512: $entity[0] = $this->resolveEntityType($entity[0], $recursive);
513: if (!$entity[0]) {
514: return;
515: } elseif (isset($this->definitions[$service]) && $this->definitions[$service]->getImplement()) {
516: return $entity[1] === 'create' ? $this->resolveServiceType($service, $recursive) : null;
517: }
518: }
519:
520: try {
521: $reflection = Nette\Utils\Callback::toReflection($entity[0] === '' ? $entity[1] : $entity);
522: $refClass = $reflection instanceof \ReflectionMethod ? $reflection->getDeclaringClass() : null;
523: } catch (\ReflectionException $e) {
524: }
525:
526: if (isset($e) || ($refClass && (!$reflection->isPublic()
527: || ($refClass->isTrait() && !$reflection->isStatic())
528: ))) {
529: throw new ServiceCreationException(sprintf("Method %s() used in service '%s' is not callable.", Nette\Utils\Callback::toString($entity), $serviceName));
530: }
531: $this->addDependency($reflection);
532:
533: $type = Helpers::getReturnType($reflection);
534: if ($type && !class_exists($type) && !interface_exists($type)) {
535: throw new ServiceCreationException(sprintf("Class or interface '%s' not found. Is return type of %s() used in service '%s' correct?", $type, Nette\Utils\Callback::toString($entity), $serviceName));
536: }
537: return $type;
538:
539: } elseif ($service = $this->getServiceName($entity)) {
540: if (Strings::contains($service, '\\')) {
541: return $service;
542: }
543: return $this->definitions[$service]->getImplement()
544: ?: $this->definitions[$service]->getType()
545: ?: $this->resolveServiceType($service, $recursive);
546:
547: } elseif (is_string($entity)) {
548: if (!class_exists($entity)) {
549: throw new ServiceCreationException("Class $entity used in service '$serviceName' not found.");
550: }
551: return $entity;
552: }
553: }
554:
555:
556: 557: 558:
559: public function complete()
560: {
561: $this->prepareClassList();
562:
563: foreach ($this->definitions as $name => $def) {
564: if ($def->isDynamic()) {
565: continue;
566: }
567:
568: $this->currentService = null;
569: $entity = $def->getFactory()->getEntity();
570: $serviceRef = $this->getServiceName($entity);
571: $factory = $serviceRef && !$def->getFactory()->arguments && !$def->getSetup() && $def->getImplementMode() !== $def::IMPLEMENT_MODE_CREATE
572: ? new Statement(['@' . self::THIS_CONTAINER, 'getService'], [$serviceRef])
573: : $def->getFactory();
574:
575: try {
576: $def->setFactory($this->completeStatement($factory));
577: $this->classListNeedsRefresh = false;
578:
579: $this->currentService = $name;
580: $setups = $def->getSetup();
581: foreach ($setups as &$setup) {
582: if (is_string($setup->getEntity()) && strpbrk($setup->getEntity(), ':@?\\') === false) {
583: $setup = new Statement(['@' . $name, $setup->getEntity()], $setup->arguments);
584: }
585: $setup = $this->completeStatement($setup);
586: }
587: $def->setSetup($setups);
588:
589: } catch (\Exception $e) {
590: $message = "Service '$name' (type of {$def->getType()}): " . $e->getMessage();
591: throw $e instanceof ServiceCreationException
592: ? $e->setMessage($message)
593: : new ServiceCreationException($message, 0, $e);
594:
595: } finally {
596: $this->currentService = null;
597: }
598: }
599: }
600:
601:
602: 603: 604:
605: public function completeStatement(Statement $statement)
606: {
607: $entity = $this->normalizeEntity($statement->getEntity());
608: $arguments = $statement->arguments;
609:
610: if (is_string($entity) && Strings::contains($entity, '?')) {
611:
612: } elseif ($service = $this->getServiceName($entity)) {
613: $params = [];
614: foreach ($this->definitions[$service]->parameters as $k => $v) {
615: $params[] = preg_replace('#\w+\z#', '\$$0', (is_int($k) ? $v : $k)) . (is_int($k) ? '' : ' = ' . PhpHelpers::dump($v));
616: }
617: $rm = new \ReflectionFunction(eval('return function(' . implode(', ', $params) . ') {};'));
618: $arguments = Helpers::autowireArguments($rm, $arguments, $this);
619: $entity = '@' . $service;
620:
621: } elseif ($entity === 'not') {
622:
623: } elseif (is_string($entity)) {
624: if (!class_exists($entity)) {
625: throw new ServiceCreationException("Class $entity not found.");
626: } elseif ((new ReflectionClass($entity))->isAbstract()) {
627: throw new ServiceCreationException("Class $entity is abstract.");
628: } elseif (($rm = (new ReflectionClass($entity))->getConstructor()) !== null && !$rm->isPublic()) {
629: $visibility = $rm->isProtected() ? 'protected' : 'private';
630: throw new ServiceCreationException("Class $entity has $visibility constructor.");
631: } elseif ($constructor = (new ReflectionClass($entity))->getConstructor()) {
632: $this->addDependency($constructor);
633: $arguments = Helpers::autowireArguments($constructor, $arguments, $this);
634: } elseif ($arguments) {
635: throw new ServiceCreationException("Unable to pass arguments, class $entity has no constructor.");
636: }
637:
638: } elseif (!Nette\Utils\Arrays::isList($entity) || count($entity) !== 2) {
639: throw new ServiceCreationException(sprintf('Expected class, method or property, %s given.', PhpHelpers::dump($entity)));
640:
641: } elseif (!preg_match('#^\$?(\\\\?' . PhpHelpers::PHP_IDENT . ')+(\[\])?\z#', $entity[1])) {
642: throw new ServiceCreationException("Expected function, method or property name, '$entity[1]' given.");
643:
644: } elseif ($entity[0] === '') {
645: if (!Nette\Utils\Arrays::isList($arguments)) {
646: throw new ServiceCreationException("Unable to pass specified arguments to $entity[0].");
647: } elseif (!function_exists($entity[1])) {
648: throw new ServiceCreationException("Function $entity[1] doesn't exist.");
649: }
650:
651: $rf = new \ReflectionFunction($entity[1]);
652: $this->addDependency($rf);
653: $arguments = Helpers::autowireArguments($rf, $arguments, $this);
654:
655: } else {
656: if ($entity[0] instanceof Statement) {
657: $entity[0] = $this->completeStatement($entity[0]);
658: } elseif ($service = $this->getServiceName($entity[0])) {
659: $entity[0] = '@' . $service;
660: }
661:
662: if ($entity[1][0] === '$') {
663: Validators::assert($arguments, 'list:0..1', "setup arguments for '" . Nette\Utils\Callback::toString($entity) . "'");
664: if (!$arguments && substr($entity[1], -2) === '[]') {
665: throw new ServiceCreationException("Missing argument for $entity[1].");
666: }
667: } elseif (
668: $type = empty($service) || $entity[1] === 'create'
669: ? $this->resolveEntityType($entity[0])
670: : $this->definitions[$service]->getType()
671: ) {
672: $arguments = $this->autowireArguments($type, $entity[1], $arguments);
673: }
674: }
675:
676: try {
677: array_walk_recursive($arguments, function (&$val) {
678: if ($val instanceof Statement) {
679: $val = $this->completeStatement($val);
680:
681: } elseif ($val === $this) {
682: trigger_error("Replace object ContainerBuilder in Statement arguments with '@container'.", E_USER_DEPRECATED);
683: $val = self::literal('$this');
684:
685: } elseif ($val instanceof ServiceDefinition) {
686: $val = '@' . current(array_keys($this->getDefinitions(), $val, true));
687:
688: } elseif (is_string($val) && strlen($val) > 1 && $val[0] === '@' && $val[1] !== '@') {
689: $pair = explode('::', $val, 2);
690: $name = $this->getServiceName($pair[0]);
691: if (!isset($pair[1])) {
692: $val = '@' . $name;
693: } elseif (preg_match('#^[A-Z][A-Z0-9_]*\z#', $pair[1], $m)) {
694: $val = self::literal($this->getDefinition($name)->getType() . '::' . $pair[1]);
695: } else {
696: $val = new Statement(['@' . $name, '$' . $pair[1]]);
697: }
698: }
699: });
700:
701: } catch (ServiceCreationException $e) {
702: if ((is_string($entity) || is_array($entity)) && !strpos($e->getMessage(), ' (used in')) {
703: $desc = is_string($entity)
704: ? $entity . '::__construct'
705: : (is_string($entity[0]) ? ($entity[0] . '::') : 'method ') . $entity[1];
706: $e->setMessage($e->getMessage() . " (used in $desc)");
707: }
708: throw $e;
709: }
710:
711: return new Statement($entity, $arguments);
712: }
713:
714:
715: 716: 717: 718: 719: 720:
721: public function addDependency($dep)
722: {
723: $this->dependencies[] = $dep;
724: return $this;
725: }
726:
727:
728: 729: 730: 731:
732: public function getDependencies()
733: {
734: return $this->dependencies;
735: }
736:
737:
738: 739: 740: 741: 742:
743: public function expand($value)
744: {
745: return Helpers::expand($value, $this->parameters);
746: }
747:
748:
749: 750: 751:
752: public static function literal($code, array $args = null)
753: {
754: return new Nette\PhpGenerator\PhpLiteral($args === null ? $code : PhpHelpers::formatArgs($code, $args));
755: }
756:
757:
758: 759: 760: 761:
762: public function normalizeEntity($entity)
763: {
764: if (is_string($entity) && Strings::contains($entity, '::') && !Strings::contains($entity, '?')) {
765: $entity = explode('::', $entity);
766: }
767:
768: if (is_array($entity) && $entity[0] instanceof ServiceDefinition) {
769: $entity[0] = '@' . current(array_keys($this->definitions, $entity[0], true));
770:
771: } elseif ($entity instanceof ServiceDefinition) {
772: $entity = '@' . current(array_keys($this->definitions, $entity, true));
773:
774: } elseif (is_array($entity) && $entity[0] === $this) {
775: trigger_error("Replace object ContainerBuilder in Statement entity with '@container'.", E_USER_DEPRECATED);
776: $entity[0] = '@' . self::THIS_CONTAINER;
777: }
778: return $entity;
779: }
780:
781:
782: 783: 784: 785: 786:
787: public function getServiceName($arg)
788: {
789: if (!is_string($arg) || !preg_match('#^@[\w\\\\.][^:]*\z#', $arg)) {
790: return false;
791: }
792: $service = substr($arg, 1);
793: if ($service === self::THIS_SERVICE) {
794: $service = $this->currentService;
795: }
796: if (Strings::contains($service, '\\')) {
797: if ($this->classList === false) {
798: return $service;
799: }
800: $res = $this->getByType($service);
801: if (!$res) {
802: throw new ServiceCreationException("Reference to missing service of type $service.");
803: }
804: return $res;
805: }
806: $service = isset($this->aliases[$service]) ? $this->aliases[$service] : $service;
807: if (!isset($this->definitions[$service])) {
808: throw new ServiceCreationException("Reference to missing service '$service'.");
809: }
810: return $service;
811: }
812:
813:
814: 815: 816: 817: 818:
819: public function autowireArguments($class, $method, array $arguments)
820: {
821: $rc = new ReflectionClass($class);
822: if (!$rc->hasMethod($method)) {
823: if (!Nette\Utils\Arrays::isList($arguments)) {
824: throw new ServiceCreationException("Unable to pass specified arguments to $class::$method().");
825: }
826: return $arguments;
827: }
828:
829: $rm = $rc->getMethod($method);
830: if (!$rm->isPublic()) {
831: throw new ServiceCreationException("$class::$method() is not callable.");
832: }
833: $this->addDependency($rm);
834: return Helpers::autowireArguments($rm, $arguments, $this);
835: }
836:
837:
838:
839: public function generateClasses($className = 'Container', $parentName = null)
840: {
841: trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
842: return (new PhpGenerator($this))->generate($className);
843: }
844:
845:
846:
847: public function formatStatement(Statement $statement)
848: {
849: trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
850: return (new PhpGenerator($this))->formatStatement($statement);
851: }
852:
853:
854:
855: public function formatPhp($statement, $args)
856: {
857: array_walk_recursive($args, function (&$val) {
858: if ($val instanceof Statement) {
859: $val = $this->completeStatement($val);
860:
861: } elseif ($val === $this) {
862: trigger_error("Replace object ContainerBuilder in Statement arguments with '@container'.", E_USER_DEPRECATED);
863: $val = self::literal('$this');
864:
865: } elseif ($val instanceof ServiceDefinition) {
866: $val = '@' . current(array_keys($this->getDefinitions(), $val, true));
867: }
868: });
869: return (new PhpGenerator($this))->formatPhp($statement, $args);
870: }
871: }
872: