Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #25288 [DI][FrameworkBundle] Add PSR-11 "ContainerBag" to acc…
…ess 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
- Loading branch information
Showing
8 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/DependencyInjection/ParameterBag/ContainerBag.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\ParameterBag; | ||
|
||
use Symfony\Component\DependencyInjection\Container; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class ContainerBag extends FrozenParameterBag implements ContainerBagInterface | ||
{ | ||
private $container; | ||
|
||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function all() | ||
{ | ||
return $this->container->getParameterBag()->all(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($name) | ||
{ | ||
return $this->container->getParameter($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($name) | ||
{ | ||
return $this->container->hasParameter($name); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/DependencyInjection/ParameterBag/ContainerBagInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\ParameterBag; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface ContainerBagInterface extends ContainerInterface | ||
{ | ||
/** | ||
* Gets the service container parameters. | ||
* | ||
* @return array An array of parameters | ||
*/ | ||
public function all(); | ||
|
||
/** | ||
* Replaces parameter placeholders (%name%) by their values. | ||
* | ||
* @param mixed $value A value | ||
* | ||
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist | ||
*/ | ||
public function resolveValue($value); | ||
|
||
/** | ||
* Escape parameter placeholders %. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
*/ | ||
public function escapeValue($value); | ||
|
||
/** | ||
* Unescape parameter placeholders %. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
*/ | ||
public function unescapeValue($value); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ContainerBagTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\ParameterBag; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; | ||
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
|
||
class ContainerBagTest extends TestCase | ||
{ | ||
/** @var ParameterBag */ | ||
private $parameterBag; | ||
/** @var ContainerBag */ | ||
private $containerBag; | ||
|
||
public function setUp() | ||
{ | ||
$this->parameterBag = new ParameterBag(array('foo' => 'value')); | ||
$this->containerBag = new ContainerBag(new Container($this->parameterBag)); | ||
} | ||
|
||
public function testGetAllParameters() | ||
{ | ||
$this->assertSame(array('foo' => 'value'), $this->containerBag->all()); | ||
} | ||
|
||
public function testHasAParameter() | ||
{ | ||
$this->assertTrue($this->containerBag->has('foo')); | ||
$this->assertFalse($this->containerBag->has('bar')); | ||
} | ||
|
||
public function testGetParameter() | ||
{ | ||
$this->assertSame('value', $this->containerBag->get('foo')); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException | ||
*/ | ||
public function testGetParameterNotFound() | ||
{ | ||
$this->containerBag->get('bar'); | ||
} | ||
|
||
public function testInstanceOf() | ||
{ | ||
$this->assertInstanceOf(FrozenParameterBag::class, $this->containerBag); | ||
$this->assertInstanceOf(ContainerBagInterface::class, $this->containerBag); | ||
$this->assertInstanceOf(ContainerInterface::class, $this->containerBag); | ||
} | ||
} |