Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0e18d3e

Browse filesBrowse files
[DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service
1 parent a603ba0 commit 0e18d3e
Copy full SHA for 0e18d3e

File tree

7 files changed

+126
-2
lines changed
Filter options

7 files changed

+126
-2
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+2-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+8Lines 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;
@@ -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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Exception/ParameterNotFoundException.php
+3-1Lines 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;
+51Lines 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+
}
+55Lines 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+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.