Skip to content

Commit

Permalink
Merge pull request #288 from oanhnn/bugfix/fix-feature-protected-env-…
Browse files Browse the repository at this point in the history
…params

Fix bug of pull request #276: add feature protected env params
  • Loading branch information
Anton Medvedev committed May 24, 2015
2 parents e168c81 + be28f82 commit 5429b07
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
21 changes: 8 additions & 13 deletions src/Server/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,15 @@ public function setAsProtected($name, $value)
*/
private function checkIfNameIsProtected($name)
{
$nameArray = strpos($name, '.') !== false ? explode('.', $name) : [$name];

// Checks every level for protection
foreach ($nameArray as $subName) {
$dotName = !isset($dotName) ? $subName : "$dotName.$subName";
if (in_array($dotName, $this->protectedNames)) {
throw new \RuntimeException("The parameter `$name` cannot be set, because " . ($name === $dotName ? "it's" : "`$dotName` is" ) . " protected.");
}
}

// Even if not one of $name's levels is protected, protected env params
// under it could still exist, so let's check for those too.
$length = strlen($name);

foreach ($this->protectedNames as $protectedName) {
if (strpos($protectedName, $name) !== false) {
$len = strlen($protectedName);
if ($name === $protectedName) {
throw new \RuntimeException("The parameter `$name` cannot be set, because it's protected.");
} elseif ($len < $length && '.' === $name[$len] && 0 === strpos($name, $protectedName)) {
throw new \RuntimeException("The parameter `$name` cannot be set, because `$protectedName` is protected.");
} elseif ($len > $length && '.' === $protectedName[$length] && 0 === strpos($protectedName, $name)) {
throw new \RuntimeException("The parameter `$name` could not be set, because a protected parameter named `$protectedName` already exists.");
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/Type/DotArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ public function offsetSet($key, $value)
$scope = &$scope[$parts[$i]];
}
if (is_array($value)) {
$tmp = new static();
foreach ($value as $k => $v) {
$tmp->offsetSet($k, $v);
}
$tmp = new static($value);
$scope[$parts[$i]] = $tmp->toArray();
} else {
$scope[$parts[$i]] = $value;
Expand Down
4 changes: 4 additions & 0 deletions test/src/Server/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function testContainingProtectedParam()
$env->set('not_protected', []);
$env->setAsProtected('not_protected.protected', 'value');

// Ok
$env->setAsProtected('protected', 'value');
$env->setAsProtected('not', 'value');

// Since `not_protected.protected` is a protected parameter, overwriting
// the whole `not_protected` parameter is not allowed.
$env->setAsProtected('not_protected', 'value');
Expand Down

0 comments on commit 5429b07

Please sign in to comment.