Skip to content

Commit b49fa12

Browse files
Make the container considered non-fresh if the environment parameters are changed
1 parent 503c760 commit b49fa12

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\HttpKernel\Config;
13+
14+
use Symfony\Component\Config\Resource\ResourceInterface;
15+
16+
/**
17+
* EnvParametersResource represents resources stored in prefixed environment variables.
18+
*
19+
* @author Chris Wilkinson <[email protected]>
20+
*/
21+
class EnvParametersResource implements ResourceInterface, \Serializable
22+
{
23+
/**
24+
* @var string
25+
*/
26+
private $prefix;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $variables;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @param string $prefix
37+
*/
38+
public function __construct($prefix)
39+
{
40+
$this->prefix = $prefix;
41+
$this->variables = $this->findVariables();
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function __toString()
48+
{
49+
return serialize($this->getResource());
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function getResource()
56+
{
57+
return array('prefix' => $this->prefix, 'variables' => $this->variables);
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function isFresh($timestamp)
64+
{
65+
return $this->findVariables() === $this->variables;
66+
}
67+
68+
public function serialize()
69+
{
70+
return serialize(array('prefix' => $this->prefix, 'variables' => $this->variables));
71+
}
72+
73+
public function unserialize($serialized)
74+
{
75+
$unserialized = unserialize($serialized);
76+
77+
$this->prefix = $unserialized['prefix'];
78+
$this->variables = $unserialized['variables'];
79+
}
80+
81+
private function findVariables()
82+
{
83+
$variables = array();
84+
85+
foreach ($_SERVER as $key => $value) {
86+
if (0 === strpos($key, $this->prefix)) {
87+
$variables[$key] = $value;
88+
}
89+
}
90+
91+
ksort($variables);
92+
93+
return $variables;
94+
}
95+
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\HttpFoundation\Request;
2626
use Symfony\Component\HttpFoundation\Response;
2727
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
28+
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
2829
use Symfony\Component\HttpKernel\Config\FileLocator;
2930
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
3031
use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass;
@@ -647,6 +648,7 @@ protected function buildContainer()
647648
}
648649

649650
$container->addCompilerPass(new AddClassesToCachePass($this));
651+
$container->addResource(new EnvParametersResource('SYMFONY__'));
650652

651653
return $container;
652654
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\HttpKernel\Tests\Config;
13+
14+
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
15+
16+
class EnvParametersResourceTest extends \PHPUnit_Framework_TestCase
17+
{
18+
protected $prefix = '__DUMMY_';
19+
protected $initialEnv;
20+
protected $resource;
21+
22+
protected function setUp()
23+
{
24+
$this->initialEnv = array(
25+
$this->prefix.'1' => 'foo',
26+
$this->prefix.'2' => 'bar',
27+
);
28+
29+
foreach ($this->initialEnv as $key => $value) {
30+
$_SERVER[$key] = $value;
31+
}
32+
33+
$this->resource = new EnvParametersResource($this->prefix);
34+
}
35+
36+
protected function tearDown()
37+
{
38+
foreach ($_SERVER as $key => $value) {
39+
if (0 === strpos($key, $this->prefix)) {
40+
unset($_SERVER[$key]);
41+
}
42+
}
43+
}
44+
45+
public function testGetResource()
46+
{
47+
$this->assertSame(
48+
array('prefix' => $this->prefix, 'variables' => $this->initialEnv),
49+
$this->resource->getResource(),
50+
'->getResource() returns the resource'
51+
);
52+
}
53+
54+
public function testToString()
55+
{
56+
$this->assertSame(
57+
serialize(array('prefix' => $this->prefix, 'variables' => $this->initialEnv)),
58+
(string) $this->resource
59+
);
60+
}
61+
62+
public function testIsFreshNotChanged()
63+
{
64+
$this->assertTrue(
65+
$this->resource->isFresh(time()),
66+
'->isFresh() returns true if the variables have not changed'
67+
);
68+
}
69+
70+
public function testIsFreshValueChanged()
71+
{
72+
reset($this->initialEnv);
73+
$_SERVER[key($this->initialEnv)] = 'baz';
74+
75+
$this->assertFalse(
76+
$this->resource->isFresh(time()),
77+
'->isFresh() returns false if a variable has been changed'
78+
);
79+
}
80+
81+
public function testIsFreshValueRemoved()
82+
{
83+
reset($this->initialEnv);
84+
unset($_SERVER[key($this->initialEnv)]);
85+
86+
$this->assertFalse(
87+
$this->resource->isFresh(time()),
88+
'->isFresh() returns false if a variable has been removed'
89+
);
90+
}
91+
92+
public function testIsFreshValueAdded()
93+
{
94+
$_SERVER[$this->prefix.'3'] = 'foo';
95+
96+
$this->assertFalse(
97+
$this->resource->isFresh(time()),
98+
'->isFresh() returns false if a variable has been added'
99+
);
100+
}
101+
102+
public function testSerializeUnserialize()
103+
{
104+
$this->assertEquals($this->resource, unserialize(serialize($this->resource)));
105+
}
106+
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests;
1313

14+
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
1415
use Symfony\Component\HttpKernel\Kernel;
1516
use Symfony\Component\HttpKernel\Bundle\Bundle;
1617
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -154,6 +155,35 @@ public function testClassCacheIsNotLoadedWhenKernelIsNotBooted()
154155
->method('doLoadClassCache');
155156
}
156157

158+
public function testEnvParametersResourceIsAdded()
159+
{
160+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
161+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
162+
->disableOriginalConstructor()
163+
->setMethods(array('getContainerBuilder', 'prepareContainer', 'getCacheDir', 'getLogDir'))
164+
->getMock();
165+
$kernel->expects($this->any())
166+
->method('getContainerBuilder')
167+
->will($this->returnValue($container));
168+
$kernel->expects($this->any())
169+
->method('prepareContainer')
170+
->will($this->returnValue(null));
171+
$kernel->expects($this->any())
172+
->method('getCacheDir')
173+
->will($this->returnValue(sys_get_temp_dir()));
174+
$kernel->expects($this->any())
175+
->method('getLogDir')
176+
->will($this->returnValue(sys_get_temp_dir()));
177+
$container->expects($this->once())
178+
->method('addResource')
179+
->with(new EnvParametersResource('SYMFONY__'));
180+
181+
$reflection = new \ReflectionClass(get_class($kernel));
182+
$method = $reflection->getMethod('buildContainer');
183+
$method->setAccessible(true);
184+
$method->invoke($kernel);
185+
}
186+
157187
public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce()
158188
{
159189
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')

0 commit comments

Comments
 (0)