InterfaceInjector.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * InterfaceInjector is used for Interface Injection.
  14. *
  15. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  16. */
  17. class InterfaceInjector
  18. {
  19. private $class;
  20. private $calls = array();
  21. private $processedDefinitions = array();
  22. /**
  23. * Constructs interface injector by specifying the target class name
  24. *
  25. * @param string $class
  26. */
  27. public function __construct($class)
  28. {
  29. $this->class = $class;
  30. }
  31. /**
  32. * Returns the interface name
  33. *
  34. * @return string
  35. */
  36. public function getClass()
  37. {
  38. return $this->class;
  39. }
  40. /**
  41. * Sets the interface class
  42. *
  43. * @param string $class
  44. * @return void
  45. */
  46. public function setClass($class)
  47. {
  48. $this->class = $class;
  49. }
  50. /**
  51. * Adds method calls if Definition is of required interface
  52. *
  53. * @param Definition $definition
  54. * @param string $class
  55. * @return void
  56. */
  57. public function processDefinition(Definition $definition, $class = null)
  58. {
  59. if (in_array($definition, $this->processedDefinitions, true)) {
  60. return;
  61. }
  62. $class = $class ?: $definition->getClass();
  63. if (!$this->supports($class)) {
  64. return;
  65. }
  66. foreach ($this->calls as $callback) {
  67. list($method, $arguments) = $callback;
  68. $definition->addMethodCall($method, $arguments);
  69. }
  70. $this->processedDefinitions[] = $definition;
  71. }
  72. /**
  73. * Inspects if current interface injector is to be used with a given class
  74. *
  75. * @param string $object
  76. * @return Boolean
  77. */
  78. public function supports($object)
  79. {
  80. if (is_string($object)) {
  81. if (!class_exists($object)) {
  82. return false;
  83. }
  84. $reflection = new \ReflectionClass($object);
  85. return $reflection->isSubClassOf($this->class)
  86. || $object === $this->class;
  87. }
  88. if ( ! is_object($object)) {
  89. throw new InvalidArgumentException(sprintf("%s expects class or object, %s given", __METHOD__, substr(str_replace("\n", '', var_export($object, true)), 0, 10)));
  90. }
  91. return is_a($object, $this->class);
  92. }
  93. /**
  94. * Adds a method to call to be injected on any service implementing the interface.
  95. *
  96. * @param string $method The method name to call
  97. * @param array $arguments An array of arguments to pass to the method call
  98. *
  99. * @return InterfaceInjector The current instance
  100. */
  101. public function addMethodCall($method, array $arguments = array())
  102. {
  103. $this->calls[] = array($method, $arguments);
  104. return $this;
  105. }
  106. /**
  107. * Removes a method to call after service initialization.
  108. *
  109. * @param string $method The method name to remove
  110. *
  111. * @return Definition The current instance
  112. */
  113. public function removeMethodCall($method)
  114. {
  115. foreach ($this->calls as $i => $call) {
  116. if ($call[0] === $method) {
  117. unset($this->calls[$i]);
  118. break;
  119. }
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Check if the current definition has a given method to call after service initialization.
  125. *
  126. * @param string $method The method name to search for
  127. *
  128. * @return Boolean
  129. */
  130. public function hasMethodCall($method)
  131. {
  132. foreach ($this->calls as $call) {
  133. if ($call[0] === $method) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. /**
  140. * Gets the methods to call after service initialization.
  141. *
  142. * @return array An array of method calls
  143. */
  144. public function getMethodCalls()
  145. {
  146. return $this->calls;
  147. }
  148. /**
  149. * Merges another InterfaceInjector
  150. *
  151. * @param InterfaceInjector $injector
  152. */
  153. public function merge(InterfaceInjector $injector)
  154. {
  155. if ($this->class === $injector->getClass()) {
  156. foreach ($injector->getMethodCalls() as $call) {
  157. list ($method, $arguments) = $call;
  158. $this->addMethodCall($method, $arguments);
  159. }
  160. }
  161. }
  162. }