Skip to content

Commit b56ab7b

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 7744e8f + 561cd7e commit b56ab7b

File tree

8 files changed

+191
-2
lines changed

8 files changed

+191
-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
@@ -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

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
@@ -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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*/
20+
interface ContainerBagInterface extends ContainerInterface
21+
{
22+
/**
23+
* Gets the service container parameters.
24+
*
25+
* @return array An array of parameters
26+
*/
27+
public function all();
28+
29+
/**
30+
* Replaces parameter placeholders (%name%) by their values.
31+
*
32+
* @param mixed $value A value
33+
*
34+
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
35+
*/
36+
public function resolveValue($value);
37+
38+
/**
39+
* Escape parameter placeholders %.
40+
*
41+
* @param mixed $value
42+
*
43+
* @return mixed
44+
*/
45+
public function escapeValue($value);
46+
47+
/**
48+
* Unescape parameter placeholders %.
49+
*
50+
* @param mixed $value
51+
*
52+
* @return mixed
53+
*/
54+
public function unescapeValue($value);
55+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Tests\ParameterBag;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Container\ContainerInterface;
16+
use Symfony\Component\DependencyInjection\Container;
17+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
18+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
19+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
20+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
21+
22+
class ContainerBagTest extends TestCase
23+
{
24+
/** @var ParameterBag */
25+
private $parameterBag;
26+
/** @var ContainerBag */
27+
private $containerBag;
28+
29+
public function setUp()
30+
{
31+
$this->parameterBag = new ParameterBag(array('foo' => 'value'));
32+
$this->containerBag = new ContainerBag(new Container($this->parameterBag));
33+
}
34+
35+
public function testGetAllParameters()
36+
{
37+
$this->assertSame(array('foo' => 'value'), $this->containerBag->all());
38+
}
39+
40+
public function testHasAParameter()
41+
{
42+
$this->assertTrue($this->containerBag->has('foo'));
43+
$this->assertFalse($this->containerBag->has('bar'));
44+
}
45+
46+
public function testGetParameter()
47+
{
48+
$this->assertSame('value', $this->containerBag->get('foo'));
49+
}
50+
51+
/**
52+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
53+
*/
54+
public function testGetParameterNotFound()
55+
{
56+
$this->containerBag->get('bar');
57+
}
58+
59+
public function testInstanceOf()
60+
{
61+
$this->assertInstanceOf(FrozenParameterBag::class, $this->containerBag);
62+
$this->assertInstanceOf(ContainerBagInterface::class, $this->containerBag);
63+
$this->assertInstanceOf(ContainerInterface::class, $this->containerBag);
64+
}
65+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy