ContainerBuilder.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Compiler\Compiler;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  14. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  15. use Symfony\Component\DependencyInjection\InterfaceInjector;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Component\Config\Resource\FileResource;
  18. use Symfony\Component\Config\Resource\ResourceInterface;
  19. /**
  20. * ContainerBuilder is a DI container that provides an API to easily describe services.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class ContainerBuilder extends Container implements TaggedContainerInterface
  25. {
  26. protected $extensions = array();
  27. protected $extensionsByNs = array();
  28. protected $definitions = array();
  29. protected $aliases = array();
  30. protected $loading = array();
  31. protected $resources = array();
  32. protected $extensionConfigs = array();
  33. protected $injectors = array();
  34. protected $compiler;
  35. /**
  36. * Registers an extension.
  37. *
  38. * @param ExtensionInterface $extension An extension instance
  39. */
  40. public function registerExtension(ExtensionInterface $extension)
  41. {
  42. $this->extensions[$extension->getAlias()] = $extension;
  43. if (false !== $extension->getNamespace()) {
  44. $this->extensionsByNs[$extension->getNamespace()] = $extension;
  45. }
  46. }
  47. /**
  48. * Returns an extension by alias or namespace.
  49. *
  50. * @param string $name An alias or a namespace
  51. *
  52. * @return ExtensionInterface An extension instance
  53. */
  54. public function getExtension($name)
  55. {
  56. if (isset($this->extensions[$name])) {
  57. return $this->extensions[$name];
  58. }
  59. if (isset($this->extensionsByNs[$name])) {
  60. return $this->extensionsByNs[$name];
  61. }
  62. throw new \LogicException(sprintf('Container extension "%s" is not registered', $name));
  63. }
  64. /**
  65. * Returns all registered extensions.
  66. *
  67. * @return array An array of ExtensionInterface
  68. */
  69. public function getExtensions()
  70. {
  71. return $this->extensions;
  72. }
  73. /**
  74. * Checks if we have an extension.
  75. *
  76. * @param string $name The name of the extension
  77. * @return boolean If the extension exists
  78. */
  79. public function hasExtension($name)
  80. {
  81. return isset($this->extensions[$name]) || isset($this->extensionsByNs[$name]);
  82. }
  83. /**
  84. * Returns an array of resources loaded to build this configuration.
  85. *
  86. * @return ResourceInterface[] An array of resources
  87. */
  88. public function getResources()
  89. {
  90. return array_unique($this->resources);
  91. }
  92. /**
  93. * Adds a resource for this configuration.
  94. *
  95. * @param ResourceInterface $resource A resource instance
  96. *
  97. * @return ContainerBuilder The current instance
  98. */
  99. public function addResource(ResourceInterface $resource)
  100. {
  101. $this->resources[] = $resource;
  102. return $this;
  103. }
  104. /**
  105. * Adds the object class hierarchy as resources.
  106. *
  107. * @param object $object An object instance
  108. */
  109. public function addObjectResource($object)
  110. {
  111. $parent = new \ReflectionObject($object);
  112. do {
  113. $this->addResource(new FileResource($parent->getFileName()));
  114. } while ($parent = $parent->getParentClass());
  115. }
  116. /**
  117. * Loads the configuration for an extension.
  118. *
  119. * @param string $extension The extension alias or namespace
  120. * @param array $values An array of values that customizes the extension
  121. *
  122. * @return ContainerBuilder The current instance
  123. */
  124. public function loadFromExtension($extension, array $values = array())
  125. {
  126. if (true === $this->isFrozen()) {
  127. throw new \LogicException('Cannot load from an extension on a frozen container.');
  128. }
  129. $namespace = $this->getExtension($extension)->getAlias();
  130. if (!isset($this->extensionConfigs[$namespace])) {
  131. $this->extensionConfigs[$namespace] = array();
  132. }
  133. $this->extensionConfigs[$namespace][] = $values;
  134. return $this;
  135. }
  136. /**
  137. * Adds a compiler pass.
  138. *
  139. * @param CompilerPassInterface $pass A compiler pass
  140. * @param string $type The type of compiler pass
  141. */
  142. public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
  143. {
  144. if (null === $this->compiler) {
  145. $this->compiler = new Compiler();
  146. }
  147. $this->compiler->addPass($pass, $type);
  148. $this->addObjectResource($pass);
  149. }
  150. /**
  151. * Returns the compiler pass config which can then be modified.
  152. *
  153. * @return PassConfig The compiler pass config
  154. */
  155. public function getCompilerPassConfig()
  156. {
  157. if (null === $this->compiler) {
  158. $this->compiler = new Compiler();
  159. }
  160. return $this->compiler->getPassConfig();
  161. }
  162. /**
  163. * Returns the compiler.
  164. *
  165. * @return Compiler The compiler
  166. */
  167. public function getCompiler()
  168. {
  169. if (null === $this->compiler) {
  170. $this->compiler = new Compiler();
  171. }
  172. return $this->compiler;
  173. }
  174. /**
  175. * Returns all Scopes.
  176. *
  177. * @return array An array of scopes
  178. */
  179. public function getScopes()
  180. {
  181. return $this->scopes;
  182. }
  183. /**
  184. * Returns all Scope chilren.
  185. *
  186. * @return array An array of scope children.
  187. */
  188. public function getScopeChildren()
  189. {
  190. return $this->scopeChildren;
  191. }
  192. /**
  193. * Sets a service.
  194. *
  195. * @param string $id The service identifier
  196. * @param object $service The service instance
  197. * @param string $scope The scope
  198. *
  199. * @throws BadMethodCallException
  200. */
  201. public function set($id, $service, $scope = self::SCOPE_CONTAINER)
  202. {
  203. if ($this->isFrozen()) {
  204. throw new \BadMethodCallException('Setting service on a frozen container is not allowed');
  205. }
  206. $id = strtolower($id);
  207. unset($this->definitions[$id], $this->aliases[$id]);
  208. parent::set($id, $service, $scope);
  209. }
  210. /**
  211. * Removes a service.
  212. *
  213. * @param string $id The service identifier
  214. */
  215. public function remove($id)
  216. {
  217. unset($this->definitions[strtolower($id)]);
  218. }
  219. /**
  220. * Returns true if the given service is defined.
  221. *
  222. * @param string $id The service identifier
  223. *
  224. * @return Boolean true if the service is defined, false otherwise
  225. */
  226. public function has($id)
  227. {
  228. $id = strtolower($id);
  229. return isset($this->definitions[$id]) || isset($this->aliases[$id]) || parent::has($id);
  230. }
  231. /**
  232. * Gets a service.
  233. *
  234. * @param string $id The service identifier
  235. * @param int $invalidBehavior The behavior when the service does not exist
  236. *
  237. * @return object The associated service
  238. *
  239. * @throws \InvalidArgumentException if the service is not defined
  240. * @throws \LogicException if the service has a circular reference to itself
  241. *
  242. * @see Reference
  243. */
  244. public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
  245. {
  246. $id = strtolower($id);
  247. try {
  248. return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
  249. } catch (\InvalidArgumentException $e) {
  250. if (isset($this->loading[$id])) {
  251. throw new \LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
  252. }
  253. if (!$this->hasDefinition($id) && isset($this->aliases[$id])) {
  254. return $this->get($this->aliases[$id]);
  255. }
  256. try {
  257. $definition = $this->getDefinition($id);
  258. } catch (\InvalidArgumentException $e) {
  259. if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
  260. return null;
  261. }
  262. throw $e;
  263. }
  264. $this->loading[$id] = true;
  265. $service = $this->createService($definition, $id);
  266. unset($this->loading[$id]);
  267. return $service;
  268. }
  269. }
  270. /**
  271. * Merges a ContainerBuilder with the current ContainerBuilder configuration.
  272. *
  273. * Service definitions overrides the current defined ones.
  274. *
  275. * But for parameters, they are overridden by the current ones. It allows
  276. * the parameters passed to the container constructor to have precedence
  277. * over the loaded ones.
  278. *
  279. * $container = new ContainerBuilder(array('foo' => 'bar'));
  280. * $loader = new LoaderXXX($container);
  281. * $loader->load('resource_name');
  282. * $container->register('foo', new stdClass());
  283. *
  284. * In the above example, even if the loaded resource defines a foo
  285. * parameter, the value will still be 'bar' as defined in the ContainerBuilder
  286. * constructor.
  287. *
  288. * @param ContainerBuilder $container The ContainerBuilder instance to merge.
  289. * @throws \LogicException when this ContainerBuilder is frozen
  290. */
  291. public function merge(ContainerBuilder $container)
  292. {
  293. if (true === $this->isFrozen()) {
  294. throw new \LogicException('Cannot merge on a frozen container.');
  295. }
  296. $this->addDefinitions($container->getDefinitions());
  297. $this->addAliases($container->getAliases());
  298. $this->addInterfaceInjectors($container->getInterfaceInjectors());
  299. $this->parameterBag->add($container->getParameterBag()->all());
  300. foreach ($container->getResources() as $resource) {
  301. $this->addResource($resource);
  302. }
  303. foreach ($this->extensions as $name => $extension) {
  304. if (!isset($this->extensionConfigs[$name])) {
  305. $this->extensionConfigs[$name] = array();
  306. }
  307. $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
  308. }
  309. }
  310. /**
  311. * Returns the configuration array for the given extension.
  312. *
  313. * @param string $name The name of the extension
  314. *
  315. * @return array An array of configuration
  316. */
  317. public function getExtensionConfig($name)
  318. {
  319. if (!isset($this->extensionConfigs[$name])) {
  320. $this->extensionConfigs[$name] = array();
  321. }
  322. return $this->extensionConfigs[$name];
  323. }
  324. /**
  325. * Compiles the container.
  326. *
  327. * This method passes the container to compiler
  328. * passes whose job is to manipulate and optimize
  329. * the container.
  330. *
  331. * The main compiler passes roughly do four things:
  332. *
  333. * * The extension configurations are merged;
  334. * * Parameter values are resolved;
  335. * * The parameter bag is frozen;
  336. * * Extension loading is disabled.
  337. */
  338. public function compile()
  339. {
  340. if (null === $this->compiler) {
  341. $this->compiler = new Compiler();
  342. }
  343. foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
  344. $this->addObjectResource($pass);
  345. }
  346. $this->compiler->compile($this);
  347. $this->extensionConfigs = array();
  348. parent::compile();
  349. }
  350. /**
  351. * Gets all service ids.
  352. *
  353. * @return array An array of all defined service ids
  354. */
  355. public function getServiceIds()
  356. {
  357. return array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliases), parent::getServiceIds()));
  358. }
  359. /**
  360. * Adds the service aliases.
  361. *
  362. * @param array $aliases An array of aliases
  363. */
  364. public function addAliases(array $aliases)
  365. {
  366. foreach ($aliases as $alias => $id) {
  367. $this->setAlias($alias, $id);
  368. }
  369. }
  370. /**
  371. * Sets the service aliases.
  372. *
  373. * @param array $definitions An array of service definitions
  374. */
  375. public function setAliases(array $aliases)
  376. {
  377. $this->aliases = array();
  378. $this->addAliases($aliases);
  379. }
  380. /**
  381. * Sets an alias for an existing service.
  382. *
  383. * @param string $alias The alias to create
  384. * @param mixed $id The service to alias
  385. */
  386. public function setAlias($alias, $id)
  387. {
  388. $alias = strtolower($alias);
  389. if (is_string($id)) {
  390. $id = new Alias($id);
  391. } else if (!$id instanceof Alias) {
  392. throw new \InvalidArgumentException('$id must be a string, or an Alias object.');
  393. }
  394. if ($alias === strtolower($id)) {
  395. throw new \InvalidArgumentException('An alias can not reference itself, got a circular reference on "'.$alias.'".');
  396. }
  397. unset($this->definitions[$alias]);
  398. $this->aliases[$alias] = $id;
  399. }
  400. /**
  401. * Removes an alias.
  402. *
  403. * @param string $alias The alias to remove
  404. */
  405. public function removeAlias($alias)
  406. {
  407. unset($this->aliases[strtolower($alias)]);
  408. }
  409. /**
  410. * Returns true if an alias exists under the given identifier.
  411. *
  412. * @param string $id The service identifier
  413. *
  414. * @return Boolean true if the alias exists, false otherwise
  415. */
  416. public function hasAlias($id)
  417. {
  418. return isset($this->aliases[strtolower($id)]);
  419. }
  420. /**
  421. * Gets all defined aliases.
  422. *
  423. * @return array An array of aliases
  424. */
  425. public function getAliases()
  426. {
  427. return $this->aliases;
  428. }
  429. /**
  430. * Gets an alias.
  431. *
  432. * @param string $id The service identifier
  433. *
  434. * @return string The aliased service identifier
  435. *
  436. * @throws \InvalidArgumentException if the alias does not exist
  437. */
  438. public function getAlias($id)
  439. {
  440. $id = strtolower($id);
  441. if (!$this->hasAlias($id)) {
  442. throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
  443. }
  444. return $this->aliases[$id];
  445. }
  446. /**
  447. * Adds an InterfaceInjector.
  448. *
  449. * @param InterfaceInjector $injector
  450. */
  451. public function addInterfaceInjector(InterfaceInjector $injector)
  452. {
  453. $class = $injector->getClass();
  454. if (isset($this->injectors[$class])) {
  455. return $this->injectors[$class]->merge($injector);
  456. }
  457. $this->injectors[$class] = $injector;
  458. }
  459. /**
  460. * Adds multiple InterfaceInjectors.
  461. *
  462. * @param array $injectors An array of InterfaceInjectors
  463. */
  464. public function addInterfaceInjectors(array $injectors)
  465. {
  466. foreach ($injectors as $injector) {
  467. $this->addInterfaceInjector($injector);
  468. }
  469. }
  470. /**
  471. * Gets defined InterfaceInjectors. If a service is provided, only that
  472. * support the service will be returned.
  473. *
  474. * @param string $service If provided, only injectors supporting this service will be returned
  475. *
  476. * @return array An array of InterfaceInjectors
  477. */
  478. public function getInterfaceInjectors($service = null)
  479. {
  480. if (null === $service) {
  481. return $this->injectors;
  482. }
  483. return array_filter($this->injectors, function(InterfaceInjector $injector) use ($service) {
  484. return $injector->supports($service);
  485. });
  486. }
  487. /**
  488. * Returns true if an InterfaceInjector is defined for the class.
  489. *
  490. * @param string $class The class
  491. *
  492. * @return Boolean true if at least one InterfaceInjector is defined, false otherwise
  493. */
  494. public function hasInterfaceInjectorForClass($class)
  495. {
  496. return array_key_exists($class, $this->injectors);
  497. }
  498. /**
  499. * Sets the defined InterfaceInjectors.
  500. *
  501. * @param array $injectors An array of InterfaceInjectors indexed by class names
  502. */
  503. public function setInterfaceInjectors(array $injectors)
  504. {
  505. $this->injectors = $injectors;
  506. }
  507. /**
  508. * Registers a service definition.
  509. *
  510. * This methods allows for simple registration of service definition
  511. * with a fluid interface.
  512. *
  513. * @param string $id The service identifier
  514. * @param string $class The service class
  515. *
  516. * @return Definition A Definition instance
  517. */
  518. public function register($id, $class = null)
  519. {
  520. return $this->setDefinition(strtolower($id), new Definition($class));
  521. }
  522. /**
  523. * Adds the service definitions.
  524. *
  525. * @param Definition[] $definitions An array of service definitions
  526. */
  527. public function addDefinitions(array $definitions)
  528. {
  529. foreach ($definitions as $id => $definition) {
  530. $this->setDefinition($id, $definition);
  531. }
  532. }
  533. /**
  534. * Sets the service definitions.
  535. *
  536. * @param array $definitions An array of service definitions
  537. */
  538. public function setDefinitions(array $definitions)
  539. {
  540. $this->definitions = array();
  541. $this->addDefinitions($definitions);
  542. }
  543. /**
  544. * Gets all service definitions.
  545. *
  546. * @return array An array of Definition instances
  547. */
  548. public function getDefinitions()
  549. {
  550. return $this->definitions;
  551. }
  552. /**
  553. * Sets a service definition.
  554. *
  555. * @param string $id The service identifier
  556. * @param Definition $definition A Definition instance
  557. *
  558. * @throws BadMethodCallException
  559. */
  560. public function setDefinition($id, Definition $definition)
  561. {
  562. if ($this->isFrozen()) {
  563. throw new \BadMethodCallException('Adding definition to a frozen container is not allowed');
  564. }
  565. $id = strtolower($id);
  566. unset($this->aliases[$id]);
  567. return $this->definitions[$id] = $definition;
  568. }
  569. /**
  570. * Returns true if a service definition exists under the given identifier.
  571. *
  572. * @param string $id The service identifier
  573. *
  574. * @return Boolean true if the service definition exists, false otherwise
  575. */
  576. public function hasDefinition($id)
  577. {
  578. return array_key_exists(strtolower($id), $this->definitions);
  579. }
  580. /**
  581. * Gets a service definition.
  582. *
  583. * @param string $id The service identifier
  584. *
  585. * @return Definition A Definition instance
  586. *
  587. * @throws \InvalidArgumentException if the service definition does not exist
  588. */
  589. public function getDefinition($id)
  590. {
  591. $id = strtolower($id);
  592. if (!$this->hasDefinition($id)) {
  593. throw new \InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
  594. }
  595. return $this->definitions[$id];
  596. }
  597. /**
  598. * Gets a service definition by id or alias.
  599. *
  600. * The method "unaliases" recursively to return a Definition instance.
  601. *
  602. * @param string $id The service identifier or alias
  603. *
  604. * @return Definition A Definition instance
  605. *
  606. * @throws \InvalidArgumentException if the service definition does not exist
  607. */
  608. public function findDefinition($id)
  609. {
  610. $id = strtolower($id);
  611. if ($this->hasAlias($id)) {
  612. return $this->findDefinition((string) $this->getAlias($id));
  613. }
  614. return $this->getDefinition($id);
  615. }
  616. /**
  617. * Creates a service for a service definition.
  618. *
  619. * @param Definition $definition A service definition instance
  620. * @param string $id The service identifier
  621. *
  622. * @return object The service described by the service definition
  623. *
  624. * @throws \InvalidArgumentException When configure callable is not callable
  625. */
  626. protected function createService(Definition $definition, $id)
  627. {
  628. if (null !== $definition->getFile()) {
  629. require_once $this->getParameterBag()->resolveValue($definition->getFile());
  630. }
  631. $arguments = $this->resolveServices($this->getParameterBag()->resolveValue($definition->getArguments()));
  632. if (null !== $definition->getFactoryMethod()) {
  633. if (null !== $definition->getFactoryClass()) {
  634. $factory = $this->getParameterBag()->resolveValue($definition->getFactoryClass());
  635. } elseif (null !== $definition->getFactoryService()) {
  636. $factory = $this->get($this->getParameterBag()->resolveValue($definition->getFactoryService()));
  637. } else {
  638. throw new \RuntimeException('Cannot create service from factory method without a factory service or factory class.');
  639. }
  640. $service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
  641. } else {
  642. $r = new \ReflectionClass($this->getParameterBag()->resolveValue($definition->getClass()));
  643. $service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
  644. }
  645. foreach ($this->getInterfaceInjectors($service) as $injector) {
  646. $injector->processDefinition($definition, $service);
  647. }
  648. if (self::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
  649. if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
  650. throw new \RuntimeException('You tried to create a service of an inactive scope.');
  651. }
  652. $this->services[$lowerId = strtolower($id)] = $service;
  653. if (self::SCOPE_CONTAINER !== $scope) {
  654. $this->scopedServices[$scope][$lowerId] = $service;
  655. }
  656. }
  657. foreach ($definition->getMethodCalls() as $call) {
  658. $services = self::getServiceConditionals($call[1]);
  659. $ok = true;
  660. foreach ($services as $s) {
  661. if (!$this->has($s)) {
  662. $ok = false;
  663. break;
  664. }
  665. }
  666. if ($ok) {
  667. call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
  668. }
  669. }
  670. if ($callable = $definition->getConfigurator()) {
  671. if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
  672. $callable[0] = $this->get((string) $callable[0]);
  673. } elseif (is_array($callable)) {
  674. $callable[0] = $this->getParameterBag()->resolveValue($callable[0]);
  675. }
  676. if (!is_callable($callable)) {
  677. throw new \InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
  678. }
  679. call_user_func($callable, $service);
  680. }
  681. return $service;
  682. }
  683. /**
  684. * Replaces service references by the real service instance.
  685. *
  686. * @param mixed $value A value
  687. *
  688. * @return mixed The same value with all service references replaced by the real service instances
  689. */
  690. public function resolveServices($value)
  691. {
  692. if (is_array($value)) {
  693. foreach ($value as &$v) {
  694. $v = $this->resolveServices($v);
  695. }
  696. } else if (is_object($value) && $value instanceof Reference) {
  697. $value = $this->get((string) $value, $value->getInvalidBehavior());
  698. }
  699. return $value;
  700. }
  701. /**
  702. * Returns service ids for a given tag.
  703. *
  704. * @param string $name The tag name
  705. *
  706. * @return array An array of tags
  707. */
  708. public function findTaggedServiceIds($name)
  709. {
  710. $tags = array();
  711. foreach ($this->getDefinitions() as $id => $definition) {
  712. if ($definition->getTag($name)) {
  713. $tags[$id] = $definition->getTag($name);
  714. }
  715. }
  716. return $tags;
  717. }
  718. /**
  719. * Returns the Service Conditionals.
  720. *
  721. * @param mixed $value An array of conditionals to return.
  722. * @return array An array of Service conditionals
  723. */
  724. static public function getServiceConditionals($value)
  725. {
  726. $services = array();
  727. if (is_array($value)) {
  728. foreach ($value as $v) {
  729. $services = array_unique(array_merge($services, self::getServiceConditionals($v)));
  730. }
  731. } elseif (is_object($value) && $value instanceof Reference && $value->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
  732. $services[] = (string) $value;
  733. }
  734. return $services;
  735. }
  736. }