4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Definition;
17 * Looks for definitions with autowiring enabled and registers their corresponding "@required" methods as setters.
19 * @author Nicolas Grekas <p@tchwork.com>
21 class AutowireRequiredMethodsPass extends AbstractRecursivePass
26 protected function processValue($value, $isRoot = false)
28 $value = parent::processValue($value, $isRoot);
30 if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
33 if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
37 $alreadyCalledMethods = array();
39 foreach ($value->getMethodCalls() as list($method)) {
40 $alreadyCalledMethods[strtolower($method)] = true;
43 foreach ($reflectionClass->getMethods() as $reflectionMethod) {
44 $r = $reflectionMethod;
46 if ($r->isConstructor() || isset($alreadyCalledMethods[strtolower($r->name)])) {
51 if (false !== $doc = $r->getDocComment()) {
52 if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
53 $value->addMethodCall($reflectionMethod->name);
56 if (false === stripos($doc, '@inheritdoc') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+(?:\{@inheritdoc\}|@inheritdoc)(?:\s|\*/$)#i', $doc)) {
61 $r = $r->getPrototype();
62 } catch (\ReflectionException $e) {
63 break; // method has no prototype