8000 Add constructor of anonymous services · symfony/symfony@766ceb3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 766ceb3

Browse files
committed
Add constructor of anonymous services
1 parent 1376b4b commit 766ceb3

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,23 @@ public function log(CompilerPassInterface $pass, $message)
14401440
$this->getCompiler()->log($pass, $message);
14411441
}
14421442

1443+
/**
1444+
* @param $prefix
1445+
* @param string $prefix
1446+
*
1447+
* @return string
1448+
*/
1449+
public function generateServiceName($prefix)
1450+
{
1451+
$i = 0;
1452+
1453+
while ($this->hasDefinition($prefix.$i)) {
1454+
++$i;
1455+
}
1456+
1457+
return $prefix.$i;
1458+
}
1459+
14431460
/**
14441461
* Returns the Service Conditionals.
14451462
*

src/Symfony/Component/DependencyInjection/Loader/Configurator/ServicesConfigurator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ final public function set($id, $class = null)
9090
return null !== $class ? $configurator->class($class) : $configurator;
9191
}
9292

93+
/**
94+
* Creates anonymous service (service without explicit name).
95+
*
96+
* @param string $class
97+
*
98+
* @return ServiceConfigurator
99+
*/
100+
final public function anonymous($class)
101+
{
102+
return $this->set($this->container->generateServiceName('anon'), $class)->private();
103+
}
104+
93105
/**
94106
* Creates an alias.
95107
*

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,18 @@ public function testParameterWithMixedCase()
12501250

12511251
$this->assertSame('bar', $container->get('foo')->foo);
12521252
}
1253+
1254+
public function testServiceNameGenerator()
1255+
{
1256+
$container = new ContainerBuilder();
1257+
$this->assertSame('foo0', $container->generateServiceName('foo'));
1258+
$this->assertSame('bar0', $container->generateServiceName('bar'));
1259+
1260+
$container->register('foo0', \stdClass::class);
1261+
$container->register('foo1', \stdClass::class);
1262+
1263+
$this->assertSame('foo2', $container->generateServiceName('foo'));
1264+
}
12531265
}
12541266

12551267
class FooClass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
final class StdClassDecorator
6+
{
7+
public function __construct(\stdClass $foo)
8+
{
9+
$this->foo = $foo;
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
services:
3+
service_container:
4+
class: Symfony\Component\DependencyInjection\ContainerInterface
5+
public: true
6+
synthetic: true
7+
listener_aggregator:
8+
class: Bar\FooClass
9+
public: true
10+
arguments: [!tagged listener]
11+
anon1:
12+
class: stdClass
13+
public: false
14+
tags:
15+
- { name: listener }
16+
decorated:
17+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator
18+
public: true
19+
arguments: [!service { class: stdClass, public: false }]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
use Bar\FooClass;
6+
use stdClass;
7+
use Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator;
8+
9+
return function (ContainerConfigurator $c) {
10+
$s = $c->services();
11+
12+
$s->set('decorated', stdClass::class);
13+
14+
$s->anonymous(StdClassDecorator::class)
15+
->decorate('decorated', 'decorator42')
16+
->args(array(ref('decorator42')));
17+
18+
$s->set('listener_aggregator', FooClass::class)->public()->args(array(tagged('listener')));
19+
20+
$s->anonymous(stdClass::class)->tag('listener');
21+
};

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function provideConfig()
7171
yield array('instanceof');
7272
yield array('prototype');
7373
yield array('child');
74+
yield array('anonymous');
7475

7576
if (\PHP_VERSION_ID >= 70000) {
7677
yield array('php7');

0 commit comments

Comments
 (0)
0