diff --git a/src/Server/Environment.php b/src/Server/Environment.php index 85f0695d..5429ebf3 100644 --- a/src/Server/Environment.php +++ b/src/Server/Environment.php @@ -25,7 +25,14 @@ class Environment * @var \Deployer\Type\DotArray */ private $values = null; - + + /** + * Values represented by their keys here are protected, and cannot be + * changed by calling the `set` method. + * @var array + */ + private $protectedValueKeys = []; + /** * Constructor */ @@ -37,10 +44,19 @@ class Environment /** * @param string $name * @param bool|int|string|array $value + * @param bool $isProtected */ - public function set($name, $value) + public function set($name, $value, $isProtected = false) { + if (in_array($name, $this->protectedValueKeys)) { + throw new \RuntimeException("The environment parameter `$name` has already been set, and is protected from changes."); + } + $this->values[$name] = $value; + + if ($isProtected === true) { + $this->protectedValueKeys[] = $name; + } } /** diff --git a/test/src/Server/EnvironmentTest.php b/test/src/Server/EnvironmentTest.php index 333df66a..d35b2105 100644 --- a/test/src/Server/EnvironmentTest.php +++ b/test/src/Server/EnvironmentTest.php @@ -4,7 +4,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - + namespace Deployer\Server; class EnvironmentTest extends \PHPUnit_Framework_TestCase @@ -30,7 +30,7 @@ class EnvironmentTest extends \PHPUnit_Framework_TestCase $env->set('string', 'value'); $env->set('array', [1, 'two']); $env->set('parse', 'is {{int}}'); - + $this->assertEquals(42, $env->get('int')); $this->assertEquals('value', $env->get('string')); $this->assertEquals([1, 'two'], $env->get('array')); @@ -45,4 +45,14 @@ class EnvironmentTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('RuntimeException', 'Environment parameter `so` does not exists.'); $env->get('so'); } + + public function testProtectedParameters() + { + $env = new Environment(); + + $env->set('protected', 'protected-value', true); + + $this->setExpectedException('\RuntimeException', 'The environment parameter `protected` has already been set, and is protected from changes.'); + $env->set('protected', 'some-other-value'); + } }