8000 [DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters … · symfony/symfony@fb6e28e · GitHub
[go: up one dir, main page]

Skip to content

Commit fb6e28e

Browse files
[DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service
1 parent a603ba0 commit fb6e28e

File tree

7 files changed

+98
-2
lines changed

7 files changed

+98
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/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
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
3535
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
3636
use Symfony\Component\DependencyInjection\Exception\LogicException;
37+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
38+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
3739
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3840
use Symfony\Component\DependencyInjection\Reference;
3941
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
@@ -110,6 +112,12 @@ public function load(array $configs, ContainerBuilder $container)
110112
$loader->load('services.xml');
111113
$loader->load('fragment_renderer.xml');
112114

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

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added support for variadics in named arguments
8+
* added PSR-11 `ContainerBagInterface` and its `ContainerBag` implementation to access parameters as-a-service
89

910
4.0.0
1011
-----

src/Symfony/Component/DependencyInjection/Exception/ParameterNotFoundException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
< 628C div class="d-flex flex-row">
@@ -11,12 +11,14 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Exception;
1313

14+
use Psr\Container\NotFoundExceptionInterface;
15+
1416
/**
1517
* This exception is thrown when a non-existent parameter is used.
1618
*
1719
* @author Fabien Potencier <fabien@symfony.com>
1820
*/
19-
class ParameterNotFoundException extends InvalidArgumentException
21+
class ParameterNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
2022
{
2123
private $key;
2224
private $sourceId;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\ParameterBag;
13+
14+
use Symfony\Component\DependencyInjection\Container;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class ContainerBag extends FrozenParameterBag implements ContainerBagInterface
20+
{
21+
private $container;
22+
23+
public function __construct(Container $container)
24+
{
25+
$this->container = $container;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function all()
32+
{
33+
return $this->container->getParameterBag()->all();
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function get($name)
40+
{
41+
return $this->container->getParameter($name);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function has($name)
48+
{
49+
return $this->container->hasParameter($name);
50+
}
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\ParameterBag;
13+
14+
use Psr\Container\ContainerInterface;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
interface ContainerBagInterface extends ContainerInterface
20+
{
21+
/**
22+
* Gets the service container parameters.
23+
*
24+
* @return array An array of parameters
25+
*/
26+
public function all();
27+
}

0 commit comments

Comments
 (0)
0