Definition.php 13KB

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