Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f333fa0

Browse files
committed
[DI] Add service_closure() to the PHP-DSL
1 parent f1643e8 commit f333fa0

File tree

7 files changed

+141
-1
lines changed

7 files changed

+141
-1
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `service_closure()` to the PHP-DSL
8+
49
5.3
510
---
611

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Loader\ParamConfigurator;
1515
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
1616
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
17+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1718
use Symfony\Component\DependencyInjection\Definition;
1819
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1920
use Symfony\Component\DependencyInjection\Parameter;
@@ -77,7 +78,9 @@ public static function processValue($value, $allowServices = false)
7778
}
7879

7980
if ($value instanceof ReferenceConfigurator) {
80-
return new Reference($value->id, $value->invalidBehavior);
81+
$reference = new Reference($value->id, $value->invalidBehavior);
82+
83+
return $value instanceof ClosureReferenceConfigurator ? new ServiceClosureArgument($reference) : $reference;
8184
}
8285

8386
if ($value instanceof InlineServiceConfigurator) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Loader\Configurator;
13+
14+
class ClosureReferenceConfigurator extends ReferenceConfigurator
15+
{
16+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,11 @@ function env(string $name): EnvConfigurator
202202
{
203203
return new EnvConfigurator($name);
204204
}
205+
206+
/**
207+
* Creates a closure service reference.
208+
*/
209+
function service_closure(string $serviceId): ClosureReferenceConfigurator
210+
{
211+
return new ClosureReferenceConfigurator($serviceId);
212+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
return function (ContainerConfigurator $c) {
6+
$s = $c->services()->defaults()->public();
7+
8+
$s->set('foo', 'Foo');
9+
10+
$s->set('service_closure', 'Bar')
11+
->args([service_closure('foo')]);
12+
13+
$s->set('service_closure_invalid', 'Bar')
14+
->args([service_closure('invalid_service')->nullOnInvalid()]);
15+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11+
12+
/**
13+
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
14+
*/
15+
class ProjectServiceContainer extends Container
16+
{
17+
protected $parameters = [];
18+
19+
public function __construct()
20+
{
21+
$this->services = $this->privates = [];
22+
$this->methodMap = [
23+
'foo' => 'getFooService',
24+
'service_closure' => 'getServiceClosureService',
25+
'service_closure_invalid' => 'getServiceClosureInvalidService',
26+
];
27+
28+
$this->aliases = [];
29+
}
30+
31+
public function compile(): void
32+
{
33+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
34+
}
35+
36+
public function isCompiled(): bool
37+
{
38+
return true;
39+
}
40+
41+
public function getRemovedIds(): array
42+
{
43+
return [
44+
'Psr\\Container\\ContainerInterface' => true,
45+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
46+
];
47+
}
48+
49+
/**
50+
* Gets the public 'foo' shared service.
51+
*
52+
* @return \Foo
53+
*/
54+
protected function getFooService()
55+
{
56+
return $this->services['foo'] = new \Foo();
57+
}
58+
59+
/**
60+
* Gets the public 'service_closure' shared service.
61+
*
62+
* @return \Bar
63+
*/
64+
protected function getServiceClosureService()
65+
{
66+
return $this->services['service_closure'] = new \Bar(function () {
67+
return ($this->services['foo'] ?? ($this->services['foo'] = new \Foo()));
68+
});
69+
}
70+
71+
/**
72+
* Gets the public 'service_closure_invalid' shared service.
73+
*
74+
* @return \Bar
75+
*/
76+
protected function getServiceClosureInvalidService()
77+
{
78+
return $this->services['service_closure_invalid'] = new \Bar(function () {
79+
return NULL;
80+
});
81+
}
82+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public function testConfigServices()
5757
$this->assertStringEqualsFile($fixtures.'/php/services9_compiled.php', str_replace(str_replace('\\', '\\\\', $fixtures.\DIRECTORY_SEPARATOR.'includes'.\DIRECTORY_SEPARATOR), '%path%', $dumper->dump()));
5858
}
5959

60+
public function testConfigServiceClosure()
61+
{
62+
$fixtures = realpath(__DIR__.'/../Fixtures');
63+
$loader = new PhpFileLoader($container = new ContainerBuilder(), new FileLocator());
64+
$loader->load($fixtures.'/config/services_closure_argument.php');
65+
66+
$container->compile();
67+
$dumper = new PhpDumper($container);
68+
$this->assertStringEqualsFile($fixtures.'/php/services_closure_argument_compiled.php', $dumper->dump());
69+
}
70+
6071
/**
6172
* @dataProvider provideConfig
6273
*/

0 commit comments

Comments
 (0)