-
Notifications
You must be signed in to change notification settings - Fork 60
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 #67 from jderusse/compiler-pass
Move Provider logic in a compilerpass
- Loading branch information
Showing
7 changed files
with
435 additions
and
26 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,56 @@ | ||
<?php | ||
|
||
namespace Swarrot\SwarrotBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class ProviderCompilerPass implements CompilerPassInterface | ||
{ | ||
/** {@inheritDoc} */ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->has('swarrot.factory.default') || !$container->hasParameter('swarrot.provider_config')) { | ||
return; | ||
} | ||
|
||
$providersIds = []; | ||
|
||
foreach ($container->findTaggedServiceIds('swarrot.provider_factory') as $id => $tags) { | ||
foreach ($tags as $tag) { | ||
if (!isset($tag['alias'])) { | ||
throw new \InvalidArgumentException( | ||
sprintf('The provider\'s alias is no defined for the service "%s"', $id) | ||
); | ||
} | ||
$providersIds[$tag['alias']] = $id; | ||
} | ||
} | ||
|
||
list($provider, $connections) = $container->getParameter('swarrot.provider_config'); | ||
|
||
if (!isset($providersIds[$provider])) { | ||
throw new \InvalidArgumentException(sprintf('Invalid provider "%s"', $provider)); | ||
} | ||
|
||
$id = $providersIds[$provider]; | ||
$definition = $container->getDefinition($id); | ||
$className = $container->getParameterBag()->resolveValue($definition->getClass()); | ||
|
||
$reflection = new \ReflectionClass($className); | ||
|
||
if (!$reflection->implementsInterface('Swarrot\\SwarrotBundle\\Broker\\FactoryInterface')) { | ||
throw new \InvalidArgumentException(sprintf('The provider "%s" is not valid', $provider)); | ||
} | ||
|
||
foreach ($connections as $name => $connectionConfig) { | ||
$definition->addMethodCall('addConnection', [ | ||
$name, | ||
$connectionConfig | ||
]); | ||
} | ||
|
||
$container->setAlias('swarrot.factory.default', $id); | ||
$container->getParameterBag()->remove('swarrot.provider_config'); | ||
} | ||
} |
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
Oops, something went wrong.