ContainerBuilder.php 23KB

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