Drupal investigation

AutowirePass.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Guesses constructor arguments of services definitions and try to instantiate services if necessary.
  17. *
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. class AutowirePass implements CompilerPassInterface
  21. {
  22. private $container;
  23. private $reflectionClasses = array();
  24. private $definedTypes = array();
  25. private $types;
  26. private $notGuessableTypes = array();
  27. private $usedTypes = array();
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. $throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
  34. spl_autoload_register($throwingAutoloader);
  35. try {
  36. $this->container = $container;
  37. foreach ($container->getDefinitions() as $id => $definition) {
  38. if ($definition->isAutowired()) {
  39. $this->completeDefinition($id, $definition);
  40. }
  41. }
  42. foreach ($this->usedTypes as $type => $id) {
  43. if (isset($this->usedTypes[$type]) && isset($this->notGuessableTypes[$type])) {
  44. $classOrInterface = class_exists($type) ? 'class' : 'interface';
  45. $matchingServices = implode(', ', $this->types[$type]);
  46. throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $type, $id, $classOrInterface, $matchingServices));
  47. }
  48. }
  49. } catch (\Exception $e) {
  50. } catch (\Throwable $e) {
  51. }
  52. spl_autoload_unregister($throwingAutoloader);
  53. // Free memory and remove circular reference to container
  54. $this->container = null;
  55. $this->reflectionClasses = array();
  56. $this->definedTypes = array();
  57. $this->types = null;
  58. $this->notGuessableTypes = array();
  59. $this->usedTypes = array();
  60. if (isset($e)) {
  61. throw $e;
  62. }
  63. }
  64. /**
  65. * Wires the given definition.
  66. *
  67. * @param string $id
  68. * @param Definition $definition
  69. *
  70. * @throws RuntimeException
  71. */
  72. private function completeDefinition($id, Definition $definition)
  73. {
  74. if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
  75. return;
  76. }
  77. $this->container->addClassResource($reflectionClass);
  78. if (!$constructor = $reflectionClass->getConstructor()) {
  79. return;
  80. }
  81. $arguments = $definition->getArguments();
  82. foreach ($constructor->getParameters() as $index => $parameter) {
  83. if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
  84. continue;
  85. }
  86. try {
  87. if (!$typeHint = $parameter->getClass()) {
  88. // no default value? Then fail
  89. if (!$parameter->isOptional()) {
  90. throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
  91. }
  92. if (!array_key_exists($index, $arguments)) {
  93. // specifically pass the default value
  94. $arguments[$index] = $parameter->getDefaultValue();
  95. }
  96. continue;
  97. }
  98. if (null === $this->types) {
  99. $this->populateAvailableTypes();
  100. }
  101. if (isset($this->types[$typeHint->name]) && !isset($this->notGuessableTypes[$typeHint->name])) {
  102. $value = new Reference($this->types[$typeHint->name]);
  103. $this->usedTypes[$typeHint->name] = $id;
  104. } else {
  105. try {
  106. $value = $this->createAutowiredDefinition($typeHint, $id);
  107. $this->usedTypes[$typeHint->name] = $id;
  108. } catch (RuntimeException $e) {
  109. if ($parameter->allowsNull()) {
  110. $value = null;
  111. } elseif ($parameter->isDefaultValueAvailable()) {
  112. $value = $parameter->getDefaultValue();
  113. } else {
  114. throw $e;
  115. }
  116. }
  117. }
  118. } catch (\ReflectionException $e) {
  119. // Typehint against a non-existing class
  120. if (!$parameter->isDefaultValueAvailable()) {
  121. throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e);
  122. }
  123. $value = $parameter->getDefaultValue();
  124. }
  125. $arguments[$index] = $value;
  126. }
  127. // it's possible index 1 was set, then index 0, then 2, etc
  128. // make sure that we re-order so they're injected as expected
  129. ksort($arguments);
  130. $definition->setArguments($arguments);
  131. }
  132. /**
  133. * Populates the list of available types.
  134. */
  135. private function populateAvailableTypes()
  136. {
  137. $this->types = array();
  138. foreach ($this->container->getDefinitions() as $id => $definition) {
  139. $this->populateAvailableType($id, $definition);
  140. }
  141. }
  142. /**
  143. * Populates the list of available types for a given definition.
  144. *
  145. * @param string $id
  146. * @param Definition $definition
  147. */
  148. private function populateAvailableType($id, Definition $definition)
  149. {
  150. // Never use abstract services
  151. if ($definition->isAbstract()) {
  152. return;
  153. }
  154. foreach ($definition->getAutowiringTypes() as $type) {
  155. $this->definedTypes[$type] = true;
  156. $this->types[$type] = $id;
  157. unset($this->notGuessableTypes[$type]);
  158. }
  159. if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
  160. return;
  161. }
  162. foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
  163. $this->set($reflectionInterface->name, $id);
  164. }
  165. do {
  166. $this->set($reflectionClass->name, $id);
  167. } while ($reflectionClass = $reflectionClass->getParentClass());
  168. }
  169. /**
  170. * Associates a type and a service id if applicable.
  171. *
  172. * @param string $type
  173. * @param string $id
  174. */
  175. private function set($type, $id)
  176. {
  177. if (isset($this->definedTypes[$type])) {
  178. return;
  179. }
  180. if (!isset($this->types[$type])) {
  181. $this->types[$type] = $id;
  182. return;
  183. }
  184. if ($this->types[$type] === $id) {
  185. return;
  186. }
  187. if (!isset($this->notGuessableTypes[$type])) {
  188. $this->notGuessableTypes[$type] = true;
  189. $this->types[$type] = (array) $this->types[$type];
  190. }
  191. $this->types[$type][] = $id;
  192. }
  193. /**
  194. * Registers a definition for the type if possible or throws an exception.
  195. *
  196. * @param \ReflectionClass $typeHint
  197. * @param string $id
  198. *
  199. * @return Reference A reference to the registered definition
  200. *
  201. * @throws RuntimeException
  202. */
  203. private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
  204. {
  205. if (isset($this->notGuessableTypes[$typeHint->name])) {
  206. $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
  207. $matchingServices = implode(', ', $this->types[$typeHint->name]);
  208. throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices));
  209. }
  210. if (!$typeHint->isInstantiable()) {
  211. $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
  212. throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeHint->name, $id, $classOrInterface));
  213. }
  214. $argumentId = sprintf('autowired.%s', $typeHint->name);
  215. $argumentDefinition = $this->container->register($argumentId, $typeHint->name);
  216. $argumentDefinition->setPublic(false);
  217. $this->populateAvailableType($argumentId, $argumentDefinition);
  218. try {
  219. $this->completeDefinition($argumentId, $argumentDefinition);
  220. } catch (RuntimeException $e) {
  221. $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
  222. $message = sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeHint->name, $id, $classOrInterface);
  223. throw new RuntimeException($message, 0, $e);
  224. }
  225. return new Reference($argumentId);
  226. }
  227. /**
  228. * Retrieves the reflection class associated with the given service.
  229. *
  230. * @param string $id
  231. * @param Definition $definition
  232. *
  233. * @return \ReflectionClass|false
  234. */
  235. private function getReflectionClass($id, Definition $definition)
  236. {
  237. if (isset($this->reflectionClasses[$id])) {
  238. return $this->reflectionClasses[$id];
  239. }
  240. // Cannot use reflection if the class isn't set
  241. if (!$class = $definition->getClass()) {
  242. return false;
  243. }
  244. $class = $this->container->getParameterBag()->resolveValue($class);
  245. try {
  246. $reflector = new \ReflectionClass($class);
  247. } catch (\ReflectionException $e) {
  248. $reflector = false;
  249. }
  250. return $this->reflectionClasses[$id] = $reflector;
  251. }
  252. }