PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Definition.php

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