ContainerBuilder.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
  23. */
  24. class ContainerBuilder extends Container implements TaggedContainerInterface
  25. {
  26. static protected $extensions = array();
  27. protected $definitions = array();
  28. protected $aliases = array();
  29. protected $loading = array();
  30. protected $resources = array();
  31. protected $extensionConfigs = array();
  32. protected $injectors = array();
  33. protected $compiler;
  34. /**
  35. * Constructor.
  36. *
  37. * @param ParameterBagInterface $parameterBag
  38. */
  39. public function __construct(ParameterBagInterface $parameterBag = null)
  40. {
  41. parent::__construct($parameterBag);
  42. }
  43. /**
  44. * Registers an extension.
  45. *
  46. * @param ExtensionInterface $extension An extension instance
  47. */
  48. static public function registerExtension(ExtensionInterface $extension)
  49. {
  50. static::$extensions[$extension->getAlias()] = $extension;
  51. if (false !== $extension->getNamespace()) {
  52. static::$extensions[$extension->getNamespace()] = $extension;
  53. }
  54. }
  55. /**
  56. * Returns an extension by alias or namespace.
  57. *
  58. * @param string $name An alias or a namespace
  59. *
  60. * @return ExtensionInterface An extension instance
  61. */
  62. static public function getExtension($name)
  63. {
  64. if (!isset(static::$extensions[$name])) {
  65. throw new \LogicException(sprintf('Container extension "%s" is not registered', $name));
  66. }
  67. return static::$extensions[$name];
  68. }
  69. /**
  70. * Checks if we have an extension.
  71. *
  72. * @param string $name The name of the extension
  73. * @return boolean If the extension exists
  74. */
  75. static public function hasExtension($name)
  76. {
  77. return isset(static::$extensions[$name]);
  78. }
  79. /**
  80. * Returns an array of resources loaded to build this configuration.
  81. *
  82. * @return ResourceInterface[] An array of resources
  83. */
  84. public function getResources()
  85. {
  86. return array_unique($this->resources);
  87. }
  88. /**
  89. * Adds a resource for this configuration.
  90. *
  91. * @param ResourceInterface $resource A resource instance
  92. *
  93. * @return ContainerBuilder The current instance
  94. */
  95. public function addResource(ResourceInterface $resource)
  96. {
  97. $this->resources[] = $resource;
  98. return $this;
  99. }
  100. /**
  101. * Adds the object class hierarchy as resources.
  102. *
  103. * @param object $object An object instance
  104. */
  105. public function addObjectResource($object)
  106. {
  107. $parent = new \ReflectionObject($object);
  108. do {
  109. $this->addResource(new FileResource($parent->getFileName()));
  110. } while ($parent = $parent->getParentClass());
  111. }
  112. /**
  113. * Loads the configuration for an extension.
  114. *
  115. * @param string $extension The extension alias or namespace
  116. * @param string $tag The extension tag to load (without the namespace - namespace.tag)
  117. * @param array $values An array of values that customizes the extension
  118. *
  119. * @return ContainerBuilder The current instance
  120. */
  121. public function loadFromExtension($extension, $tag, array $values = array())
  122. {
  123. if (true === $this->isFrozen()) {
  124. throw new \LogicException('Cannot load from an extension on a frozen container.');
  125. }
  126. $namespace = $this->getExtension($extension)->getAlias();
  127. if (!isset($this->extensionConfigs[$namespace.':'.$tag])) {
  128. $this->extensionConfigs[$namespace.':'.$tag] = array();
  129. }
  130. $this->extensionConfigs[$namespace.':'.$tag][] = $this->getParameterBag()->resolveValue($values);
  131. return $this;
  132. }
  133. /**
  134. * Adds a compiler pass at the end of the current passes
  135. *
  136. * @param CompilerPassInterface $pass
  137. * @param string $type
  138. */
  139. public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
  140. {
  141. if (null === $this->compiler) {
  142. $this->initializeCompiler();
  143. }
  144. $this->compiler->addPass($pass, $type);
  145. $this->addObjectResource($pass);
  146. }
  147. /**
  148. * Returns the compiler pass config which can then be modified
  149. *
  150. * @return PassConfig
  151. */
  152. public function getCompilerPassConfig()
  153. {
  154. if (null === $this->compiler) {
  155. $this->initializeCompiler();
  156. }
  157. return $this->compiler->getPassConfig();
  158. }
  159. /**
  160. * Returns the compiler instance
  161. *
  162. * @return Compiler
  163. */
  164. public function getCompiler()
  165. {
  166. if (null === $this->compiler) {
  167. $this->initializeCompiler();
  168. }
  169. return $this->compiler;
  170. }
  171. /**
  172. * Returns all Scopes.
  173. *
  174. * @return array An array of scopes
  175. */
  176. public function getScopes()
  177. {
  178. return $this->scopes;
  179. }
  180. /**
  181. * Returns all Scope chilren.
  182. *
  183. * @return array An array of scope children.
  184. */
  185. public function getScopeChildren()
  186. {
  187. return $this->scopeChildren;
  188. }
  189. /**
  190. * Sets a service.
  191. *
  192. * @param string $id The service identifier
  193. * @param object $service The service instance
  194. * @param string $scope The scope
  195. *
  196. * @throws BadMethodCallException
  197. */
  198. public function set($id, $service, $scope = self::SCOPE_CONTAINER)
  199. {
  200. if ($this->isFrozen()) {
  201. throw new \BadMethodCallException('Setting service on a frozen container is not allowed');
  202. }
  203. $id = strtolower($id);
  204. unset($this->definitions[$id], $this->aliases[$id]);
  205. parent::set($id, $service, $scope);
  206. }
  207. /**
  208. * Removes a service.
  209. *
  210. * @param string $id The service identifier
  211. */
  212. public function remove($id)
  213. {
  214. unset($this->definitions[strtolower($id)]);
  215. }
  216. /**
  217. * Returns true if the given service is defined.
  218. *
  219. * @param string $id The service identifier
  220. *
  221. * @return Boolean true if the service is defined, false otherwise
  222. */
  223. public function has($id)
  224. {
  225. $id = strtolower($id);
  226. return isset($this->definitions[$id]) || isset($this->aliases[$id]) || parent::has($id);
  227. }
  228. /**
  229. * Gets a service.
  230. *
  231. * @param string $id The service identifier
  232. * @param int $invalidBehavior The behavior when the service does not exist
  233. *
  234. * @return object The associated service
  235. *
  236. * @throws \InvalidArgumentException if the service is not defined
  237. * @throws \LogicException if the service has a circular reference to itself
  238. *
  239. * @see Reference
  240. */
  241. public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
  242. {
  243. $id = strtolower($id);
  244. try {
  245. return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
  246. } catch (\InvalidArgumentException $e) {
  247. if (isset($this->loading[$id])) {
  248. throw new \LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
  249. }
  250. if (!$this->hasDefinition($id) && isset($this->aliases[$id])) {
  251. return $this->get($this->aliases[$id]);
  252. }
  253. try {
  254. $definition = $this->getDefinition($id);
  255. } catch (\InvalidArgumentException $e) {
  256. if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
  257. return null;
  258. }
  259. throw $e;
  260. }
  261. $this->loading[$id] = true;
  262. $service = $this->createService($definition, $id);
  263. unset($this->loading[$id]);
  264. return $service;
  265. }
  266. }
  267. /**
  268. * Merges a ContainerBuilder with the current ContainerBuilder configuration.
  269. *
  270. * Service definitions overrides the current defined ones.
  271. *
  272. * But for parameters, they are overridden by the current ones. It allows
  273. * the parameters passed to the container constructor to have precedence
  274. * over the loaded ones.
  275. *
  276. * $container = new ContainerBuilder(array('foo' => 'bar'));
  277. * $loader = new LoaderXXX($container);
  278. * $loader->load('resource_name');
  279. * $container->register('foo', new stdClass());
  280. *
  281. * In the above example, even if the loaded resource defines a foo
  282. * parameter, the value will still be 'bar' as defined in the ContainerBuilder
  283. * constructor.
  284. *
  285. * @param ContainerBuilder $container The ContainerBuilder instance to merge.
  286. * @throws \LogicException when this ContainerBuilder is frozen
  287. */
  288. public function merge(ContainerBuilder $container)
  289. {
  290. if (true === $this->isFrozen()) {
  291. throw new \LogicException('Cannot merge on a frozen container.');
  292. }
  293. $this->addDefinitions($container->getDefinitions());
  294. $this->addAliases($container->getAliases());
  295. $this->addInterfaceInjectors($container->getInterfaceInjectors());
  296. $this->parameterBag->add($container->getParameterBag()->all());
  297. foreach ($container->getResources() as $resource) {
  298. $this->addResource($resource);
  299. }
  300. foreach ($container->getExtensionConfigs() as $name => $configs) {
  301. if (isset($this->extensionConfigs[$name])) {
  302. $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $configs);
  303. } else {
  304. $this->extensionConfigs[$name] = $configs;
  305. }
  306. }
  307. }
  308. /**
  309. * Returns the containers for the registered extensions.
  310. *
  311. * @return ExtensionInterface[] An array of extension containers
  312. */
  313. public function getExtensionConfigs()
  314. {
  315. return $this->extensionConfigs;
  316. }
  317. /**
  318. * Sets the extension configs array
  319. *
  320. * @param array $config
  321. * @return void
  322. */
  323. public function setExtensionConfigs(array $config)
  324. {
  325. $this->extensionConfigs = $config;
  326. }
  327. /**
  328. * Compiles the container.
  329. *
  330. * This method passes the container to compiler
  331. * passes whose job is to manipulate and optimize
  332. * the container.
  333. *
  334. * The main compiler passes roughly do four things:
  335. *
  336. * * The extension configurations are merged;
  337. * * Parameter values are resolved;
  338. * * The parameter bag is frozen;
  339. * * Extension loading is disabled.
  340. */
  341. public function compile()
  342. {
  343. if (null === $this->compiler) {
  344. $this->initializeCompiler();
  345. }
  346. $this->compiler->compile($this);
  347. $this->setExtensionConfigs(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. * Initializes the compiler
  720. *
  721. * @return void
  722. */
  723. protected function initializeCompiler()
  724. {
  725. $this->compiler = new Compiler();
  726. foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
  727. $this->addObjectResource($pass);
  728. }
  729. }
  730. /**
  731. * Returns the Service Conditionals.
  732. *
  733. * @param mixed $value An array of conditionals to return.
  734. * @return array An array of Service conditionals
  735. */
  736. static public function getServiceConditionals($value)
  737. {
  738. $services = array();
  739. if (is_array($value)) {
  740. foreach ($value as $v) {
  741. $services = array_unique(array_merge($services, self::getServiceConditionals($v)));
  742. }
  743. } elseif (is_object($value) && $value instanceof Reference && $value->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
  744. $services[] = (string) $value;
  745. }
  746. return $services;
  747. }
  748. }