Skip to content

Commit

Permalink
Add constructor of anonymous services
Browse files Browse the repository at this point in the history
  • Loading branch information
unkind committed Oct 20, 2017
1 parent 1376b4b commit f304759
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Expand Up @@ -1440,6 +1440,21 @@ public function log(CompilerPassInterface $pass, $message)
$this->getCompiler()->log($pass, $message);
}

/**
* @param $prefix
* @param string $prefix
* @return string
*/
public function generateServiceName($prefix) {
$i = 0;

while ($this->hasDefinition($prefix . $i)) {
$i++;
}

return $prefix . $i;
}

/**
* Returns the Service Conditionals.
*
Expand Down
Expand Up @@ -90,6 +90,17 @@ final public function set($id, $class = null)
return null !== $class ? $configurator->class($class) : $configurator;
}

/**
* Creates anonymous service (service without explicit name).
*
* @param string $class
* @return ServiceConfigurator
*/
final public function anonymous($class)
{
return $this->set($this->container->generateServiceName('anon'), $class)->private();
}

/**
* Creates an alias.
*
Expand Down
Expand Up @@ -1250,6 +1250,18 @@ public function testParameterWithMixedCase()

$this->assertSame('bar', $container->get('foo')->foo);
}

public function testServiceNameGenerator()
{
$container = new ContainerBuilder();
$this->assertSame('foo0', $container->generateServiceName('foo'));
$this->assertSame('bar0', $container->generateServiceName('bar'));

$container->register('foo0', \stdClass::class);
$container->register('foo1', \stdClass::class);

$this->assertSame('foo2', $container->generateServiceName('foo'));
}
}

class FooClass
Expand Down
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

/**
*
*/
final class StdClassDecorator
{
public function __construct(\stdClass $foo)
{
$this->foo = $foo;
}
}
@@ -0,0 +1,19 @@

services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: true
synthetic: true
listener_aggregator:
class: Bar\FooClass
public: true
arguments: [!tagged listener]
anon1:
class: stdClass
public: false
tags:
- { name: listener }
decorated:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator
public: true
arguments: [!service { class: stdClass, public: false }]
@@ -0,0 +1,21 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Bar\FooClass;
use stdClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator;

return function (ContainerConfigurator $c) {
$s = $c->services();

$s->set('decorated', stdClass::class);

$s->anonymous(StdClassDecorator::class)
->decorate('decorated', 'decorator42')
->args([ref('decorator42')]);

$s->set('listener_aggregator', FooClass::class)->public()->args([tagged('listener')]);

$s->anonymous(stdClass::class)->tag('listener');
};
Expand Up @@ -71,6 +71,7 @@ public function provideConfig()
yield array('instanceof');
yield array('prototype');
yield array('child');
yield array('anonymous');

if (\PHP_VERSION_ID >= 70000) {
yield array('php7');
Expand Down

0 comments on commit f304759

Please sign in to comment.