Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scalar options merge #1003

Merged
merged 5 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## master
[v4.2.1...master](https://github.com/deployphp/deployer/compare/v4.2.1...master)

-
### Fixed
- Fixed scalar override on recursive option merge [#1003](https://github.com/deployphp/deployer/pull/1003)

## v4.2.1
[v4.2.0...v4.2.1](https://github.com/deployphp/deployer/compare/v4.2.0...v4.2.1)
Expand Down
7 changes: 4 additions & 3 deletions src/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
namespace Deployer;

use Deployer\Collection\Collection;
use Deployer\Console\Application;
use Deployer\Console\CommandEvent;
use Deployer\Console\InitCommand;
use Deployer\Console\TaskCommand;
use Deployer\Console\WorkerCommand;
use Deployer\Console\Application;
use Deployer\Server;
use Deployer\Stage\StageStrategy;
use Deployer\Task;
use Deployer\Console\TaskCommand;
use Deployer\Type\Config;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
Expand Down Expand Up @@ -169,7 +170,7 @@ public static function addDefault($name, $array)
if (!is_array($config)) {
throw new \RuntimeException("Configuration parameter `$name` isn't array.");
}
self::setDefault($name, array_merge_recursive($config, $array));
self::setDefault($name, Config::merge($config, $array));
} else {
self::setDefault($name, $array);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Server/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Deployer\Collection\Collection;
use Deployer\Deployer;
use Deployer\Type\Config;

class Environment
{
Expand Down Expand Up @@ -96,7 +97,7 @@ public function add($name, $array)
if (!is_array($config)) {
throw new \RuntimeException("Configuration parameter `$name` isn't array.");
}
$this->set($name, array_merge_recursive($config, $array));
$this->set($name, Config::merge($config, $array));
} else {
$this->set($name, $array);
}
Expand Down
50 changes: 50 additions & 0 deletions src/Type/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/* (c) Anton Medvedev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Deployer\Type;

/**
* Class Config
* @package Deployer\Type
*/
class Config
{
/**
* Recursively merge two config arrays with a specific behavior:
*
* 1. scalar values are overridden
* 2. array values are extended uniquely if all keys are numeric
* 3. all other array values are merged
*
* @param array $a
* @param array $b
* @return array
* @see http://stackoverflow.com/a/36366886/6812729
*/
public static function merge(array $original, array $override)
{
foreach ($override as $key => $value) {
if (isset($original[$key])) {
if (!is_array($original[$key])) {
// Override scalar value
$original[$key] = $value;
} elseif (array_keys($original[$key]) === range(0, count($original[$key]) - 1)) {
// Uniquely append to array with numeric keys
$original[$key] = array_unique(array_merge($original[$key], $value));
} else {
// Merge all other arrays
$original[$key] = Config::merge($original[$key], $value);
}
} else {
// Simply add new key/value
$original[$key] = $value;
}
}

return $original;
}
}
7 changes: 3 additions & 4 deletions test/src/DeployerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public function testAddDefault()
{
Deployer::setDefault('config', [
'one',
'two',
'two' => 2,
'nested' => [],
]);
Deployer::addDefault('config', [
'three',
'two' => 20,
'nested' => [
'first',
],
Expand All @@ -94,8 +94,7 @@ public function testAddDefault()

$expected = [
'one',
'two',
'three',
'two' => 20,
'nested' => [
'first',
'second',
Expand Down
7 changes: 3 additions & 4 deletions test/src/Server/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ public function testAddParams()
$env = new Environment();
$env->set('config', [
'one',
'two',
'two' => 2,
'nested' => [],
]);
$env->add('config', [
'three',
'two' => 20,
'nested' => [
'first',
],
Expand All @@ -135,8 +135,7 @@ public function testAddParams()

$expected = [
'one',
'two',
'three',
'two' => 20,
'nested' => [
'first',
'second',
Expand Down