Skip to content

Commit d4246e8

Browse files
committed
Add an auto_alias compiler pass
1 parent 7adb842 commit d4246e8

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Alias;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
18+
19+
/**
20+
* Sets a service to be an alias of another one, given a format pattern.
21+
*/
22+
class AutoAliasServicePass implements CompilerPassInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function process(ContainerBuilder $container)
28+
{
29+
foreach ($container->findTaggedServiceIds('auto_alias') as $service_id => $tags) {
30+
// We don't want to deal with an existing alias.
31+
if ($container->hasAlias($service_id)) {
32+
continue;
33+
}
34+
foreach ($tags as $tag) {
35+
if (!isset($tag['parameter_name'])) {
36+
throw new ParameterNotFoundException(sprintf('Missing tag information "parameter_name" on auto_alias service %s', $service_id));
37+
}
38+
39+
$parameter_name = $tag['parameter_name'];
40+
if (!$container->hasParameter($parameter_name)) {
41+
throw new ParameterNotFoundException(sprintf('Missing parameter %s', $parameter_name));
42+
}
43+
44+
if (!isset($tag['format'])) {
45+
throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service %s', $service_id));
46+
}
47+
48+
$parameter_value = $container->getParameter($parameter_name);
49+
$format = $tag['format'];
50+
$alias_id = str_replace('%s', $parameter_value, $format);
51+
if ($container->hasDefinition($alias_id) || $container->hasAlias($alias_id)) {
52+
$container->setAlias($service_id, new Alias($alias_id));
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\AutoAliasServicePass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
class AutoAliasServicePassTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
12+
*/
13+
public function testProcessWithMissingParameterName()
14+
{
15+
$container = new ContainerBuilder();
16+
17+
$container->register('example')
18+
->addTag('auto_alias');
19+
20+
$pass = new AutoAliasServicePass();
21+
$pass->process($container);
22+
}
23+
24+
/**
25+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
26+
*/
27+
public function testProcessWithMissingParameter()
28+
{
29+
$container = new ContainerBuilder();
30+
31+
$container->register('example')
32+
->addTag('auto_alias', array('parameter_name' => 'non_existing'));
33+
34+
$pass = new AutoAliasServicePass();
35+
$pass->process($container);
36+
}
37+
38+
/**
39+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
40+
*/
41+
public function testProcessWithMissingFormat()
42+
{
43+
$container = new ContainerBuilder();
44+
45+
$container->register('example')
46+
->addTag('auto_alias', array('parameter_name' => 'existing'));
47+
$container->setParameter('existing', 'mysql');
48+
49+
$pass = new AutoAliasServicePass();
50+
$pass->process($container);
51+
}
52+
53+
public function testProcessWithNonExistingAlias()
54+
{
55+
$container = new ContainerBuilder();
56+
57+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
58+
->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example'));
59+
$container->setParameter('existing', 'mysql');
60+
61+
$pass = new AutoAliasServicePass();
62+
$pass->process($container);
63+
64+
$this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass());
65+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example'));
66+
}
67+
68+
public function testProcessWithExistingAlias()
69+
{
70+
$container = new ContainerBuilder();
71+
72+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
73+
->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example'));
74+
75+
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
76+
$container->setParameter('existing', 'mysql');
77+
78+
$pass = new AutoAliasServicePass();
79+
$pass->process($container);
80+
81+
$this->assertTrue($container->hasAlias('example'));
82+
$this->assertEquals('mysql.example', $container->getAlias('example'));
83+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example'));
84+
}
85+
86+
public function testProcessWithManualAlias()
87+
{
88+
$container = new ContainerBuilder();
89+
90+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
91+
->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example'));
92+
93+
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
94+
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
95+
$container->setAlias('example', 'mariadb.example');
96+
$container->setParameter('existing', 'mysql');
97+
98+
$pass = new AutoAliasServicePass();
99+
$pass->process($container);
100+
101+
$this->assertTrue($container->hasAlias('example'));
102+
$this->assertEquals('mariadb.example', $container->getAlias('example'));
103+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
104+
}
105+
}
106+
107+
class ServiceClassDefault
108+
{
109+
}
110+
111+
class ServiceClassMysql extends ServiceClassDefault
112+
{
113+
}
114+
115+
class ServiceClassMariaDb extends ServiceClassMysql
116+
{
117+
}

0 commit comments

Comments
 (0)