From d4246e88896fb064abd0f2b4dcada70913a80f24 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Thu, 20 Nov 2014 13:02:52 +0100 Subject: [PATCH 1/4] Add an auto_alias compiler pass --- .../Compiler/AutoAliasServicePass.php | 57 +++++++++ .../Compiler/AutoAliasServicePassTest.php | 117 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php new file mode 100644 index 0000000000000..a9b4751f0a702 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php @@ -0,0 +1,57 @@ + + * + * 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\Alias; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; +use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; + +/** + * Sets a service to be an alias of another one, given a format pattern. + */ +class AutoAliasServicePass implements CompilerPassInterface +{ + /** + * {@inheritdoc} + */ + public function process(ContainerBuilder $container) + { + foreach ($container->findTaggedServiceIds('auto_alias') as $service_id => $tags) { + // We don't want to deal with an existing alias. + if ($container->hasAlias($service_id)) { + continue; + } + foreach ($tags as $tag) { + if (!isset($tag['parameter_name'])) { + throw new ParameterNotFoundException(sprintf('Missing tag information "parameter_name" on auto_alias service %s', $service_id)); + } + + $parameter_name = $tag['parameter_name']; + if (!$container->hasParameter($parameter_name)) { + throw new ParameterNotFoundException(sprintf('Missing parameter %s', $parameter_name)); + } + + if (!isset($tag['format'])) { + throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service %s', $service_id)); + } + + $parameter_value = $container->getParameter($parameter_name); + $format = $tag['format']; + $alias_id = str_replace('%s', $parameter_value, $format); + if ($container->hasDefinition($alias_id) || $container->hasAlias($alias_id)) { + $container->setAlias($service_id, new Alias($alias_id)); + } + } + } + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php new file mode 100644 index 0000000000000..b9481dd19db8e --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php @@ -0,0 +1,117 @@ +register('example') + ->addTag('auto_alias'); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException + */ + public function testProcessWithMissingParameter() + { + $container = new ContainerBuilder(); + + $container->register('example') + ->addTag('auto_alias', array('parameter_name' => 'non_existing')); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + */ + public function testProcessWithMissingFormat() + { + $container = new ContainerBuilder(); + + $container->register('example') + ->addTag('auto_alias', array('parameter_name' => 'existing')); + $container->setParameter('existing', 'mysql'); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + } + + public function testProcessWithNonExistingAlias() + { + $container = new ContainerBuilder(); + + $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') + ->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example')); + $container->setParameter('existing', 'mysql'); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + + $this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass()); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example')); + } + + public function testProcessWithExistingAlias() + { + $container = new ContainerBuilder(); + + $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') + ->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example')); + + $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql'); + $container->setParameter('existing', 'mysql'); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + + $this->assertTrue($container->hasAlias('example')); + $this->assertEquals('mysql.example', $container->getAlias('example')); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example')); + } + + public function testProcessWithManualAlias() + { + $container = new ContainerBuilder(); + + $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') + ->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example')); + + $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql'); + $container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb'); + $container->setAlias('example', 'mariadb.example'); + $container->setParameter('existing', 'mysql'); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + + $this->assertTrue($container->hasAlias('example')); + $this->assertEquals('mariadb.example', $container->getAlias('example')); + $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example')); + } +} + +class ServiceClassDefault +{ +} + +class ServiceClassMysql extends ServiceClassDefault +{ +} + +class ServiceClassMariaDb extends ServiceClassMysql +{ +} From 205072497763394b5eae7f0226f780ac161bdd69 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Thu, 20 Nov 2014 15:06:06 +0100 Subject: [PATCH 2/4] adressed feedback from fabpot --- .../Compiler/AutoAliasServicePass.php | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php index a9b4751f0a702..fa6518e649a62 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php @@ -26,30 +26,29 @@ class AutoAliasServicePass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - foreach ($container->findTaggedServiceIds('auto_alias') as $service_id => $tags) { + foreach ($container->findTaggedServiceIds('auto_alias') as $serviceId => $tags) { // We don't want to deal with an existing alias. - if ($container->hasAlias($service_id)) { + if ($container->hasAlias($serviceId)) { continue; } foreach ($tags as $tag) { if (!isset($tag['parameter_name'])) { - throw new ParameterNotFoundException(sprintf('Missing tag information "parameter_name" on auto_alias service %s', $service_id)); + throw new ParameterNotFoundException(sprintf('Missing tag information "parameter_name" on auto_alias service "%s".', $serviceId)); } - $parameter_name = $tag['parameter_name']; - if (!$container->hasParameter($parameter_name)) { - throw new ParameterNotFoundException(sprintf('Missing parameter %s', $parameter_name)); + $parameterName = $tag['parameter_name']; + if (!$container->hasParameter($parameterName)) { + throw new ParameterNotFoundException(sprintf('Missing parameter "%s"', $parameterName)); } if (!isset($tag['format'])) { - throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service %s', $service_id)); + throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId)); } - $parameter_value = $container->getParameter($parameter_name); - $format = $tag['format']; - $alias_id = str_replace('%s', $parameter_value, $format); - if ($container->hasDefinition($alias_id) || $container->hasAlias($alias_id)) { - $container->setAlias($service_id, new Alias($alias_id)); + $parameterValue = $container->getParameter($parameterName); + $aliasId = sprintf($tag['format'], $parameterValue); + if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) { + $container->setAlias($serviceId, new Alias($aliasId)); } } } From fd13330d9436be1de36c31d0db469fe23148b25e Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Fri, 21 Nov 2014 17:42:37 +0100 Subject: [PATCH 3/4] adressed the feedback of stof --- .../Compiler/AutoAliasServicePass.php | 16 +------------ .../Compiler/AutoAliasServicePassTest.php | 23 ++++--------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php index fa6518e649a62..7971dcd04f407 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php @@ -27,26 +27,12 @@ class AutoAliasServicePass implements CompilerPassInterface public function process(ContainerBuilder $container) { foreach ($container->findTaggedServiceIds('auto_alias') as $serviceId => $tags) { - // We don't want to deal with an existing alias. - if ($container->hasAlias($serviceId)) { - continue; - } foreach ($tags as $tag) { - if (!isset($tag['parameter_name'])) { - throw new ParameterNotFoundException(sprintf('Missing tag information "parameter_name" on auto_alias service "%s".', $serviceId)); - } - - $parameterName = $tag['parameter_name']; - if (!$container->hasParameter($parameterName)) { - throw new ParameterNotFoundException(sprintf('Missing parameter "%s"', $parameterName)); - } - if (!isset($tag['format'])) { throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId)); } - $parameterValue = $container->getParameter($parameterName); - $aliasId = sprintf($tag['format'], $parameterValue); + $aliasId = $container->getParameterBag()->resolveValue($tag['format']); if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) { $container->setAlias($serviceId, new Alias($aliasId)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php index b9481dd19db8e..a80466589cabb 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php @@ -7,19 +7,6 @@ class AutoAliasServicePassTest extends \PHPUnit_Framework_TestCase { - /** - * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException - */ - public function testProcessWithMissingParameterName() - { - $container = new ContainerBuilder(); - - $container->register('example') - ->addTag('auto_alias'); - - $pass = new AutoAliasServicePass(); - $pass->process($container); - } /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException @@ -29,7 +16,7 @@ public function testProcessWithMissingParameter() $container = new ContainerBuilder(); $container->register('example') - ->addTag('auto_alias', array('parameter_name' => 'non_existing')); + ->addTag('auto_alias', array('format' => '%non_existing%.example')); $pass = new AutoAliasServicePass(); $pass->process($container); @@ -43,7 +30,7 @@ public function testProcessWithMissingFormat() $container = new ContainerBuilder(); $container->register('example') - ->addTag('auto_alias', array('parameter_name' => 'existing')); + ->addTag('auto_alias', array()); $container->setParameter('existing', 'mysql'); $pass = new AutoAliasServicePass(); @@ -55,7 +42,7 @@ public function testProcessWithNonExistingAlias() $container = new ContainerBuilder(); $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') - ->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example')); + ->addTag('auto_alias', array('format' => '%existing%.example')); $container->setParameter('existing', 'mysql'); $pass = new AutoAliasServicePass(); @@ -70,7 +57,7 @@ public function testProcessWithExistingAlias() $container = new ContainerBuilder(); $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') - ->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example')); + ->addTag('auto_alias', array('format' => '%existing%.example')); $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql'); $container->setParameter('existing', 'mysql'); @@ -88,7 +75,7 @@ public function testProcessWithManualAlias() $container = new ContainerBuilder(); $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') - ->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example')); + ->addTag('auto_alias', array('format' => '%existing%.example')); $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql'); $container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb'); From 7f614aee59dcd28fee7daa9f104be3c71dff6b32 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Sat, 22 Nov 2014 19:12:38 +0100 Subject: [PATCH 4/4] Fix code style errors --- .../DependencyInjection/Compiler/AutoAliasServicePass.php | 1 - .../Tests/Compiler/AutoAliasServicePassTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php index 7971dcd04f407..c1f05e03ec02c 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php @@ -14,7 +14,6 @@ use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; -use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; /** * Sets a service to be an alias of another one, given a format pattern. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php index a80466589cabb..368ec3c5cd46a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php @@ -7,7 +7,6 @@ class AutoAliasServicePassTest extends \PHPUnit_Framework_TestCase { - /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException */