Drupal investigation

Definition.php 22KB

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