-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from mcg-web/fix-aliases
Move Aliased to dedicated compiler pass
- Loading branch information
Showing
5 changed files
with
95 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler; | ||
|
||
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
final class AliasedPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$definitions = $this->filterDefinitions($container->getDefinitions()); | ||
foreach ($definitions as $definition) { | ||
$this->addDefinitionTagsFromAliases($definition); | ||
} | ||
} | ||
|
||
/** | ||
* @param Definition[] $definitions | ||
* | ||
* @return Definition[] | ||
*/ | ||
private function filterDefinitions($definitions) | ||
{ | ||
return array_filter($definitions, function (Definition $definition) { | ||
foreach (AutoMappingPass::SERVICE_SUBCLASS_TAG_MAPPING as $tagName) { | ||
if ($definition->hasTag($tagName)) { | ||
return is_subclass_of($definition->getClass(), AliasedInterface::class); | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
|
||
/** | ||
* @param Definition $definition | ||
*/ | ||
private function addDefinitionTagsFromAliases(Definition $definition) | ||
{ | ||
$aliases = call_user_func([$definition->getClass(), 'getAliases']); | ||
$tagName = $this->guessTagName($definition); | ||
$withMethod = TypeTaggedServiceMappingPass::TAG_NAME !== $tagName; | ||
|
||
foreach ($aliases as $key => $alias) { | ||
$definition->addTag($tagName, $withMethod ? ['alias' => $alias, 'method' => $key] : ['alias' => $alias]); | ||
} | ||
} | ||
|
||
private function guessTagName(Definition $definition) | ||
{ | ||
$tagName = null; | ||
foreach (AutoMappingPass::SERVICE_SUBCLASS_TAG_MAPPING as $refClassName => $tag) { | ||
if (is_subclass_of($definition->getClass(), $refClassName)) { | ||
$tagName = $tag; | ||
break; | ||
} | ||
} | ||
|
||
return $tagName; | ||
} | ||
} |
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
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