Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #24301 [DI] Add AutowireRequiredMethodsPass to fix bindings f…
…or `@required` methods (nicolas-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [DI] Add AutowireRequiredMethodsPass to fix bindings for `@required` methods | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Spotted while doing a SF4 workshop :) Discovery of `@required` methods should be split from AutowirePass so that bindings can apply to these methods also when autowiring is enabled. Commits ------- dc55dd2 [DI] Add AutowireRequiredMethodsPass to fix bindings for `@required` methods
- Loading branch information
Showing
9 changed files
with
535 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredMethodsPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* Looks for definitions with autowiring enabled and registers their corresponding "@required" methods as setters. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class AutowireRequiredMethodsPass extends AbstractRecursivePass | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function processValue($value, $isRoot = false) | ||
{ | ||
$value = parent::processValue($value, $isRoot); | ||
|
||
if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) { | ||
return $value; | ||
} | ||
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) { | ||
return $value; | ||
} | ||
|
||
$alreadyCalledMethods = array(); | ||
|
||
foreach ($value->getMethodCalls() as list($method)) { | ||
$alreadyCalledMethods[strtolower($method)] = true; | ||
} | ||
|
||
foreach ($reflectionClass->getMethods() as $reflectionMethod) { | ||
$r = $reflectionMethod; | ||
|
||
if ($r->isConstructor() || isset($alreadyCalledMethods[strtolower($r->name)])) { | ||
continue; | ||
} | ||
|
||
while (true) { | ||
if (false !== $doc = $r->getDocComment()) { | ||
if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) { | ||
$value->addMethodCall($reflectionMethod->name); | ||
break; | ||
} | ||
if (false === stripos($doc, '@inheritdoc') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+(?:\{@inheritdoc\}|@inheritdoc)(?:\s|\*/$)#i', $doc)) { | ||
break; | ||
} | ||
} | ||
try { | ||
$r = $r->getPrototype(); | ||
} catch (\ReflectionException $e) { | ||
break; // method has no prototype | ||
} | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.