mirror of
https://github.com/deployphp/deployer.git
synced 2025-02-24 01:02:24 +01:00
Added the possibility of 'protected' env parameters. (+test).
This commit is contained in:
parent
3e8f6ea361
commit
24d680b15b
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user