PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Object/Configuration/Configuration.php

https://github.com/christianjul/FLOW3-Composer
PHP | 394 lines | 139 code | 46 blank | 209 comment | 11 complexity | 8a94fe744617bfe298a7da940c17cb4a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Object\Configuration;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use Doctrine\ORM\Mapping as ORM;
  13. use TYPO3\FLOW3\Annotations as FLOW3;
  14. /**
  15. * FLOW3 Object Configuration
  16. *
  17. * @FLOW3\Proxy(false)
  18. */
  19. class Configuration {
  20. const AUTOWIRING_MODE_OFF = 0;
  21. const AUTOWIRING_MODE_ON = 1;
  22. const SCOPE_PROTOTYPE = 1;
  23. const SCOPE_SINGLETON = 2;
  24. const SCOPE_SESSION = 3;
  25. /**
  26. * Name of the object
  27. * @var string $objectName
  28. */
  29. protected $objectName;
  30. /**
  31. * Name of the class the object is based on
  32. * @var string $className
  33. */
  34. protected $className;
  35. /**
  36. * Key of the package the specified object is part of
  37. * @var string
  38. */
  39. protected $packageKey;
  40. /**
  41. * If set, specifies the factory class used to create this object
  42. * @var string
  43. */
  44. protected $factoryObjectName = '';
  45. /**
  46. * Name of the factory method. Only used if $factoryObjectName is set.
  47. * @var string
  48. */
  49. protected $factoryMethodName = 'create';
  50. /**
  51. * @var string
  52. */
  53. protected $scope = self::SCOPE_PROTOTYPE;
  54. /**
  55. * Arguments of the constructor detected by reflection
  56. * @var array
  57. */
  58. protected $arguments = array();
  59. /**
  60. * Array of properties which are injected into the object
  61. * @var array
  62. */
  63. protected $properties = array();
  64. /**
  65. * Mode of the autowiring feature. One of the AUTOWIRING_MODE_* constants
  66. * @var integer
  67. */
  68. protected $autowiring = self::AUTOWIRING_MODE_ON;
  69. /**
  70. * Name of the method to call during the initialization of the object (after dependencies are injected)
  71. * @var string
  72. */
  73. protected $lifecycleInitializationMethodName = 'initializeObject';
  74. /**
  75. * Name of the method to call during the shutdown of the framework
  76. * @var string
  77. */
  78. protected $lifecycleShutdownMethodName = 'shutdownObject';
  79. /**
  80. * Information about where this configuration has been created. Used in error messages to make debugging easier.
  81. * @var string
  82. */
  83. protected $configurationSourceHint = '< unknown >';
  84. /**
  85. * The constructor
  86. *
  87. * @param string $objectName The unique identifier of the object
  88. * @param string $className Name of the class which provides the functionality of this object
  89. */
  90. public function __construct($objectName, $className = NULL) {
  91. $backtrace = debug_backtrace();
  92. if (isset($backtrace[1]['object'])) {
  93. $this->configurationSourceHint = get_class($backtrace[1]['object']);
  94. } elseif (isset($backtrace[1]['class'])) {
  95. $this->configurationSourceHint = $backtrace[1]['class'];
  96. }
  97. $this->objectName = $objectName;
  98. $this->className = ($className === NULL ? $objectName : $className);
  99. }
  100. /**
  101. * Returns the object name
  102. *
  103. * @return string object name
  104. */
  105. public function getObjectName() {
  106. return $this->objectName;
  107. }
  108. /**
  109. * Setter function for property "className"
  110. *
  111. * @param string $className Name of the class which provides the functionality for this object
  112. * @return void
  113. */
  114. public function setClassName($className) {
  115. $this->className = $className;
  116. }
  117. /**
  118. * Returns the class name
  119. *
  120. * @return string Name of the implementing class of this object
  121. */
  122. public function getClassName() {
  123. return $this->className;
  124. }
  125. /**
  126. * Sets the package key
  127. *
  128. * @param string $packageKey Key of the package this object is part of
  129. * @return void
  130. */
  131. public function setPackageKey($packageKey) {
  132. $this->packageKey = $packageKey;
  133. }
  134. /**
  135. * Returns the package key
  136. *
  137. * @return string Key of the package this object is part of
  138. */
  139. public function getPackageKey() {
  140. return $this->packageKey;
  141. }
  142. /**
  143. * Sets the class name of a factory which is in charge of instantiating this object
  144. *
  145. * @param string $className Valid class name of a factory
  146. * @return void
  147. * @throws \TYPO3\FLOW3\Object\Exception\InvalidClassException
  148. */
  149. public function setFactoryObjectName($className) {
  150. if (!class_exists($className, TRUE)) {
  151. throw new \TYPO3\FLOW3\Object\Exception\InvalidClassException('"' . $className . '" is not a valid class name or a class of that name does not exist.', 1229697796);
  152. }
  153. $this->factoryObjectName= $className;
  154. }
  155. /**
  156. * Returns the class name of the factory for this object, if any
  157. *
  158. * @return string The factory class name
  159. */
  160. public function getFactoryObjectName() {
  161. return $this->factoryObjectName;
  162. }
  163. /**
  164. * Sets the name of the factory method
  165. *
  166. * @param string $methodName The factory method name
  167. * @return void
  168. * @throws \InvalidArgumentException
  169. */
  170. public function setFactoryMethodName($methodName) {
  171. if (!is_string($methodName) || $methodName === '') {
  172. throw new \InvalidArgumentException('No valid factory method name specified.', 1229700126);
  173. }
  174. $this->factoryMethodName = $methodName;
  175. }
  176. /**
  177. * Returns the factory method name
  178. *
  179. * @return string The factory method name
  180. */
  181. public function getFactoryMethodName() {
  182. return $this->factoryMethodName;
  183. }
  184. /**
  185. * Setter function for property "scope"
  186. *
  187. * @param integer $scope Name of the scope
  188. * @return void
  189. */
  190. public function setScope($scope) {
  191. $this->scope = $scope;
  192. }
  193. /**
  194. * Returns the scope for this object
  195. *
  196. * @return string The scope, one of the SCOPE constants
  197. */
  198. public function getScope() {
  199. return $this->scope;
  200. }
  201. /**
  202. * Setter function for property "autowiring"
  203. *
  204. * @param integer $autowiring One of the AUTOWIRING_MODE_* constants
  205. * @return void
  206. */
  207. public function setAutowiring($autowiring) {
  208. $this->autowiring = $autowiring;
  209. }
  210. /**
  211. * Returns the autowiring mode for the configured object
  212. *
  213. * @return integer Value of one of the AUTOWIRING_MODE_* constants
  214. */
  215. public function getAutowiring() {
  216. return $this->autowiring;
  217. }
  218. /**
  219. * Setter function for property "lifecycleInitializationMethodName"
  220. *
  221. * @param string $lifecycleInitializationMethodName Name of the method to call after setter injection
  222. * @return void
  223. */
  224. public function setLifecycleInitializationMethodName($lifecycleInitializationMethodName) {
  225. $this->lifecycleInitializationMethodName = $lifecycleInitializationMethodName;
  226. }
  227. /**
  228. * Returns the name of the lifecycle initialization method for this object
  229. *
  230. * @return string The name of the initialization method
  231. */
  232. public function getLifecycleInitializationMethodName() {
  233. return $this->lifecycleInitializationMethodName;
  234. }
  235. /**
  236. * Setter function for property "lifecycleShutdownMethodName"
  237. *
  238. * @param string $lifecycleShutdownMethodName Name of the method to call during shutdown of the framework
  239. * @return void
  240. */
  241. public function setLifecycleShutdownMethodName($lifecycleShutdownMethodName) {
  242. $this->lifecycleShutdownMethodName = $lifecycleShutdownMethodName;
  243. }
  244. /**
  245. * Returns the name of the lifecycle shutdown method for this object
  246. *
  247. * @return string The name of the shutdown method
  248. */
  249. public function getLifecycleShutdownMethodName() {
  250. return $this->lifecycleShutdownMethodName;
  251. }
  252. /**
  253. * Setter function for injection properties. If an empty array is passed to this
  254. * method, all (possibly) defined properties are removed from the configuration.
  255. *
  256. * @param array $properties Array of \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty
  257. * @return void
  258. */
  259. public function setProperties(array $properties) {
  260. if ($properties === array()) {
  261. $this->properties = array();
  262. } else {
  263. foreach ($properties as $value) {
  264. $this->setProperty($value);
  265. }
  266. }
  267. }
  268. /**
  269. * Returns the currently set injection properties of the object
  270. *
  271. * @return array Array of \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty
  272. */
  273. public function getProperties() {
  274. return $this->properties;
  275. }
  276. /**
  277. * Setter function for a single injection property
  278. *
  279. * @param \TYPO3\FLOW3\Object\Configuration\ConfigurationProperty $property
  280. * @return void
  281. */
  282. public function setProperty(\TYPO3\FLOW3\Object\Configuration\ConfigurationProperty $property) {
  283. $this->properties[$property->getName()] = $property;
  284. }
  285. /**
  286. * Setter function for injection constructor arguments. If an empty array is passed to this
  287. * method, all (possibly) defined constructor arguments are removed from the configuration.
  288. *
  289. * @param array $arguments Array of \TYPO3\FLOW3\Object\Configuration\ConfigurationArgument
  290. * @return void
  291. */
  292. public function setArguments(array $arguments) {
  293. if ($arguments === array()) {
  294. $this->arguments = array();
  295. } else {
  296. foreach ($arguments as $argument) {
  297. if ($argument !== NULL) {
  298. $this->setArgument($argument);
  299. }
  300. }
  301. }
  302. }
  303. /**
  304. * Setter function for a single constructor argument
  305. *
  306. * @param \TYPO3\FLOW3\Object\Configuration\ConfigurationArgument $argument The argument
  307. * @return void
  308. */
  309. public function setArgument(\TYPO3\FLOW3\Object\Configuration\ConfigurationArgument $argument) {
  310. $this->arguments[$argument->getIndex()] = $argument;
  311. }
  312. /**
  313. * Returns a sorted array of constructor arguments indexed by position (starting with "1")
  314. *
  315. * @return array A sorted array of \TYPO3\FLOW3\Object\Configuration\ConfigurationArgument objects with the argument position as index
  316. */
  317. public function getArguments() {
  318. if (count($this->arguments) < 1 ) {
  319. return array();
  320. }
  321. asort($this->arguments);
  322. $lastArgument = end($this->arguments);
  323. $argumentsCount = $lastArgument->getIndex();
  324. $sortedArguments = array();
  325. for ($index = 1; $index <= $argumentsCount; $index++) {
  326. $sortedArguments[$index] = isset($this->arguments[$index]) ? $this->arguments[$index] : NULL;
  327. }
  328. return $sortedArguments;
  329. }
  330. /**
  331. * Sets some information (hint) about where this configuration has been created.
  332. *
  333. * @param string $hint The hint - e.g. the filename of the configuration file
  334. * @return void
  335. */
  336. public function setConfigurationSourceHint($hint) {
  337. $this->configurationSourceHint = $hint;
  338. }
  339. /**
  340. * Returns some information (if any) about where this configuration has been created.
  341. *
  342. * @return string The hint - e.g. the filename of the configuration file
  343. */
  344. public function getConfigurationSourceHint() {
  345. return $this->configurationSourceHint;
  346. }
  347. }
  348. ?>