Definition.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace Symfony\Components\DependencyInjection;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Definition represents a service definition.
  13. *
  14. * @package Symfony
  15. * @subpackage Components_DependencyInjection
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class Definition
  19. {
  20. protected $class;
  21. protected $file;
  22. protected $factoryMethod;
  23. protected $factoryService;
  24. protected $shared;
  25. protected $arguments;
  26. protected $calls;
  27. protected $configurator;
  28. protected $annotations;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $class The service class
  33. * @param array $arguments An array of arguments to pass to the service constructor
  34. */
  35. public function __construct($class = null, array $arguments = array())
  36. {
  37. $this->class = $class;
  38. $this->arguments = $arguments;
  39. $this->calls = array();
  40. $this->shared = true;
  41. $this->annotations = array();
  42. }
  43. /**
  44. * Sets the factory method able to create an instance of this class.
  45. *
  46. * @param string $method The method name
  47. *
  48. * @return Definition The current instance
  49. */
  50. public function setFactoryMethod($method)
  51. {
  52. $this->factoryMethod = $method;
  53. return $this;
  54. }
  55. /**
  56. * Gets the factory method.
  57. *
  58. * @return string The factory method name
  59. */
  60. public function getFactoryMethod()
  61. {
  62. return $this->factoryMethod;
  63. }
  64. /**
  65. * Sets the name of the service that acts as a factory using the constructor method.
  66. *
  67. * @param string $factoryService The factory service id
  68. *
  69. * @return Definition The current instance
  70. */
  71. public function setFactoryService($factoryService)
  72. {
  73. $this->factoryService = $factoryService;
  74. return $this;
  75. }
  76. /**
  77. * Gets the factory service id.
  78. *
  79. * @return string The factory service id
  80. */
  81. public function getFactoryService()
  82. {
  83. return $this->factoryService;
  84. }
  85. /**
  86. * Sets the service class.
  87. *
  88. * @param string $class The service class
  89. *
  90. * @return Definition The current instance
  91. */
  92. public function setClass($class)
  93. {
  94. $this->class = $class;
  95. return $this;
  96. }
  97. /**
  98. * Sets the service class.
  99. *
  100. * @return string The service class
  101. */
  102. public function getClass()
  103. {
  104. return $this->class;
  105. }
  106. /**
  107. * Sets the arguments to pass to the service constructor/factory method.
  108. *
  109. * @param array $arguments An array of arguments
  110. *
  111. * @return Definition The current instance
  112. */
  113. public function setArguments(array $arguments)
  114. {
  115. $this->arguments = $arguments;
  116. return $this;
  117. }
  118. /**
  119. * Adds an argument to pass to the service constructor/factory method.
  120. *
  121. * @param mixed $argument An argument
  122. *
  123. * @return Definition The current instance
  124. */
  125. public function addArgument($argument)
  126. {
  127. $this->arguments[] = $argument;
  128. return $this;
  129. }
  130. /**
  131. * Gets the arguments to pass to the service constructor/factory method.
  132. *
  133. * @return array The array of arguments
  134. */
  135. public function getArguments()
  136. {
  137. return $this->arguments;
  138. }
  139. /**
  140. * Sets the methods to call after service initialization.
  141. *
  142. * @param array $calls An array of method calls
  143. *
  144. * @return Definition The current instance
  145. */
  146. public function setMethodCalls(array $calls = array())
  147. {
  148. $this->calls = array();
  149. foreach ($calls as $call) {
  150. $this->addMethodCall($call[0], $call[1]);
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Adds a method to call after service initialization.
  156. *
  157. * @param string $method The method name to call
  158. * @param array $arguments An array of arguments to pass to the method call
  159. *
  160. * @return Definition The current instance
  161. */
  162. public function addMethodCall($method, array $arguments = array())
  163. {
  164. $this->calls[] = array($method, $arguments);
  165. return $this;
  166. }
  167. /**
  168. * Gets the methods to call after service initialization.
  169. *
  170. * @return array An array of method calls
  171. */
  172. public function getMethodCalls()
  173. {
  174. return $this->calls;
  175. }
  176. /**
  177. * Returns all annotations.
  178. *
  179. * @return array An array of annotations
  180. */
  181. public function getAnnotations()
  182. {
  183. return $this->annotations;
  184. }
  185. /**
  186. * Gets an annotation by name.
  187. *
  188. * @param string $name The annotation name
  189. *
  190. * @return array An array of attributes
  191. */
  192. public function getAnnotation($name)
  193. {
  194. if (!isset($this->annotations[$name])) {
  195. $this->annotations[$name] = array();
  196. }
  197. return $this->annotations[$name];
  198. }
  199. /**
  200. * Adds an annotation for this definition.
  201. *
  202. * @param string $name The annotation name
  203. * @param array $attributes An array of attributes
  204. *
  205. * @return Definition The current instance
  206. */
  207. public function addAnnotation($name, array $attributes = array())
  208. {
  209. if (!isset($this->annotations[$name])) {
  210. $this->annotations[$name] = array();
  211. }
  212. $this->annotations[$name][] = $attributes;
  213. return $this;
  214. }
  215. /**
  216. * Clears the annotation for this definition.
  217. *
  218. * @return Definition The current instance
  219. */
  220. public function clearAnnotations()
  221. {
  222. $this->annotations = array();
  223. return $this;
  224. }
  225. /**
  226. * Sets a file to require before creating the service.
  227. *
  228. * @param string $file A full pathname to include
  229. *
  230. * @return Definition The current instance
  231. */
  232. public function setFile($file)
  233. {
  234. $this->file = $file;
  235. return $this;
  236. }
  237. /**
  238. * Gets the file to require before creating the service.
  239. *
  240. * @return string The full pathname to include
  241. */
  242. public function getFile()
  243. {
  244. return $this->file;
  245. }
  246. /**
  247. * Sets if the service must be shared or not.
  248. *
  249. * @param Boolean $shared Whether the service must be shared or not
  250. *
  251. * @return Definition The current instance
  252. */
  253. public function setShared($shared)
  254. {
  255. $this->shared = (Boolean) $shared;
  256. return $this;
  257. }
  258. /**
  259. * Returns true if the service must be shared.
  260. *
  261. * @return Boolean true if the service is shared, false otherwise
  262. */
  263. public function isShared()
  264. {
  265. return $this->shared;
  266. }
  267. /**
  268. * Sets a configurator to call after the service is fully initialized.
  269. *
  270. * @param mixed $callable A PHP callable
  271. *
  272. * @return Definition The current instance
  273. */
  274. public function setConfigurator($callable)
  275. {
  276. $this->configurator = $callable;
  277. return $this;
  278. }
  279. /**
  280. * Gets the configurator to call after the service is fully initialized.
  281. *
  282. * @return mixed The PHP callable to call
  283. */
  284. public function getConfigurator()
  285. {
  286. return $this->configurator;
  287. }
  288. }