InterfaceInjector.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if (!$definition->hasMethodCall($method)) {
  69. $definition->addMethodCall($method, $arguments);
  70. }
  71. }
  72. $this->processedDefinitions[] = $definition;
  73. }
  74. /**
  75. * Inspects if current interface injector is to be used with a given class
  76. *
  77. * @param string $object
  78. * @return Boolean
  79. */
  80. public function supports($object)
  81. {
  82. if (is_string($object)) {
  83. if (!class_exists($object)) {
  84. return false;
  85. }
  86. $reflection = new \ReflectionClass($object);
  87. return $reflection->isSubClassOf($this->class)
  88. || $object === $this->class;
  89. }
  90. if ( ! is_object($object)) {
  91. throw new InvalidArgumentException(sprintf("%s expects class or object, %s given", __METHOD__, substr(str_replace("\n", '', var_export($object, true)), 0, 10)));
  92. }
  93. return is_a($object, $this->class);
  94. }
  95. /**
  96. * Adds a method to call to be injected on any service implementing the interface.
  97. *
  98. * @param string $method The method name to call
  99. * @param array $arguments An array of arguments to pass to the method call
  100. *
  101. * @return InterfaceInjector The current instance
  102. */
  103. public function addMethodCall($method, array $arguments = array())
  104. {
  105. $this->calls[] = array($method, $arguments);
  106. return $this;
  107. }
  108. /**
  109. * Removes a method to call after service initialization.
  110. *
  111. * @param string $method The method name to remove
  112. *
  113. * @return Definition The current instance
  114. */
  115. public function removeMethodCall($method)
  116. {
  117. foreach ($this->calls as $i => $call) {
  118. if ($call[0] === $method) {
  119. unset($this->calls[$i]);
  120. break;
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Check if the current definition has a given method to call after service initialization.
  127. *
  128. * @param string $method The method name to search for
  129. *
  130. * @return Boolean
  131. */
  132. public function hasMethodCall($method)
  133. {
  134. foreach ($this->calls as $call) {
  135. if ($call[0] === $method) {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. /**
  142. * Gets the methods to call after service initialization.
  143. *
  144. * @return array An array of method calls
  145. */
  146. public function getMethodCalls()
  147. {
  148. return $this->calls;
  149. }
  150. /**
  151. * Merges another InterfaceInjector
  152. *
  153. * @param InterfaceInjector $injector
  154. */
  155. public function merge(InterfaceInjector $injector)
  156. {
  157. if ($this->class === $injector->getClass()) {
  158. foreach ($injector->getMethodCalls() as $call) {
  159. list ($method, $arguments) = $call;
  160. $this->addMethodCall($method, $arguments);
  161. }
  162. }
  163. }
  164. }