1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Selector; |
12
|
|
|
|
13
|
|
|
use Deployer\Host\Host; |
14
|
|
|
use Deployer\Host\HostCollection; |
15
|
|
|
|
16
|
|
|
use function Deployer\Support\array_all; |
17
|
|
|
|
18
|
11 |
|
class Selector |
19
|
|
|
{ |
20
|
11 |
|
/** |
21
|
11 |
|
* @var HostCollection |
22
|
|
|
*/ |
23
|
|
|
private $hosts; |
24
|
|
|
|
25
|
|
|
public function __construct(HostCollection $hosts) |
26
|
|
|
{ |
27
|
11 |
|
$this->hosts = $hosts; |
28
|
|
|
} |
29
|
11 |
|
|
30
|
|
|
/** |
31
|
11 |
|
* @return Host[] |
32
|
11 |
|
*/ |
33
|
11 |
|
public function select(string $selectExpression) |
34
|
11 |
|
{ |
35
|
|
|
$conditions = self::parse($selectExpression); |
36
|
|
|
|
37
|
|
|
$hosts = []; |
38
|
11 |
|
foreach ($this->hosts as $host) { |
39
|
|
|
if (self::apply($conditions, $host)) { |
40
|
|
|
$hosts[] = $host; |
41
|
13 |
|
} |
42
|
|
|
} |
43
|
13 |
|
|
44
|
|
|
return $hosts; |
45
|
13 |
|
} |
46
|
13 |
|
|
47
|
13 |
|
public static function apply(?array $conditions, Host $host): bool |
48
|
13 |
|
{ |
49
|
13 |
|
if (empty($conditions)) { |
50
|
|
|
return true; |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
$labels = $host->get('labels', []); |
54
|
1 |
|
$labels['alias'] = $host->getAlias(); |
55
|
|
|
$labels['true'] = 'true'; |
56
|
|
|
$isTrue = function ($value) { |
57
|
|
|
return $value; |
58
|
|
|
}; |
59
|
13 |
|
|
60
|
13 |
|
foreach ($conditions as $hmm) { |
61
|
|
|
$ok = []; |
62
|
|
|
foreach ($hmm as [$op, $var, $value]) { |
63
|
1 |
|
if (is_array($value)) { |
64
|
|
|
$orOk = []; |
65
|
1 |
|
foreach ($value as $val) { |
66
|
1 |
|
$orOk[] = self::compare($op, $labels[$var] ?? null, $val); |
67
|
|
|
} |
68
|
1 |
|
$ok[] = count(array_filter($orOk, $isTrue)) > 0; |
69
|
1 |
|
} else { |
70
|
|
|
$ok[] = self::compare($op, $labels[$var] ?? null, $value); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
if (count($ok) > 0 && array_all($ok, $isTrue)) { |
74
|
20 |
|
return true; |
75
|
|
|
} |
76
|
20 |
|
} |
77
|
|
|
return false; |
78
|
20 |
|
} |
79
|
20 |
|
|
80
|
20 |
|
/** |
81
|
20 |
|
* @param string|string[] $a |
82
|
20 |
|
*/ |
83
|
20 |
|
private static function compare(string $op, $a, ?string $b): bool |
84
|
|
|
{ |
85
|
1 |
|
$matchFunction = function ($a, ?string $b) { |
86
|
1 |
|
foreach ((array) $a as $item) { |
87
|
|
|
if ($item === $b) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
20 |
|
|
92
|
|
|
return false; |
93
|
|
|
}; |
94
|
|
|
|
95
|
|
|
if ($op === '=') { |
96
|
|
|
return $matchFunction($a, $b); |
97
|
|
|
} |
98
|
|
|
if ($op === '!=') { |
99
|
|
|
return !$matchFunction($a, $b); |
100
|
|
|
} |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function parse(string $expression): array |
105
|
|
|
{ |
106
|
|
|
$all = []; |
107
|
|
|
foreach (explode(',', $expression) as $sub) { |
108
|
|
|
$conditions = []; |
109
|
|
|
foreach (explode('&', $sub) as $part) { |
110
|
|
|
$part = trim($part); |
111
|
|
|
if ($part === 'all') { |
112
|
|
|
$conditions[] = ['=', 'true', 'true']; |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
if (preg_match('/(?<var>.+?)(?<op>!?=)(?<value>.+)/', $part, $match)) { |
116
|
|
|
$values = array_map('trim', explode('|', trim($match['value']))); |
117
|
|
|
$conditions[] = [$match['op'], trim($match['var']), $values]; |
118
|
|
|
} else { |
119
|
|
|
$conditions[] = ['=', 'alias', trim($part)]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
$all[] = $conditions; |
123
|
|
|
} |
124
|
|
|
return $all; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|