1
|
|
|
<?php declare(strict_types=1); |
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\Configuration; |
9
|
|
|
|
10
|
|
|
use Deployer\Collection\Collection; |
11
|
|
|
use Deployer\Deployer; |
12
|
|
|
use Deployer\Exception\ConfigurationException; |
13
|
|
|
use function Deployer\Support\array_merge_alternate; |
14
|
|
|
|
15
|
|
|
class Configuration implements \ArrayAccess |
16
|
|
|
{ |
17
|
|
|
public $parent; |
18
|
|
|
private $collection; |
19
|
|
|
|
20
|
33 |
|
public function __construct(Configuration $parent = null) |
21
|
|
|
{ |
22
|
33 |
|
$this->parent = $parent; |
23
|
33 |
|
$this->collection = new Collection(); |
24
|
33 |
|
} |
25
|
|
|
|
26
|
|
|
public function getCollection(): Collection |
27
|
|
|
{ |
28
|
|
|
return $this->collection; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setCollection(Collection $collection) |
32
|
|
|
{ |
33
|
|
|
$this->collection = $collection; |
34
|
|
|
} |
35
|
|
|
|
36
|
33 |
|
public function set(string $name, $value) |
37
|
|
|
{ |
38
|
33 |
|
$this->collection[$name] = $value; |
39
|
33 |
|
} |
40
|
|
|
|
41
|
5 |
|
public function add(string $name, array $array) |
42
|
|
|
{ |
43
|
5 |
|
if ($this->has($name)) { |
44
|
5 |
|
$config = $this->get($name); |
45
|
5 |
|
if (!is_array($config)) { |
46
|
2 |
|
throw new ConfigurationException("Configuration parameter `$name` isn't array."); |
47
|
|
|
} |
48
|
3 |
|
$this->set($name, array_merge_alternate($config, $array)); |
49
|
|
|
} else { |
50
|
|
|
$this->set($name, $array); |
51
|
|
|
} |
52
|
3 |
|
} |
53
|
|
|
|
54
|
23 |
|
public function get(string $name, $default = null) |
55
|
|
|
{ |
56
|
23 |
|
if ($this->collection->has($name)) { |
57
|
22 |
|
if ($this->isClosure($this->collection[$name])) { |
58
|
1 |
|
$value = $this->collection[$name] = call_user_func($this->collection[$name]); |
59
|
|
|
} else { |
60
|
22 |
|
$value = $this->collection[$name]; |
61
|
|
|
} |
62
|
6 |
|
} else if ($this->parent && $this->parent->has($name)) { |
63
|
1 |
|
$value = $this->collection[$name] = $this->parent->get($name, $default); |
64
|
|
|
} else { |
65
|
5 |
|
if (null === $default) { |
66
|
1 |
|
throw new ConfigurationException("Configuration parameter `$name` does not exist."); |
67
|
|
|
} else { |
68
|
5 |
|
$value = $default; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
23 |
|
return $this->parse($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
public function has(string $name): bool |
76
|
|
|
{ |
77
|
8 |
|
return $this->collection->has($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
23 |
|
public function parse($value) |
81
|
|
|
{ |
82
|
23 |
|
if (is_string($value)) { |
83
|
18 |
|
return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
14 |
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function offsetExists($offset) |
90
|
|
|
{ |
91
|
1 |
|
return $this->has($offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public function offsetGet($offset) |
95
|
|
|
{ |
96
|
3 |
|
return $this->get($offset); |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
public function offsetSet($offset, $value) |
100
|
|
|
{ |
101
|
10 |
|
$this->set($offset, $value); |
102
|
10 |
|
} |
103
|
|
|
|
104
|
|
|
public function offsetUnset($offset) |
105
|
|
|
{ |
106
|
|
|
unset($this->collection[$offset]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function persist() |
110
|
|
|
{ |
111
|
|
|
$values = []; |
112
|
|
|
if ($this->parent !== null) { |
113
|
|
|
$values = $this->parent->persist(); |
114
|
|
|
} |
115
|
|
|
foreach ($this->collection as $key => $value) { |
116
|
|
|
if ($this->isClosure($value)) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
$values[$key] = $value; |
120
|
|
|
} |
121
|
|
|
return $values; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
private function parseCallback(array $matches) |
125
|
|
|
{ |
126
|
3 |
|
return isset($matches[1]) ? $this->get($matches[1]) : null; |
127
|
|
|
} |
128
|
|
|
|
129
|
22 |
|
private function isClosure($var) |
130
|
|
|
{ |
131
|
22 |
|
return is_object($var) && ($var instanceof \Closure); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|