8000 Add an auto_alias compiler pass · symfony/symfony@be4daae · GitHub
[go: up one dir, main page]

Skip to content

Commit be4daae

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

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
/**
12+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
13+
*/
14+
public function testProcessWithMissingParameterName()
15+
{
16+
$container = new ContainerBuilder();
17+
18+
$container->register('example')
19+
->addTag('auto_alias');
20+
21+
$pass = new AutoAliasServicePass();
22+
$pass->process($container);
23+
}
24+
25+
/**
26+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
27+
*/
28+
public function testProcessWithMissingParameter()
29+
{
30+
$container = new ContainerBuilder();
31+
32+
$container->register('example')
33+
->addTag('auto_alias', array('parameter_name' => 'non_existing'));
34+
35+
$pass = new AutoAliasServicePass();
36+
$pass->process($container);
37+
}
38+
39+
/**
40+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
41+
*/
42+
public function testProcessWithMissingFormat()
43+
{
44+
$container = new ContainerBuilder();
45+
46+
$container->register('example')
47+
->addTag('auto_alias', array('parameter_name' => 'existing'));
48+
$container->setParameter('existing', 'mysql');
49+
50+
$pass = new AutoAliasServicePass();
51+
$pass->process($container);
52+
}
53+
54+
public function testProcessWithNonExistingAlias()
55+
{
56+
$container = new ContainerBuilder();
57+
58+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
59+
->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example'));
60+
$container->setParameter('existing', 'mysql');
61+
62+
$pass = new AutoAliasServicePass();
63+
$pass->process($container);
64+
65+
$this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass());
66+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example'));
67+
}
68+
69+
public function testProcessWithExistingAlias()
70+
{
71+
$container = new ContainerBuilder();
72+
73+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
74+
->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example'));
75+
76+
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
77+
$container->setParameter('existing', 'mysql');
78+
79+
$pass = new AutoAliasServicePass();
80+
$pass->process($container);
81+
82+
$this->assertTrue($container->hasAlias('example'));
83+
$this->assertEquals('mysql.example', $container->getAlias('example'));
84+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example'));
85+
}
86+
87+
public function testProcessWithManualAlias()
88+
{
89+
$container = new ContainerBuilder();
90+
91+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
92+
->addTag('auto_alias', array('parameter_name' => 'existing', 'format' => '%s.example'));
93+
94+
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
95+
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
96+
$container->setAlias('example', 'mariadb.example');
97+
$container->setParameter('existing', 'mysql');
98+
99+
$pass = new AutoAliasServicePass();
100+
$pass->process($container);
101+
102+
$this->assertTrue($container->hasAlias('example'));
103+
$this->assertEquals('mariadb.example', $container->getAlias('example'));
104+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
105+
}
106+
}
107+
108+
109+
class ServiceClassDefault
110+
{
111+
}
112+
113+
class ServiceClassMysql extends ServiceClassDefault
114+
{
115+
}
116+
117+
class ServiceClassMariaDb extends ServiceClassMysql
118+
{
119+
}

0 commit comments

Comments
 (0)
0