Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #12972 Make the container considered non-fresh if the environment…
… parameters are changed (thewilkybarkid) This PR was merged into the 2.3 branch. Discussion ---------- Make the container considered non-fresh if the environment parameters are changed | Q | A | ------------- | --- | Bug fix? | not really | New feature? | kind of | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11365 | License | MIT | Doc PR | symfony/symfony-docs#4645 Fixes #11365 by making the container non-fresh if any of the prefixed environment parameters are added/removed/changed. Not a bug as it was documented as behaving as such, but I think it's worth applying to 2.3 rather than considering it a new feature. Commits ------- b49fa12 Make the container considered non-fresh if the environment parameters are changed
- Loading branch information
Showing
4 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/Symfony/Component/HttpKernel/Config/EnvParametersResource.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,95 @@ | ||
<?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\HttpKernel\Config; | ||
|
||
use Symfony\Component\Config\Resource\ResourceInterface; | ||
|
||
/** | ||
* EnvParametersResource represents resources stored in prefixed environment variables. | ||
* | ||
* @author Chris Wilkinson <chriswilkinson84@gmail.com> | ||
*/ | ||
class EnvParametersResource implements ResourceInterface, \Serializable | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $prefix; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $variables; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $prefix | ||
*/ | ||
public function __construct($prefix) | ||
{ | ||
$this->prefix = $prefix; | ||
$this->variables = $this->findVariables(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __toString() | ||
{ | ||
return serialize($this->getResource()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getResource() | ||
{ | ||
return array('prefix' => $this->prefix, 'variables' => $this->variables); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isFresh($timestamp) | ||
{ | ||
return $this->findVariables() === $this->variables; | ||
} | ||
|
||
public function serialize() | ||
{ | ||
return serialize(array('prefix' => $this->prefix, 'variables' => $this->variables)); | ||
} | ||
|
||
public function unserialize($serialized) | ||
{ | ||
$unserialized = unserialize($serialized); | ||
|
||
$this->prefix = $unserialized['prefix']; | ||
$this->variables = $unserialized['variables']; | ||
} | ||
|
||
private function findVariables() | ||
{ | ||
$variables = array(); | ||
|
||
foreach ($_SERVER as $key => $value) { | ||
if (0 === strpos($key, $this->prefix)) { | ||
$variables[$key] = $value; | ||
} | ||
} | ||
|
||
ksort($variables); | ||
|
||
return $variables; | ||
} | ||
} |
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
106 changes: 106 additions & 0 deletions
106
src/Symfony/Component/HttpKernel/Tests/Config/EnvParametersResourceTest.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,106 @@ | ||
<?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\HttpKernel\Tests\Config; | ||
|
||
use Symfony\Component\HttpKernel\Config\EnvParametersResource; | ||
|
||
class EnvParametersResourceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $prefix = '__DUMMY_'; | ||
protected $initialEnv; | ||
protected $resource; | ||
|
||
protected function setUp() | ||
{ | ||
$this->initialEnv = array( | ||
$this->prefix.'1' => 'foo', | ||
$this->prefix.'2' => 'bar', | ||
); | ||
|
||
foreach ($this->initialEnv as $key => $value) { | ||
$_SERVER[$key] = $value; | ||
} | ||
|
||
$this->resource = new EnvParametersResource($this->prefix); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
foreach ($_SERVER as $key => $value) { | ||
if (0 === strpos($key, $this->prefix)) { | ||
unset($_SERVER[$key]); | ||
} | ||
} | ||
} | ||
|
||
public function testGetResource() | ||
{ | ||
$this->assertSame( | ||
array('prefix' => $this->prefix, 'variables' => $this->initialEnv), | ||
$this->resource->getResource(), | ||
'->getResource() returns the resource' | ||
); | ||
} | ||
|
||
public function testToString() | ||
{ | ||
$this->assertSame( | ||
serialize(array('prefix' => $this->prefix, 'variables' => $this->initialEnv)), | ||
(string) $this->resource | ||
); | ||
} | ||
|
||
public function testIsFreshNotChanged() | ||
{ | ||
$this->assertTrue( | ||
$this->resource->isFresh(time()), | ||
'->isFresh() returns true if the variables have not changed' | ||
); | ||
} | ||
|
||
public function testIsFreshValueChanged() | ||
{ | ||
reset($this->initialEnv); | ||
$_SERVER[key($this->initialEnv)] = 'baz'; | ||
|
||
$this->assertFalse( | ||
$this->resource->isFresh(time()), | ||
'->isFresh() returns false if a variable has been changed' | ||
); | ||
} | ||
|
||
public function testIsFreshValueRemoved() | ||
{ | ||
reset($this->initialEnv); | ||
unset($_SERVER[key($this->initialEnv)]); | ||
|
||
$this->assertFalse( | ||
$this->resource->isFresh(time()), | ||
'->isFresh() returns false if a variable has been removed' | ||
); | ||
} | ||
|
||
public function testIsFreshValueAdded() | ||
{ | ||
$_SERVER[$this->prefix.'3'] = 'foo'; | ||
|
||
$this->assertFalse( | ||
$this->resource->isFresh(time()), | ||
'->isFresh() returns false if a variable has been added' | ||
); | ||
} | ||
|
||
public function testSerializeUnserialize() | ||
{ | ||
$this->assertEquals($this->resource, unserialize(serialize($this->resource))); | ||
} | ||
} |
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