Skip to content

Commit

Permalink
feature #42625 [DependencyInjection] Add service_closure() to the PHP…
Browse files Browse the repository at this point in the history
…-DSL (HypeMC)

This PR was merged into the 5.4 branch.

Discussion
----------

[DependencyInjection] Add service_closure() to the PHP-DSL

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | symfony/symfony-docs#15730 and symfony/symfony-docs#15731

Adds a `service_closure()` function to the PHP-DSL.

Commits
-------

f333fa0 [DI] Add service_closure() to the PHP-DSL
  • Loading branch information
nicolas-grekas committed Aug 20, 2021
2 parents c9dc567 + f333fa0 commit a42062d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add `service_closure()` to the PHP-DSL

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Config\Loader\ParamConfigurator;
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Parameter;
Expand Down Expand Up @@ -77,7 +78,9 @@ public static function processValue($value, $allowServices = false)
}

if ($value instanceof ReferenceConfigurator) {
return new Reference($value->id, $value->invalidBehavior);
$reference = new Reference($value->id, $value->invalidBehavior);

return $value instanceof ClosureReferenceConfigurator ? new ServiceClosureArgument($reference) : $reference;
}

if ($value instanceof InlineServiceConfigurator) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

class ClosureReferenceConfigurator extends ReferenceConfigurator
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,11 @@ function env(string $name): EnvConfigurator
{
return new EnvConfigurator($name);
}

/**
* Creates a closure service reference.
*/
function service_closure(string $serviceId): ClosureReferenceConfigurator
{
return new ClosureReferenceConfigurator($serviceId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

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

$s->set('foo', 'Foo');

$s->set('service_closure', 'Bar')
->args([service_closure('foo')]);

$s->set('service_closure_invalid', 'Bar')
->args([service_closure('invalid_service')->nullOnInvalid()]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
*/
class ProjectServiceContainer extends Container
{
protected $parameters = [];

public function __construct()
{
$this->services = $this->privates = [];
$this->methodMap = [
'foo' => 'getFooService',
'service_closure' => 'getServiceClosureService',
'service_closure_invalid' => 'getServiceClosureInvalidService',
];

$this->aliases = [];
}

public function compile(): void
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled(): bool
{
return true;
}

public function getRemovedIds(): array
{
return [
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
];
}

/**
* Gets the public 'foo' shared service.
*
* @return \Foo
*/
protected function getFooService()
{
return $this->services['foo'] = new \Foo();
}

/**
* Gets the public 'service_closure' shared service.
*
* @return \Bar
*/
protected function getServiceClosureService()
{
return $this->services['service_closure'] = new \Bar(function () {
return ($this->services['foo'] ?? ($this->services['foo'] = new \Foo()));
});
}

/**
* Gets the public 'service_closure_invalid' shared service.
*
* @return \Bar
*/
protected function getServiceClosureInvalidService()
{
return $this->services['service_closure_invalid'] = new \Bar(function () {
return NULL;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public function testConfigServices()
$this->assertStringEqualsFile($fixtures.'/php/services9_compiled.php', str_replace(str_replace('\\', '\\\\', $fixtures.\DIRECTORY_SEPARATOR.'includes'.\DIRECTORY_SEPARATOR), '%path%', $dumper->dump()));
}

public function testConfigServiceClosure()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
$loader = new PhpFileLoader($container = new ContainerBuilder(), new FileLocator());
$loader->load($fixtures.'/config/services_closure_argument.php');

$container->compile();
$dumper = new PhpDumper($container);
$this->assertStringEqualsFile($fixtures.'/php/services_closure_argument_compiled.php', $dumper->dump());
}

/**
* @dataProvider provideConfig
*/
Expand Down

0 comments on commit a42062d

Please sign in to comment.