Skip to content

Commit 23ccefc

Browse files
feature #25288 [DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service (nicolas-grekas, sroze)
This PR was merged into the 4.1-dev branch. Discussion ---------- [DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17160 | License | MIT | Doc PR | - There is one thing that prevents us from not injecting the container: access to the parameter bag. This PR fixes this limitation by providing a PSR-11 `ContainerBagInterface` + related implementation, and wiring it as a service that ppl can then also autowire using the new interface as a type hint, or `ParameterBagInterface`. Needed to complete e.g. #24738 Commits ------- 561cd7e Add tests on the ContainerBag 0e18d3e [DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service
2 parents fc057e7 + 45c9df5 commit 23ccefc

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ CHANGELOG
44
4.1.0
55
-----
66

7-
* allowed to pass an optional `LoggerInterface $logger` instance to the `Router`
7+
* Allowed to pass an optional `LoggerInterface $logger` instance to the `Router`
8+
* Added a new `parameter_bag` service with related autowiring aliases to acces parameters as-a-service
89

910
4.0.0
1011
-----

DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
3636
use Symfony\Component\DependencyInjection\Exception\LogicException;
3737
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
38+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
39+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
3840
use Symfony\Component\DependencyInjection\Reference;
3941
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
4042
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -111,6 +113,12 @@ public function load(array $configs, ContainerBuilder $container)
111113
$loader->load('services.xml');
112114
$loader->load('fragment_renderer.xml');
113115

116+
if (!interface_exists(ContainerBagInterface::class)) {
117+
$container->removeDefinition('parameter_bag');
118+
$container->removeAlias(ContainerBagInterface::class);
119+
$container->removeAlias(ParameterBagInterface::class);
120+
}
121+
114122
if (class_exists(Application::class)) {
115123
$loader->load('console.xml');
116124

Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
<services>
88
<defaults public="false" />
99

10+
<service id="parameter_bag" class="Symfony\Component\DependencyInjection\ParameterBag\ContainerBag">
11+
<argument type="service" id="service_container" />
12+
</service>
13+
<service id="Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface" alias="parameter_bag" />
14+
<service id="Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface" alias="parameter_bag" />
15+
1016
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\EventDispatcher" public="true">
1117
<tag name="container.hot_path" />
1218
</service>

0 commit comments

Comments
 (0)