Definition.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
  13. /**
  14. * Definition represents a service definition.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class Definition
  19. {
  20. private $class;
  21. private $file;
  22. private $factory;
  23. private $shared = true;
  24. private $deprecated = false;
  25. private $deprecationTemplate;
  26. private $properties = array();
  27. private $calls = array();
  28. private $instanceof = array();
  29. private $autoconfigured = false;
  30. private $configurator;
  31. private $tags = array();
  32. private $public = true;
  33. private $synthetic = false;
  34. private $abstract = false;
  35. private $lazy = false;
  36. private $decoratedService;
  37. private $autowired = false;
  38. private $autowiringTypes = array();
  39. private $changes = array();
  40. protected $arguments = array();
  41. private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
  42. /**
  43. * @param string|null $class The service class
  44. * @param array $arguments An array of arguments to pass to the service constructor
  45. */
  46. public function __construct($class = null, array $arguments = array())
  47. {
  48. if (null !== $class) {
  49. $this->setClass($class);
  50. }
  51. $this->arguments = $arguments;
  52. }
  53. /**
  54. * Returns all changes tracked for the Definition object.
  55. *
  56. * @return array An array of changes for this Definition
  57. */
  58. public function getChanges()
  59. {
  60. return $this->changes;
  61. }
  62. /**
  63. * Sets the tracked changes for the Definition object.
  64. *
  65. * @param array $changes An array of changes for this Definition
  66. *
  67. * @return $this
  68. */
  69. public function setChanges(array $changes)
  70. {
  71. $this->changes = $changes;
  72. return $this;
  73. }
  74. /**
  75. * Sets a factory.
  76. *
  77. * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
  78. *
  79. * @return $this
  80. */
  81. public function setFactory($factory)
  82. {
  83. $this->changes['factory'] = true;
  84. if (is_string($factory) && false !== strpos($factory, '::')) {
  85. $factory = explode('::', $factory, 2);
  86. }
  87. $this->factory = $factory;
  88. return $this;
  89. }
  90. /**
  91. * Gets the factory.
  92. *
  93. * @return string|array The PHP function or an array containing a class/Reference and a method to call
  94. */
  95. public function getFactory()
  96. {
  97. return $this->factory;
  98. }
  99. /**
  100. * Sets the service that this service is decorating.
  101. *
  102. * @param null|string $id The decorated service id, use null to remove decoration
  103. * @param null|string $renamedId The new decorated service id
  104. * @param int $priority The priority of decoration
  105. *
  106. * @return $this
  107. *
  108. * @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
  109. */
  110. public function setDecoratedService($id, $renamedId = null, $priority = 0)
  111. {
  112. if ($renamedId && $id == $renamedId) {
  113. throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
  114. }
  115. $this->changes['decorated_service'] = true;
  116. if (null === $id) {
  117. $this->decoratedService = null;
  118. } else {
  119. $this->decoratedService = array($id, $renamedId, (int) $priority);
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Gets the service that this service is decorating.
  125. *
  126. * @return null|array An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
  127. */
  128. public function getDecoratedService()
  129. {
  130. return $this->decoratedService;
  131. }
  132. /**
  133. * Sets the service class.
  134. *
  135. * @param string $class The service class
  136. *
  137. * @return $this
  138. */
  139. public function setClass($class)
  140. {
  141. $this->changes['class'] = true;
  142. $this->class = $class;
  143. return $this;
  144. }
  145. /**
  146. * Gets the service class.
  147. *
  148. * @return string|null The service class
  149. */
  150. public function getClass()
  151. {
  152. return $this->class;
  153. }
  154. /**
  155. * Sets the arguments to pass to the service constructor/factory method.
  156. *
  157. * @return $this
  158. */
  159. public function setArguments(array $arguments)
  160. {
  161. $this->arguments = $arguments;
  162. return $this;
  163. }
  164. /**
  165. * Sets the properties to define when creating the service.
  166. *
  167. * @return $this
  168. */
  169. public function setProperties(array $properties)
  170. {
  171. $this->properties = $properties;
  172. return $this;
  173. }
  174. /**
  175. * Gets the properties to define when creating the service.
  176. *
  177. * @return array
  178. */
  179. public function getProperties()
  180. {
  181. return $this->properties;
  182. }
  183. /**
  184. * Sets a specific property.
  185. *
  186. * @param string $name
  187. * @param mixed $value
  188. *
  189. * @return $this
  190. */
  191. public function setProperty($name, $value)
  192. {
  193. $this->properties[$name] = $value;
  194. return $this;
  195. }
  196. /**
  197. * Adds an argument to pass to the service constructor/factory method.
  198. *
  199. * @param mixed $argument An argument
  200. *
  201. * @return $this
  202. */
  203. public function addArgument($argument)
  204. {
  205. $this->arguments[] = $argument;
  206. return $this;
  207. }
  208. /**
  209. * Replaces a specific argument.
  210. *
  211. * @param int|string $index
  212. * @param mixed $argument
  213. *
  214. * @return $this
  215. *
  216. * @throws OutOfBoundsException When the replaced argument does not exist
  217. */
  218. public function replaceArgument($index, $argument)
  219. {
  220. if (0 === count($this->arguments)) {
  221. throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
  222. }
  223. if (is_int($index) && ($index < 0 || $index > count($this->arguments) - 1)) {
  224. throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
  225. }
  226. if (!array_key_exists($index, $this->arguments)) {
  227. throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
  228. }
  229. $this->arguments[$index] = $argument;
  230. return $this;
  231. }
  232. /**
  233. * Sets a specific argument.
  234. *
  235. * @param int|string $key
  236. * @param mixed $value
  237. *
  238. * @return $this
  239. */
  240. public function setArgument($key, $value)
  241. {
  242. $this->arguments[$key] = $value;
  243. return $this;
  244. }
  245. /**
  246. * Gets the arguments to pass to the service constructor/factory method.
  247. *
  248. * @return array The array of arguments
  249. */
  250. public function getArguments()
  251. {
  252. return $this->arguments;
  253. }
  254. /**
  255. * Gets an argument to pass to the service constructor/factory method.
  256. *
  257. * @param int|string $index
  258. *
  259. * @return mixed The argument value
  260. *
  261. * @throws OutOfBoundsException When the argument does not exist
  262. */
  263. public function getArgument($index)
  264. {
  265. if (!array_key_exists($index, $this->arguments)) {
  266. throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
  267. }
  268. return $this->arguments[$index];
  269. }
  270. /**
  271. * Sets the methods to call after service initialization.
  272. *
  273. * @return $this
  274. */
  275. public function setMethodCalls(array $calls = array())
  276. {
  277. $this->calls = array();
  278. foreach ($calls as $call) {
  279. $this->addMethodCall($call[0], $call[1]);
  280. }
  281. return $this;
  282. }
  283. /**
  284. * Adds a method to call after service initialization.
  285. *
  286. * @param string $method The method name to call
  287. * @param array $arguments An array of arguments to pass to the method call
  288. *
  289. * @return $this
  290. *
  291. * @throws InvalidArgumentException on empty $method param
  292. */
  293. public function addMethodCall($method, array $arguments = array())
  294. {
  295. if (empty($method)) {
  296. throw new InvalidArgumentException('Method name cannot be empty.');
  297. }
  298. $this->calls[] = array($method, $arguments);
  299. return $this;
  300. }
  301. /**
  302. * Removes a method to call after service initialization.
  303. *
  304. * @param string $method The method name to remove
  305. *
  306. * @return $this
  307. */
  308. public function removeMethodCall($method)
  309. {
  310. foreach ($this->calls as $i => $call) {
  311. if ($call[0] === $method) {
  312. unset($this->calls[$i]);
  313. break;
  314. }
  315. }
  316. return $this;
  317. }
  318. /**
  319. * Check if the current definition has a given method to call after service initialization.
  320. *
  321. * @param string $method The method name to search for
  322. *
  323. * @return bool
  324. */
  325. public function hasMethodCall($method)
  326. {
  327. foreach ($this->calls as $call) {
  328. if ($call[0] === $method) {
  329. return true;
  330. }
  331. }
  332. return false;
  333. }
  334. /**
  335. * Gets the methods to call after service initialization.
  336. *
  337. * @return array An array of method calls
  338. */
  339. public function getMethodCalls()
  340. {
  341. return $this->calls;
  342. }
  343. /**
  344. * Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
  345. *
  346. * @param $instanceof ChildDefinition[]
  347. *
  348. * @return $this
  349. */
  350. public function setInstanceofConditionals(array $instanceof)
  351. {
  352. $this->instanceof = $instanceof;
  353. return $this;
  354. }
  355. /**
  356. * Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
  357. *
  358. * @return ChildDefinition[]
  359. */
  360. public function getInstanceofConditionals()
  361. {
  362. return $this->instanceof;
  363. }
  364. /**
  365. * Sets whether or not instanceof conditionals should be prepended with a global set.
  366. *
  367. * @param bool $autoconfigured
  368. *
  369. * @return $this
  370. */
  371. public function setAutoconfigured($autoconfigured)
  372. {
  373. $this->changes['autoconfigured'] = true;
  374. $this->autoconfigured = $autoconfigured;
  375. return $this;
  376. }
  377. /**
  378. * @return bool
  379. */
  380. public function isAutoconfigured()
  381. {
  382. return $this->autoconfigured;
  383. }
  384. /**
  385. * Sets tags for this definition.
  386. *
  387. * @return $this
  388. */
  389. public function setTags(array $tags)
  390. {
  391. $this->tags = $tags;
  392. return $this;
  393. }
  394. /**
  395. * Returns all tags.
  396. *
  397. * @return array An array of tags
  398. */
  399. public function getTags()
  400. {
  401. return $this->tags;
  402. }
  403. /**
  404. * Gets a tag by name.
  405. *
  406. * @param string $name The tag name
  407. *
  408. * @return array An array of attributes
  409. */
  410. public function getTag($name)
  411. {
  412. return isset($this->tags[$name]) ? $this->tags[$name] : array();
  413. }
  414. /**
  415. * Adds a tag for this definition.
  416. *
  417. * @param string $name The tag name
  418. * @param array $attributes An array of attributes
  419. *
  420. * @return $this
  421. */
  422. public function addTag($name, array $attributes = array())
  423. {
  424. $this->tags[$name][] = $attributes;
  425. return $this;
  426. }
  427. /**
  428. * Whether this definition has a tag with the given name.
  429. *
  430. * @param string $name
  431. *
  432. * @return bool
  433. */
  434. public function hasTag($name)
  435. {
  436. return isset($this->tags[$name]);
  437. }
  438. /**
  439. * Clears all tags for a given name.
  440. *
  441. * @param string $name The tag name
  442. *
  443. * @return $this
  444. */
  445. public function clearTag($name)
  446. {
  447. unset($this->tags[$name]);
  448. return $this;
  449. }
  450. /**
  451. * Clears the tags for this definition.
  452. *
  453. * @return $this
  454. */
  455. public function clearTags()
  456. {
  457. $this->tags = array();
  458. return $this;
  459. }
  460. /**
  461. * Sets a file to require before creating the service.
  462. *
  463. * @param string $file A full pathname to include
  464. *
  465. * @return $this
  466. */
  467. public function setFile($file)
  468. {
  469. $this->changes['file'] = true;
  470. $this->file = $file;
  471. return $this;
  472. }
  473. /**
  474. * Gets the file to require before creating the service.
  475. *
  476. * @return string|null The full pathname to include
  477. */
  478. public function getFile()
  479. {
  480. return $this->file;
  481. }
  482. /**
  483. * Sets if the service must be shared or not.
  484. *
  485. * @param bool $shared Whether the service must be shared or not
  486. *
  487. * @return $this
  488. */
  489. public function setShared($shared)
  490. {
  491. $this->changes['shared'] = true;
  492. $this->shared = (bool) $shared;
  493. return $this;
  494. }
  495. /**
  496. * Whether this service is shared.
  497. *
  498. * @return bool
  499. */
  500. public function isShared()
  501. {
  502. return $this->shared;
  503. }
  504. /**
  505. * Sets the visibility of this service.
  506. *
  507. * @param bool $boolean
  508. *
  509. * @return $this
  510. */
  511. public function setPublic($boolean)
  512. {
  513. $this->changes['public'] = true;
  514. $this->public = (bool) $boolean;
  515. return $this;
  516. }
  517. /**
  518. * Whether this service is public facing.
  519. *
  520. * @return bool
  521. */
  522. public function isPublic()
  523. {
  524. return $this->public;
  525. }
  526. /**
  527. * Sets the lazy flag of this service.
  528. *
  529. * @param bool $lazy
  530. *
  531. * @return $this
  532. */
  533. public function setLazy($lazy)
  534. {
  535. $this->changes['lazy'] = true;
  536. $this->lazy = (bool) $lazy;
  537. return $this;
  538. }
  539. /**
  540. * Whether this service is lazy.
  541. *
  542. * @return bool
  543. */
  544. public function isLazy()
  545. {
  546. return $this->lazy;
  547. }
  548. /**
  549. * Sets whether this definition is synthetic, that is not constructed by the
  550. * container, but dynamically injected.
  551. *
  552. * @param bool $boolean
  553. *
  554. * @return $this
  555. */
  556. public function setSynthetic($boolean)
  557. {
  558. $this->synthetic = (bool) $boolean;
  559. return $this;
  560. }
  561. /**
  562. * Whether this definition is synthetic, that is not constructed by the
  563. * container, but dynamically injected.
  564. *
  565. * @return bool
  566. */
  567. public function isSynthetic()
  568. {
  569. return $this->synthetic;
  570. }
  571. /**
  572. * Whether this definition is abstract, that means it merely serves as a
  573. * template for other definitions.
  574. *
  575. * @param bool $boolean
  576. *
  577. * @return $this
  578. */
  579. public function setAbstract($boolean)
  580. {
  581. $this->abstract = (bool) $boolean;
  582. return $this;
  583. }
  584. /**
  585. * Whether this definition is abstract, that means it merely serves as a
  586. * template for other definitions.
  587. *
  588. * @return bool
  589. */
  590. public function isAbstract()
  591. {
  592. return $this->abstract;
  593. }
  594. /**
  595. * Whether this definition is deprecated, that means it should not be called
  596. * anymore.
  597. *
  598. * @param bool $status
  599. * @param string $template Template message to use if the definition is deprecated
  600. *
  601. * @return $this
  602. *
  603. * @throws InvalidArgumentException when the message template is invalid
  604. */
  605. public function setDeprecated($status = true, $template = null)
  606. {
  607. if (null !== $template) {
  608. if (preg_match('#[\r\n]|\*/#', $template)) {
  609. throw new InvalidArgumentException('Invalid characters found in deprecation template.');
  610. }
  611. if (false === strpos($template, '%service_id%')) {
  612. throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
  613. }
  614. $this->deprecationTemplate = $template;
  615. }
  616. $this->changes['deprecated'] = true;
  617. $this->deprecated = (bool) $status;
  618. return $this;
  619. }
  620. /**
  621. * Whether this definition is deprecated, that means it should not be called
  622. * anymore.
  623. *
  624. * @return bool
  625. */
  626. public function isDeprecated()
  627. {
  628. return $this->deprecated;
  629. }
  630. /**
  631. * Message to use if this definition is deprecated.
  632. *
  633. * @param string $id Service id relying on this definition
  634. *
  635. * @return string
  636. */
  637. public function getDeprecationMessage($id)
  638. {
  639. return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
  640. }
  641. /**
  642. * Sets a configurator to call after the service is fully initialized.
  643. *
  644. * @param string|array $configurator A PHP callable
  645. *
  646. * @return $this
  647. */
  648. public function setConfigurator($configurator)
  649. {
  650. $this->changes['configurator'] = true;
  651. if (is_string($configurator) && false !== strpos($configurator, '::')) {
  652. $configurator = explode('::', $configurator, 2);
  653. }
  654. $this->configurator = $configurator;
  655. return $this;
  656. }
  657. /**
  658. * Gets the configurator to call after the service is fully initialized.
  659. *
  660. * @return callable|null The PHP callable to call
  661. */
  662. public function getConfigurator()
  663. {
  664. return $this->configurator;
  665. }
  666. /**
  667. * Sets types that will default to this definition.
  668. *
  669. * @param string[] $types
  670. *
  671. * @return $this
  672. *
  673. * @deprecated since version 3.3, to be removed in 4.0.
  674. */
  675. public function setAutowiringTypes(array $types)
  676. {
  677. @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
  678. $this->autowiringTypes = array();
  679. foreach ($types as $type) {
  680. $this->autowiringTypes[$type] = true;
  681. }
  682. return $this;
  683. }
  684. /**
  685. * Is the definition autowired?
  686. *
  687. * @return bool
  688. */
  689. public function isAutowired()
  690. {
  691. return $this->autowired;
  692. }
  693. /**
  694. * Enables/disables autowiring.
  695. *
  696. * @param bool $autowired
  697. *
  698. * @return $this
  699. */
  700. public function setAutowired($autowired)
  701. {
  702. $this->changes['autowired'] = true;
  703. $this->autowired = (bool) $autowired;
  704. return $this;
  705. }
  706. /**
  707. * Gets autowiring types that will default to this definition.
  708. *
  709. * @return string[]
  710. *
  711. * @deprecated since version 3.3, to be removed in 4.0.
  712. */
  713. public function getAutowiringTypes(/*$triggerDeprecation = true*/)
  714. {
  715. if (1 > func_num_args() || func_get_arg(0)) {
  716. @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
  717. }
  718. return array_keys($this->autowiringTypes);
  719. }
  720. /**
  721. * Adds a type that will default to this definition.
  722. *
  723. * @param string $type
  724. *
  725. * @return $this
  726. *
  727. * @deprecated since version 3.3, to be removed in 4.0.
  728. */
  729. public function addAutowiringType($type)
  730. {
  731. @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
  732. $this->autowiringTypes[$type] = true;
  733. return $this;
  734. }
  735. /**
  736. * Removes a type.
  737. *
  738. * @param string $type
  739. *
  740. * @return $this
  741. *
  742. * @deprecated since version 3.3, to be removed in 4.0.
  743. */
  744. public function removeAutowiringType($type)
  745. {
  746. @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
  747. unset($this->autowiringTypes[$type]);
  748. return $this;
  749. }
  750. /**
  751. * Will this definition default for the given type?
  752. *
  753. * @param string $type
  754. *
  755. * @return bool
  756. *
  757. * @deprecated since version 3.3, to be removed in 4.0.
  758. */
  759. public function hasAutowiringType($type)
  760. {
  761. @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
  762. return isset($this->autowiringTypes[$type]);
  763. }
  764. }