Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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