-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DependencyInjection] Add "instanceof" section for local interface-defined configs #21530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[DependencyInjection] Add "instanceof" section for local interface-de…
…fined configs
- Loading branch information
commit 2fb601983f01e13ac9d91229052c37f759a799ed
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
106 changes: 106 additions & 0 deletions
106
src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionInheritancePass.php
This file contains hidden or 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,106 @@ | ||
<?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\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* Applies tags and instanceof inheritance to definitions. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class ResolveDefinitionInheritancePass extends AbstractRecursivePass | ||
{ | ||
protected function processValue($value, $isRoot = false) | ||
{ | ||
if (!$value instanceof Definition) { | ||
return parent::processValue($value, $isRoot); | ||
} | ||
if ($value instanceof ChildDefinition) { | ||
$this->resolveDefinition($value); | ||
} | ||
$class = $value->getClass(); | ||
if (!$class || false !== strpos($class, '%') || !$instanceof = $value->getInstanceofConditionals()) { | ||
return parent::processValue($value, $isRoot); | ||
} | ||
|
||
foreach ($instanceof as $interface => $definition) { | ||
if ($interface !== $class && (!$this->container->getReflectionClass($interface) || !$this->container->getReflectionClass($class))) { | ||
continue; | ||
} | ||
if ($interface === $class || is_subclass_of($class, $interface)) { | ||
$this->mergeDefinition($value, $definition); | ||
} | ||
} | ||
|
||
return parent::processValue($value, $isRoot); | ||
} | ||
|
||
/** | ||
* Populates the class and tags from parent definitions. | ||
*/ | ||
private function resolveDefinition(ChildDefinition $definition) | ||
{ | ||
if (!$this->container->has($parent = $definition->getParent())) { | ||
return; | ||
} | ||
|
||
$parentDef = $this->container->findDefinition($parent); | ||
if ($parentDef instanceof ChildDefinition) { | ||
$this->resolveDefinition($parentDef); | ||
} | ||
|
||
if (!isset($definition->getChanges()['class'])) { | ||
$definition->setClass($parentDef->getClass()); | ||
} | ||
|
||
// append parent tags when inheriting is enabled | ||
if ($definition->getInheritTags()) { | ||
foreach ($parentDef->getTags() as $k => $v) { | ||
foreach ($v as $v) { | ||
$definition->addTag($k, $v); | ||
} | ||
} | ||
} | ||
|
||
$definition->setInheritTags(false); | ||
} | ||
|
||
private function mergeDefinition(Definition $def, ChildDefinition $definition) | ||
{ | ||
$changes = $definition->getChanges(); | ||
if (isset($changes['shared'])) { | ||
$def->setShared($definition->isShared()); | ||
} | ||
if (isset($changes['abstract'])) { | ||
$def->setAbstract($definition->isAbstract()); | ||
} | ||
if (isset($changes['autowired_calls'])) { | ||
$autowiredCalls = $def->getAutowiredCalls(); | ||
} | ||
|
||
ResolveDefinitionTemplatesPass::mergeDefinition($def, $definition); | ||
|
||
// merge autowired calls | ||
if (isset($changes['autowired_calls'])) { | ||
$def->setAutowiredCalls(array_merge($autowiredCalls, $def->getAutowiredCalls())); | ||
} | ||
|
||
// merge tags | ||
foreach ($definition->getTags() as $k => $v) { | ||
foreach ($v as $v) { | ||
$def->addTag($k, $v); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -30,6 +30,7 @@ class Definition | |
private $properties = array(); | ||
private $calls = array(); | ||
private $getters = array(); | ||
private $instanceof = array(); | ||
private $configurator; | ||
private $tags = array(); | ||
private $public = true; | ||
|
@@ -363,6 +364,32 @@ public function getOverriddenGetters() | |
return $this->getters; | ||
} | ||
|
||
/** | ||
* Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class. | ||
* | ||
* @param $instanceof ChildDefinition[] | ||
* | ||
* @experimental in version 3.3 | ||
*/ | ||
public function setInstanceofConditionals(array $instanceof) | ||
{ | ||
$this->instanceof = $instanceof; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class. | ||
* | ||
* @return ChildDefinition[] | ||
* | ||
* @experimental in version 3.3 | ||
*/ | ||
public function getInstanceofConditionals() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should document the returned type (an array of something, but what ?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{ | ||
return $this->instanceof; | ||
} | ||
|
||
/** | ||
* Sets tags for this definition. | ||
* | ||
|
@@ -736,9 +763,7 @@ public function getAutowiredCalls() | |
*/ | ||
public function setAutowired($autowired) | ||
{ | ||
$this->autowiredCalls = $autowired ? array('__construct') : array(); | ||
|
||
return $this; | ||
return $this->setAutowiredCalls($autowired ? array('__construct') : array()); | ||
} | ||
|
||
/** | ||
|
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should document the type of the argument (what is inside the array ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done