Definition.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. * @api
  216. */
  217. public function replaceArgument($index, $argument)
  218. {
  219. if ($index < 0 || $index > count($this->arguments) - 1) {
  220. throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
  221. }
  222. $this->arguments[$index] = $argument;
  223. return $this;
  224. }
  225. /**
  226. * Gets the arguments to pass to the service constructor/factory method.
  227. *
  228. * @return array The array of arguments
  229. *
  230. * @api
  231. */
  232. public function getArguments()
  233. {
  234. return $this->arguments;
  235. }
  236. /**
  237. * Gets an argument to pass to the service constructor/factory method.
  238. *
  239. * @param integer $index
  240. *
  241. * @return mixed The argument value
  242. *
  243. * @api
  244. */
  245. public function getArgument($index)
  246. {
  247. if ($index < 0 || $index > count($this->arguments) - 1) {
  248. throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
  249. }
  250. return $this->arguments[$index];
  251. }
  252. /**
  253. * Sets the methods to call after service initialization.
  254. *
  255. * @param array $calls An array of method calls
  256. *
  257. * @return Definition The current instance
  258. *
  259. * @api
  260. */
  261. public function setMethodCalls(array $calls = array())
  262. {
  263. $this->calls = array();
  264. foreach ($calls as $call) {
  265. $this->addMethodCall($call[0], $call[1]);
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Adds a method to call after service initialization.
  271. *
  272. * @param string $method The method name to call
  273. * @param array $arguments An array of arguments to pass to the method call
  274. *
  275. * @return Definition The current instance
  276. *
  277. * @throws InvalidArgumentException on empty $method param
  278. *
  279. * @api
  280. */
  281. public function addMethodCall($method, array $arguments = array())
  282. {
  283. if (empty($method)) {
  284. throw new InvalidArgumentException(sprintf('Method name cannot be empty.'));
  285. }
  286. $this->calls[] = array($method, $arguments);
  287. return $this;
  288. }
  289. /**
  290. * Removes a method to call after service initialization.
  291. *
  292. * @param string $method The method name to remove
  293. *
  294. * @return Definition The current instance
  295. *
  296. * @api
  297. */
  298. public function removeMethodCall($method)
  299. {
  300. foreach ($this->calls as $i => $call) {
  301. if ($call[0] === $method) {
  302. unset($this->calls[$i]);
  303. break;
  304. }
  305. }
  306. return $this;
  307. }
  308. /**
  309. * Check if the current definition has a given method to call after service initialization.
  310. *
  311. * @param string $method The method name to search for
  312. *
  313. * @return Boolean
  314. *
  315. * @api
  316. */
  317. public function hasMethodCall($method)
  318. {
  319. foreach ($this->calls as $call) {
  320. if ($call[0] === $method) {
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. /**
  327. * Gets the methods to call after service initialization.
  328. *
  329. * @return array An array of method calls
  330. *
  331. * @api
  332. */
  333. public function getMethodCalls()
  334. {
  335. return $this->calls;
  336. }
  337. /**
  338. * Sets tags for this definition
  339. *
  340. * @param array $tags
  341. *
  342. * @return Definition the current instance
  343. *
  344. * @api
  345. */
  346. public function setTags(array $tags)
  347. {
  348. $this->tags = $tags;
  349. return $this;
  350. }
  351. /**
  352. * Returns all tags.
  353. *
  354. * @return array An array of tags
  355. *
  356. * @api
  357. */
  358. public function getTags()
  359. {
  360. return $this->tags;
  361. }
  362. /**
  363. * Gets a tag by name.
  364. *
  365. * @param string $name The tag name
  366. *
  367. * @return array An array of attributes
  368. *
  369. * @api
  370. */
  371. public function getTag($name)
  372. {
  373. return isset($this->tags[$name]) ? $this->tags[$name] : array();
  374. }
  375. /**
  376. * Adds a tag for this definition.
  377. *
  378. * @param string $name The tag name
  379. * @param array $attributes An array of attributes
  380. *
  381. * @return Definition The current instance
  382. *
  383. * @api
  384. */
  385. public function addTag($name, array $attributes = array())
  386. {
  387. $this->tags[$name][] = $attributes;
  388. return $this;
  389. }
  390. /**
  391. * Whether this definition has a tag with the given name
  392. *
  393. * @param string $name
  394. *
  395. * @return Boolean
  396. *
  397. * @api
  398. */
  399. public function hasTag($name)
  400. {
  401. return isset($this->tags[$name]);
  402. }
  403. /**
  404. * Clears the tags for this definition.
  405. *
  406. * @return Definition The current instance
  407. *
  408. * @api
  409. */
  410. public function clearTags()
  411. {
  412. $this->tags = array();
  413. return $this;
  414. }
  415. /**
  416. * Sets a file to require before creating the service.
  417. *
  418. * @param string $file A full pathname to include
  419. *
  420. * @return Definition The current instance
  421. *
  422. * @api
  423. */
  424. public function setFile($file)
  425. {
  426. $this->file = $file;
  427. return $this;
  428. }
  429. /**
  430. * Gets the file to require before creating the service.
  431. *
  432. * @return string The full pathname to include
  433. *
  434. * @api
  435. */
  436. public function getFile()
  437. {
  438. return $this->file;
  439. }
  440. /**
  441. * Sets the scope of the service
  442. *
  443. * @param string $scope Whether the service must be shared or not
  444. *
  445. * @return Definition The current instance
  446. *
  447. * @api
  448. */
  449. public function setScope($scope)
  450. {
  451. $this->scope = $scope;
  452. return $this;
  453. }
  454. /**
  455. * Returns the scope of the service
  456. *
  457. * @return string
  458. *
  459. * @api
  460. */
  461. public function getScope()
  462. {
  463. return $this->scope;
  464. }
  465. /**
  466. * Sets the visibility of this service.
  467. *
  468. * @param Boolean $boolean
  469. * @return Definition The current instance
  470. *
  471. * @api
  472. */
  473. public function setPublic($boolean)
  474. {
  475. $this->public = (Boolean) $boolean;
  476. return $this;
  477. }
  478. /**
  479. * Whether this service is public facing
  480. *
  481. * @return Boolean
  482. *
  483. * @api
  484. */
  485. public function isPublic()
  486. {
  487. return $this->public;
  488. }
  489. /**
  490. * Sets whether this definition is synthetic, that is not constructed by the
  491. * container, but dynamically injected.
  492. *
  493. * @param Boolean $boolean
  494. *
  495. * @return Definition the current instance
  496. *
  497. * @api
  498. */
  499. public function setSynthetic($boolean)
  500. {
  501. $this->synthetic = (Boolean) $boolean;
  502. return $this;
  503. }
  504. /**
  505. * Whether this definition is synthetic, that is not constructed by the
  506. * container, but dynamically injected.
  507. *
  508. * @return Boolean
  509. *
  510. * @api
  511. */
  512. public function isSynthetic()
  513. {
  514. return $this->synthetic;
  515. }
  516. /**
  517. * Whether this definition is abstract, that means it merely serves as a
  518. * template for other definitions.
  519. *
  520. * @param Boolean $boolean
  521. *
  522. * @return Definition the current instance
  523. *
  524. * @api
  525. */
  526. public function setAbstract($boolean)
  527. {
  528. $this->abstract = (Boolean) $boolean;
  529. return $this;
  530. }
  531. /**
  532. * Whether this definition is abstract, that means it merely serves as a
  533. * template for other definitions.
  534. *
  535. * @return Boolean
  536. *
  537. * @api
  538. */
  539. public function isAbstract()
  540. {
  541. return $this->abstract;
  542. }
  543. /**
  544. * Sets a configurator to call after the service is fully initialized.
  545. *
  546. * @param mixed $callable A PHP callable
  547. *
  548. * @return Definition The current instance
  549. *
  550. * @api
  551. */
  552. public function setConfigurator($callable)
  553. {
  554. $this->configurator = $callable;
  555. return $this;
  556. }
  557. /**
  558. * Gets the configurator to call after the service is fully initialized.
  559. *
  560. * @return mixed The PHP callable to call
  561. *
  562. * @api
  563. */
  564. public function getConfigurator()
  565. {
  566. return $this->configurator;
  567. }
  568. }