AutowireRequiredMethodsPass.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Definition;
  12. /**
  13. * Looks for definitions with autowiring enabled and registers their corresponding "@required" methods as setters.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class AutowireRequiredMethodsPass extends AbstractRecursivePass
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function processValue($value, $isRoot = false)
  23. {
  24. $value = parent::processValue($value, $isRoot);
  25. if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
  26. return $value;
  27. }
  28. if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
  29. return $value;
  30. }
  31. $alreadyCalledMethods = [];
  32. $withers = [];
  33. foreach ($value->getMethodCalls() as [$method]) {
  34. $alreadyCalledMethods[strtolower($method)] = true;
  35. }
  36. foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  37. $r = $reflectionMethod;
  38. if ($r->isConstructor() || isset($alreadyCalledMethods[strtolower($r->name)])) {
  39. continue;
  40. }
  41. while (true) {
  42. if (false !== $doc = $r->getDocComment()) {
  43. if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
  44. if (preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@return\s++static[\s\*]#i', $doc)) {
  45. $withers[] = [$reflectionMethod->name, [], true];
  46. } else {
  47. $value->addMethodCall($reflectionMethod->name, []);
  48. }
  49. break;
  50. }
  51. if (false === stripos($doc, '@inheritdoc') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+(?:\{@inheritdoc\}|@inheritdoc)(?:\s|\*/$)#i', $doc)) {
  52. break;
  53. }
  54. }
  55. try {
  56. $r = $r->getPrototype();
  57. } catch (\ReflectionException $e) {
  58. break; // method has no prototype
  59. }
  60. }
  61. }
  62. if ($withers) {
  63. // Prepend withers to prevent creating circular loops
  64. $setters = $value->getMethodCalls();
  65. $value->setMethodCalls($withers);
  66. foreach ($setters as $call) {
  67. $value->addMethodCall($call[0], $call[1], $call[2] ?? false);
  68. }
  69. }
  70. return $value;
  71. }
  72. }