Definition.php 22 KB

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