Definition.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. * @api
  19. */
  20. class Definition
  21. {
  22. private $class;
  23. private $file;
  24. private $factoryClass;
  25. private $factoryMethod;
  26. private $factoryService;
  27. private $scope;
  28. private $properties;
  29. private $calls;
  30. private $configurator;
  31. private $tags;
  32. private $public;
  33. private $synthetic;
  34. private $abstract;
  35. private $synchronized;
  36. private $lazy;
  37. protected $arguments;
  38. /**
  39. * Constructor.
  40. *
  41. * @param string $class The service class
  42. * @param array $arguments An array of arguments to pass to the service constructor
  43. *
  44. * @api
  45. */
  46. public function __construct($class = null, array $arguments = array())
  47. {
  48. $this->class = $class;
  49. $this->arguments = $arguments;
  50. $this->calls = array();
  51. $this->scope = ContainerInterface::SCOPE_CONTAINER;
  52. $this->tags = array();
  53. $this->public = true;
  54. $this->synthetic = false;
  55. $this->synchronized = false;
  56. $this->lazy = false;
  57. $this->abstract = false;
  58. $this->properties = array();
  59. }
  60. /**
  61. * Sets the name of the class that acts as a factory using the factory method,
  62. * which will be invoked statically.
  63. *
  64. * @param string $factoryClass The factory class name
  65. *
  66. * @return Definition The current instance
  67. *
  68. * @api
  69. */
  70. public function setFactoryClass($factoryClass)
  71. {
  72. $this->factoryClass = $factoryClass;
  73. return $this;
  74. }
  75. /**
  76. * Gets the factory class.
  77. *
  78. * @return string The factory class name
  79. *
  80. * @api
  81. */
  82. public function getFactoryClass()
  83. {
  84. return $this->factoryClass;
  85. }
  86. /**
  87. * Sets the factory method able to create an instance of this class.
  88. *
  89. * @param string $factoryMethod The factory method name
  90. *
  91. * @return Definition The current instance
  92. *
  93. * @api
  94. */
  95. public function setFactoryMethod($factoryMethod)
  96. {
  97. $this->factoryMethod = $factoryMethod;
  98. return $this;
  99. }
  100. /**
  101. * Gets the factory method.
  102. *
  103. * @return string The factory method name
  104. *
  105. * @api
  106. */
  107. public function getFactoryMethod()
  108. {
  109. return $this->factoryMethod;
  110. }
  111. /**
  112. * Sets the name of the service that acts as a factory using the factory method.
  113. *
  114. * @param string $factoryService The factory service id
  115. *
  116. * @return Definition The current instance
  117. *
  118. * @api
  119. */
  120. public function setFactoryService($factoryService)
  121. {
  122. $this->factoryService = $factoryService;
  123. return $this;
  124. }
  125. /**
  126. * Gets the factory service id.
  127. *
  128. * @return string The factory service id
  129. *
  130. * @api
  131. */
  132. public function getFactoryService()
  133. {
  134. return $this->factoryService;
  135. }
  136. /**
  137. * Sets the service class.
  138. *
  139. * @param string $class The service class
  140. *
  141. * @return Definition The current instance
  142. *
  143. * @api
  144. */
  145. public function setClass($class)
  146. {
  147. $this->class = $class;
  148. return $this;
  149. }
  150. /**
  151. * Gets the service class.
  152. *
  153. * @return string The service class
  154. *
  155. * @api
  156. */
  157. public function getClass()
  158. {
  159. return $this->class;
  160. }
  161. /**
  162. * Sets the arguments to pass to the service constructor/factory method.
  163. *
  164. * @param array $arguments An array of arguments
  165. *
  166. * @return Definition The current instance
  167. *
  168. * @api
  169. */
  170. public function setArguments(array $arguments)
  171. {
  172. $this->arguments = $arguments;
  173. return $this;
  174. }
  175. /**
  176. * @api
  177. */
  178. public function setProperties(array $properties)
  179. {
  180. $this->properties = $properties;
  181. return $this;
  182. }
  183. /**
  184. * @api
  185. */
  186. public function getProperties()
  187. {
  188. return $this->properties;
  189. }
  190. /**
  191. * @api
  192. */
  193. public function setProperty($name, $value)
  194. {
  195. $this->properties[$name] = $value;
  196. return $this;
  197. }
  198. /**
  199. * Adds an argument to pass to the service constructor/factory method.
  200. *
  201. * @param mixed $argument An argument
  202. *
  203. * @return Definition The current instance
  204. *
  205. * @api
  206. */
  207. public function addArgument($argument)
  208. {
  209. $this->arguments[] = $argument;
  210. return $this;
  211. }
  212. /**
  213. * Sets a specific argument
  214. *
  215. * @param integer $index
  216. * @param mixed $argument
  217. *
  218. * @return Definition The current instance
  219. *
  220. * @throws OutOfBoundsException When the replaced argument does not exist
  221. *
  222. * @api
  223. */
  224. public function replaceArgument($index, $argument)
  225. {
  226. if ($index < 0 || $index > count($this->arguments) - 1) {
  227. throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
  228. }
  229. $this->arguments[$index] = $argument;
  230. return $this;
  231. }
  232. /**
  233. * Gets the arguments to pass to the service constructor/factory method.
  234. *
  235. * @return array The array of arguments
  236. *
  237. * @api
  238. */
  239. public function getArguments()
  240. {
  241. return $this->arguments;
  242. }
  243. /**
  244. * Gets an argument to pass to the service constructor/factory method.
  245. *
  246. * @param integer $index
  247. *
  248. * @return mixed The argument value
  249. *
  250. * @throws OutOfBoundsException When the argument does not exist
  251. *
  252. * @api
  253. */
  254. public function getArgument($index)
  255. {
  256. if ($index < 0 || $index > count($this->arguments) - 1) {
  257. throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
  258. }
  259. return $this->arguments[$index];
  260. }
  261. /**
  262. * Sets the methods to call after service initialization.
  263. *
  264. * @param array $calls An array of method calls
  265. *
  266. * @return Definition The current instance
  267. *
  268. * @api
  269. */
  270. public function setMethodCalls(array $calls = array())
  271. {
  272. $this->calls = array();
  273. foreach ($calls as $call) {
  274. $this->addMethodCall($call[0], $call[1]);
  275. }
  276. return $this;
  277. }
  278. /**
  279. * Adds a method to call after service initialization.
  280. *
  281. * @param string $method The method name to call
  282. * @param array $arguments An array of arguments to pass to the method call
  283. *
  284. * @return Definition The current instance
  285. *
  286. * @throws InvalidArgumentException on empty $method param
  287. *
  288. * @api
  289. */
  290. public function addMethodCall($method, array $arguments = array())
  291. {
  292. if (empty($method)) {
  293. throw new InvalidArgumentException(sprintf('Method name cannot be empty.'));
  294. }
  295. $this->calls[] = array($method, $arguments);
  296. return $this;
  297. }
  298. /**
  299. * Removes a method to call after service initialization.
  300. *
  301. * @param string $method The method name to remove
  302. *
  303. * @return Definition The current instance
  304. *
  305. * @api
  306. */
  307. public function removeMethodCall($method)
  308. {
  309. foreach ($this->calls as $i => $call) {
  310. if ($call[0] === $method) {
  311. unset($this->calls[$i]);
  312. break;
  313. }
  314. }
  315. return $this;
  316. }
  317. /**
  318. * Check if the current definition has a given method to call after service initialization.
  319. *
  320. * @param string $method The method name to search for
  321. *
  322. * @return Boolean
  323. *
  324. * @api
  325. */
  326. public function hasMethodCall($method)
  327. {
  328. foreach ($this->calls as $call) {
  329. if ($call[0] === $method) {
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. /**
  336. * Gets the methods to call after service initialization.
  337. *
  338. * @return array An array of method calls
  339. *
  340. * @api
  341. */
  342. public function getMethodCalls()
  343. {
  344. return $this->calls;
  345. }
  346. /**
  347. * Sets tags for this definition
  348. *
  349. * @param array $tags
  350. *
  351. * @return Definition the current instance
  352. *
  353. * @api
  354. */
  355. public function setTags(array $tags)
  356. {
  357. $this->tags = $tags;
  358. return $this;
  359. }
  360. /**
  361. * Returns all tags.
  362. *
  363. * @return array An array of tags
  364. *
  365. * @api
  366. */
  367. public function getTags()
  368. {
  369. return $this->tags;
  370. }
  371. /**
  372. * Gets a tag by name.
  373. *
  374. * @param string $name The tag name
  375. *
  376. * @return array An array of attributes
  377. *
  378. * @api
  379. */
  380. public function getTag($name)
  381. {
  382. return isset($this->tags[$name]) ? $this->tags[$name] : array();
  383. }
  384. /**
  385. * Adds a tag for this definition.
  386. *
  387. * @param string $name The tag name
  388. * @param array $attributes An array of attributes
  389. *
  390. * @return Definition The current instance
  391. *
  392. * @api
  393. */
  394. public function addTag($name, array $attributes = array())
  395. {
  396. $this->tags[$name][] = $attributes;
  397. return $this;
  398. }
  399. /**
  400. * Whether this definition has a tag with the given name
  401. *
  402. * @param string $name
  403. *
  404. * @return Boolean
  405. *
  406. * @api
  407. */
  408. public function hasTag($name)
  409. {
  410. return isset($this->tags[$name]);
  411. }
  412. /**
  413. * Clears all tags for a given name.
  414. *
  415. * @param string $name The tag name
  416. *
  417. * @return Definition
  418. */
  419. public function clearTag($name)
  420. {
  421. if (isset($this->tags[$name])) {
  422. unset($this->tags[$name]);
  423. }
  424. return $this;
  425. }
  426. /**
  427. * Clears the tags for this definition.
  428. *
  429. * @return Definition The current instance
  430. *
  431. * @api
  432. */
  433. public function clearTags()
  434. {
  435. $this->tags = array();
  436. return $this;
  437. }
  438. /**
  439. * Sets a file to require before creating the service.
  440. *
  441. * @param string $file A full pathname to include
  442. *
  443. * @return Definition The current instance
  444. *
  445. * @api
  446. */
  447. public function setFile($file)
  448. {
  449. $this->file = $file;
  450. return $this;
  451. }
  452. /**
  453. * Gets the file to require before creating the service.
  454. *
  455. * @return string The full pathname to include
  456. *
  457. * @api
  458. */
  459. public function getFile()
  460. {
  461. return $this->file;
  462. }
  463. /**
  464. * Sets the scope of the service
  465. *
  466. * @param string $scope Whether the service must be shared or not
  467. *
  468. * @return Definition The current instance
  469. *
  470. * @api
  471. */
  472. public function setScope($scope)
  473. {
  474. $this->scope = $scope;
  475. return $this;
  476. }
  477. /**
  478. * Returns the scope of the service
  479. *
  480. * @return string
  481. *
  482. * @api
  483. */
  484. public function getScope()
  485. {
  486. return $this->scope;
  487. }
  488. /**
  489. * Sets the visibility of this service.
  490. *
  491. * @param Boolean $boolean
  492. *
  493. * @return Definition The current instance
  494. *
  495. * @api
  496. */
  497. public function setPublic($boolean)
  498. {
  499. $this->public = (Boolean) $boolean;
  500. return $this;
  501. }
  502. /**
  503. * Whether this service is public facing
  504. *
  505. * @return Boolean
  506. *
  507. * @api
  508. */
  509. public function isPublic()
  510. {
  511. return $this->public;
  512. }
  513. /**
  514. * Sets the synchronized flag of this service.
  515. *
  516. * @param Boolean $boolean
  517. *
  518. * @return Definition The current instance
  519. *
  520. * @api
  521. */
  522. public function setSynchronized($boolean)
  523. {
  524. $this->synchronized = (Boolean) $boolean;
  525. return $this;
  526. }
  527. /**
  528. * Whether this service is synchronized.
  529. *
  530. * @return Boolean
  531. *
  532. * @api
  533. */
  534. public function isSynchronized()
  535. {
  536. return $this->synchronized;
  537. }
  538. /**
  539. * Sets the lazy flag of this service.
  540. *
  541. * @param Boolean $lazy
  542. *
  543. * @return Definition The current instance
  544. */
  545. public function setLazy($lazy)
  546. {
  547. $this->lazy = (Boolean) $lazy;
  548. return $this;
  549. }
  550. /**
  551. * Whether this service is lazy.
  552. *
  553. * @return Boolean
  554. */
  555. public function isLazy()
  556. {
  557. return $this->lazy;
  558. }
  559. /**
  560. * Sets whether this definition is synthetic, that is not constructed by the
  561. * container, but dynamically injected.
  562. *
  563. * @param Boolean $boolean
  564. *
  565. * @return Definition the current instance
  566. *
  567. * @api
  568. */
  569. public function setSynthetic($boolean)
  570. {
  571. $this->synthetic = (Boolean) $boolean;
  572. return $this;
  573. }
  574. /**
  575. * Whether this definition is synthetic, that is not constructed by the
  576. * container, but dynamically injected.
  577. *
  578. * @return Boolean
  579. *
  580. * @api
  581. */
  582. public function isSynthetic()
  583. {
  584. return $this->synthetic;
  585. }
  586. /**
  587. * Whether this definition is abstract, that means it merely serves as a
  588. * template for other definitions.
  589. *
  590. * @param Boolean $boolean
  591. *
  592. * @return Definition the current instance
  593. *
  594. * @api
  595. */
  596. public function setAbstract($boolean)
  597. {
  598. $this->abstract = (Boolean) $boolean;
  599. return $this;
  600. }
  601. /**
  602. * Whether this definition is abstract, that means it merely serves as a
  603. * template for other definitions.
  604. *
  605. * @return Boolean
  606. *
  607. * @api
  608. */
  609. public function isAbstract()
  610. {
  611. return $this->abstract;
  612. }
  613. /**
  614. * Sets a configurator to call after the service is fully initialized.
  615. *
  616. * @param callable $callable A PHP callable
  617. *
  618. * @return Definition The current instance
  619. *
  620. * @api
  621. */
  622. public function setConfigurator($callable)
  623. {
  624. $this->configurator = $callable;
  625. return $this;
  626. }
  627. /**
  628. * Gets the configurator to call after the service is fully initialized.
  629. *
  630. * @return callable The PHP callable to call
  631. *
  632. * @api
  633. */
  634. public function getConfigurator()
  635. {
  636. return $this->configurator;
  637. }
  638. }