Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\DependencyInjection\Compiler;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\DependencyInjection\Definition;
|
Chris@14
|
15
|
Chris@14
|
16 /**
|
Chris@14
|
17 * Looks for definitions with autowiring enabled and registers their corresponding "@required" methods as setters.
|
Chris@14
|
18 *
|
Chris@14
|
19 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@14
|
20 */
|
Chris@14
|
21 class AutowireRequiredMethodsPass extends AbstractRecursivePass
|
Chris@14
|
22 {
|
Chris@14
|
23 /**
|
Chris@14
|
24 * {@inheritdoc}
|
Chris@14
|
25 */
|
Chris@14
|
26 protected function processValue($value, $isRoot = false)
|
Chris@14
|
27 {
|
Chris@14
|
28 $value = parent::processValue($value, $isRoot);
|
Chris@14
|
29
|
Chris@14
|
30 if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
|
Chris@14
|
31 return $value;
|
Chris@14
|
32 }
|
Chris@14
|
33 if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
|
Chris@14
|
34 return $value;
|
Chris@14
|
35 }
|
Chris@14
|
36
|
Chris@17
|
37 $alreadyCalledMethods = [];
|
Chris@14
|
38
|
Chris@14
|
39 foreach ($value->getMethodCalls() as list($method)) {
|
Chris@14
|
40 $alreadyCalledMethods[strtolower($method)] = true;
|
Chris@14
|
41 }
|
Chris@14
|
42
|
Chris@14
|
43 foreach ($reflectionClass->getMethods() as $reflectionMethod) {
|
Chris@14
|
44 $r = $reflectionMethod;
|
Chris@14
|
45
|
Chris@14
|
46 if ($r->isConstructor() || isset($alreadyCalledMethods[strtolower($r->name)])) {
|
Chris@14
|
47 continue;
|
Chris@14
|
48 }
|
Chris@14
|
49
|
Chris@14
|
50 while (true) {
|
Chris@14
|
51 if (false !== $doc = $r->getDocComment()) {
|
Chris@14
|
52 if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
|
Chris@14
|
53 $value->addMethodCall($reflectionMethod->name);
|
Chris@14
|
54 break;
|
Chris@14
|
55 }
|
Chris@14
|
56 if (false === stripos($doc, '@inheritdoc') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+(?:\{@inheritdoc\}|@inheritdoc)(?:\s|\*/$)#i', $doc)) {
|
Chris@14
|
57 break;
|
Chris@14
|
58 }
|
Chris@14
|
59 }
|
Chris@14
|
60 try {
|
Chris@14
|
61 $r = $r->getPrototype();
|
Chris@14
|
62 } catch (\ReflectionException $e) {
|
Chris@14
|
63 break; // method has no prototype
|
Chris@14
|
64 }
|
Chris@14
|
65 }
|
Chris@14
|
66 }
|
Chris@14
|
67
|
Chris@14
|
68 return $value;
|
Chris@14
|
69 }
|
Chris@14
|
70 }
|