|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer\Support; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Flatten array |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $array |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
function array_flatten(array $array) |
|
17
|
|
|
{ |
|
18
|
6 |
|
$flatten = []; |
|
19
|
|
|
array_walk_recursive($array, function ($value) use (&$flatten) { |
|
20
|
6 |
|
$flatten[] = $value; |
|
21
|
6 |
|
}); |
|
22
|
6 |
|
return $flatten; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Recursively merge two config arrays with a specific behavior: |
|
27
|
|
|
* |
|
28
|
|
|
* 1. scalar values are overridden |
|
29
|
|
|
* 2. array values are extended uniquely if all keys are numeric |
|
30
|
|
|
* 3. all other array values are merged |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $original |
|
33
|
|
|
* @param array $override |
|
34
|
|
|
* @return array |
|
35
|
|
|
* @see http://stackoverflow.com/a/36366886/6812729 |
|
36
|
|
|
*/ |
|
37
|
|
|
function array_merge_alternate(array $original, array $override) |
|
38
|
|
|
{ |
|
39
|
6 |
|
foreach ($override as $key => $value) { |
|
40
|
6 |
|
if (isset($original[$key])) { |
|
41
|
6 |
|
if (!is_array($original[$key])) { |
|
42
|
6 |
|
if (is_numeric($key)) { |
|
43
|
|
|
// Append scalar value |
|
44
|
6 |
|
$original[] = $value; |
|
45
|
|
|
} else { |
|
46
|
|
|
// Override scalar value |
|
47
|
6 |
|
$original[$key] = $value; |
|
48
|
|
|
} |
|
49
|
2 |
|
} elseif (array_keys($original[$key]) === range(0, count($original[$key]) - 1)) { |
|
50
|
|
|
// Uniquely append to array with numeric keys |
|
51
|
2 |
|
$original[$key] = array_unique(array_merge($original[$key], $value)); |
|
52
|
|
|
} else { |
|
53
|
|
|
// Merge all other arrays |
|
54
|
6 |
|
$original[$key] = array_merge_alternate($original[$key], $value); |
|
55
|
|
|
} |
|
56
|
|
|
} else { |
|
57
|
|
|
// Simply add new key/value |
|
58
|
2 |
|
$original[$key] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
return $original; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Determines if the given string contains the given value. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $haystack |
|
69
|
|
|
* @param string $needle |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
function str_contains(string $haystack, string $needle) |
|
73
|
|
|
{ |
|
74
|
|
|
return strpos($haystack, $needle) !== false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Take array of key/value and create string of it. |
|
79
|
|
|
* |
|
80
|
|
|
* This function used for create environment string. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $array |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
function array_to_string(array $array): string |
|
87
|
|
|
{ |
|
88
|
|
|
return implode(' ', array_map( |
|
89
|
|
|
function ($key, $value) { |
|
90
|
|
|
return sprintf("%s='%s'", $key, $value); |
|
91
|
|
|
}, |
|
92
|
|
|
array_keys($array), |
|
93
|
|
|
$array |
|
94
|
|
|
)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if var is closure. |
|
99
|
|
|
* |
|
100
|
|
|
* @param $var |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
function is_closure($var) |
|
104
|
|
|
{ |
|
105
|
29 |
|
return is_object($var) && ($var instanceof \Closure); |
|
106
|
|
|
} |
|
107
|
|
|
|