Definition.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 dependency_injection
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class Definition
  19. {
  20. protected
  21. $class = null,
  22. $file = null,
  23. $constructor = null,
  24. $shared = true,
  25. $arguments = array(),
  26. $calls = array(),
  27. $configurator = null;
  28. /**
  29. * Constructor.
  30. *
  31. * @param string $class The service class
  32. * @param array $arguments An array of arguments to pass to the service constructor
  33. */
  34. public function __construct($class, array $arguments = array())
  35. {
  36. $this->class = $class;
  37. $this->arguments = $arguments;
  38. }
  39. /**
  40. * Sets the constructor method.
  41. *
  42. * @param string $method The method name
  43. *
  44. * @return Definition The current instance
  45. */
  46. public function setConstructor($method)
  47. {
  48. $this->constructor = $method;
  49. return $this;
  50. }
  51. /**
  52. * Gets the constructor method.
  53. *
  54. * @return Definition The constructor method name
  55. */
  56. public function getConstructor()
  57. {
  58. return $this->constructor;
  59. }
  60. /**
  61. * Sets the service class.
  62. *
  63. * @param string $class The service class
  64. *
  65. * @return Definition The current instance
  66. */
  67. public function setClass($class)
  68. {
  69. $this->class = $class;
  70. return $this;
  71. }
  72. /**
  73. * Sets the constructor method.
  74. *
  75. * @return string The service class
  76. */
  77. public function getClass()
  78. {
  79. return $this->class;
  80. }
  81. /**
  82. * Sets the constructor arguments to pass to the service constructor.
  83. *
  84. * @param array $arguments An array of arguments
  85. *
  86. * @return Definition The current instance
  87. */
  88. public function setArguments(array $arguments)
  89. {
  90. $this->arguments = $arguments;
  91. return $this;
  92. }
  93. /**
  94. * Adds a constructor argument to pass to the service constructor.
  95. *
  96. * @param mixed $argument An argument
  97. *
  98. * @return Definition The current instance
  99. */
  100. public function addArgument($argument)
  101. {
  102. $this->arguments[] = $argument;
  103. return $this;
  104. }
  105. /**
  106. * Gets the constructor arguments to pass to the service constructor.
  107. *
  108. * @return array The array of arguments
  109. */
  110. public function getArguments()
  111. {
  112. return $this->arguments;
  113. }
  114. /**
  115. * Sets the methods to call after service initialization.
  116. *
  117. * @param array $calls An array of method calls
  118. *
  119. * @return Definition The current instance
  120. */
  121. public function setMethodCalls(array $calls = array())
  122. {
  123. $this->calls = array();
  124. foreach ($calls as $call)
  125. {
  126. $this->addMethodCall($call[0], $call[1]);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Adds a method to call after service initialization.
  132. *
  133. * @param string $method The method name to call
  134. * @param array $arguments An array of arguments to pass to the method call
  135. *
  136. * @return Definition The current instance
  137. */
  138. public function addMethodCall($method, array $arguments = array())
  139. {
  140. $this->calls[] = array($method, $arguments);
  141. return $this;
  142. }
  143. /**
  144. * Gets the methods to call after service initialization.
  145. *
  146. * @return array An array of method calls
  147. */
  148. public function getMethodCalls()
  149. {
  150. return $this->calls;
  151. }
  152. /**
  153. * Sets a file to require before creating the service.
  154. *
  155. * @param string $file A full pathname to include
  156. *
  157. * @return Definition The current instance
  158. */
  159. public function setFile($file)
  160. {
  161. $this->file = $file;
  162. return $this;
  163. }
  164. /**
  165. * Gets the file to require before creating the service.
  166. *
  167. * @return string The full pathname to include
  168. */
  169. public function getFile()
  170. {
  171. return $this->file;
  172. }
  173. /**
  174. * Sets if the service must be shared or not.
  175. *
  176. * @param Boolean $shared Whether the service must be shared or not
  177. *
  178. * @return Definition The current instance
  179. */
  180. public function setShared($shared)
  181. {
  182. $this->shared = (Boolean) $shared;
  183. return $this;
  184. }
  185. /**
  186. * Returns true if the service must be shared.
  187. *
  188. * @return Boolean true if the service is shared, false otherwise
  189. */
  190. public function isShared()
  191. {
  192. return $this->shared;
  193. }
  194. /**
  195. * Sets a configurator to call after the service is fully initialized.
  196. *
  197. * @param mixed $callable A PHP callable
  198. *
  199. * @return Definition The current instance
  200. */
  201. public function setConfigurator($callable)
  202. {
  203. $this->configurator = $callable;
  204. return $this;
  205. }
  206. /**
  207. * Gets the configurator to call after the service is fully initialized.
  208. *
  209. * @return mixed The PHP callable to call
  210. */
  211. public function getConfigurator()
  212. {
  213. return $this->configurator;
  214. }
  215. }