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

Skip to content

Commit d7432c0

Browse files
dawehnerfabpot
authored andcommitted
Add an auto_alias compiler pass
1 parent eb4c3da commit d7432c0

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
18+
/**
19+
* Sets a service to be an alias of another one, given a format pattern.
20+
*/
21+
class AutoAliasServicePass implements CompilerPassInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function process(ContainerBuilder $container)
27+
{
28+
foreach ($container->findTaggedServiceIds('auto_alias') as $serviceId => $tags) {
29+
foreach ($tags as $tag) {
30+
if (!isset($tag['format'])) {
31+
throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId));
32+
}
33+
34+
$aliasId = $container->getParameterBag()->resolveValue($tag['format']);
35+
if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) {
36+
$container->setAlias($serviceId, new Alias($aliasId));
37+
}
38+
}
39+
}
40+
}
41+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 testProcessWithMissingParameter()
14+
{
15+
$container = new ContainerBuilder();
16+
17+
$container->register('example')
18+
->addTag('auto_alias', array('format' => '%non_existing%.example'));
19+
20+
$pass = new AutoAliasServicePass();
21+
$pass->process($container);
22+
}
23+
24+
/**
25+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
26+
*/
27+
public function testProcessWithMissingFormat()
28+
{
29+
$container = new ContainerBuilder();
30+
31+
$container->register('example')
32+
->addTag('auto_alias', array());
33+
$container->setParameter('existing', 'mysql');
34+
35+
$pass = new AutoAliasServicePass();
36+
$pass->process($container);
37+
}
38+
39+
public function testProcessWithNonExistingAlias()
40+
{
41+
$container = new ContainerBuilder();
42+
43+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
44+
->addTag('auto_alias', array('format' => '%existing%.example'));
45+
$container->setParameter('existing', 'mysql');
46+
47+
$pass = new AutoAliasServicePass();
48+
$pass->process($container);
49+
50+
$this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass());
51+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example'));
52+
}
53+
54+
public function testProcessWithExistingAlias()
55+
{
56+
$container = new ContainerBuilder();
57+
58+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
59+
->addTag('auto_alias', array('format' => '%existing%.example'));
60+
61+
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
62+
$container->setParameter('existing', 'mysql');
63+
64+
$pass = new AutoAliasServicePass();
65+
$pass->process($container);
66+
67+
$this->assertTrue($container->hasAlias('example'));
68+
$this->assertEquals('mysql.example', $container->getAlias('example'));
69+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example'));
70+
}
71+
72+
public function testProcessWithManualAlias()
73+
{
74+
$container = new ContainerBuilder();
75+
76+
$container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
77+
->addTag('auto_alias', array('format' => '%existing%.example'));
78+
79+
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
80+
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
81+
$container->setAlias('example', 'mariadb.example');
82+
$container->setParameter('existing', 'mysql');
83+
84+
$pass = new AutoAliasServicePass();
85+
$pass->process($container);
86+
87+
$this->assertTrue($container->hasAlias('example'));
88+
$this->assertEquals('mariadb.example', $container->getAlias('example'));
89+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
90+
}
91+
}
92+
93+
class ServiceClassDefault
94+
{
95+
}
96+
97+
class ServiceClassMysql extends ServiceClassDefault
98+
{
99+
}
100+
101+
class ServiceClassMariaDb extends ServiceClassMysql
102+
{
103+
}

0 commit comments

Comments
 (0)
0