|
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\Importer; |
|
12
|
|
|
|
|
13
|
|
|
use Deployer\Exception\ConfigurationException; |
|
14
|
|
|
use Deployer\Exception\Exception; |
|
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
16
|
|
|
|
|
17
|
|
|
use function array_filter; |
|
18
|
|
|
use function array_keys; |
|
19
|
|
|
use function Deployer\after; |
|
20
|
|
|
use function Deployer\before; |
|
21
|
|
|
use function Deployer\cd; |
|
22
|
|
|
use function Deployer\download; |
|
23
|
|
|
use function Deployer\host; |
|
24
|
|
|
use function Deployer\localhost; |
|
25
|
|
|
use function Deployer\run; |
|
26
|
|
|
use function Deployer\runLocally; |
|
27
|
|
|
use function Deployer\set; |
|
28
|
|
|
use function Deployer\Support\find_line_number; |
|
29
|
|
|
use function Deployer\task; |
|
30
|
|
|
use function Deployer\upload; |
|
31
|
|
|
|
|
32
|
|
|
use const ARRAY_FILTER_USE_KEY; |
|
33
|
|
|
|
|
34
|
|
|
class Importer |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $recipeFilename; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $recipeSource; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string|string[] $paths |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function import($paths) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!is_array($paths)) { |
|
51
|
|
|
$paths = [$paths]; |
|
52
|
|
|
} |
|
53
|
|
|
foreach ($paths as $path) { |
|
54
|
|
|
if (preg_match('/\.php$/i', $path)) { |
|
55
|
|
|
// Prevent variable leak into deploy.php file |
|
56
|
|
|
call_user_func(function () use ($path) { |
|
57
|
|
|
// Reorder autoload stack |
|
58
|
|
|
$originStack = spl_autoload_functions(); |
|
59
|
|
|
|
|
60
|
|
|
require $path; |
|
61
|
|
|
|
|
62
|
|
|
$newStack = spl_autoload_functions(); |
|
63
|
|
|
if ($originStack[0] !== $newStack[0]) { |
|
64
|
|
|
foreach (array_reverse($originStack) as $loader) { |
|
65
|
|
|
spl_autoload_unregister($loader); |
|
66
|
|
|
spl_autoload_register($loader, true, true); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
}); |
|
70
|
|
|
} elseif (preg_match('/\.ya?ml$/i', $path)) { |
|
71
|
|
|
self::$recipeFilename = basename($path); |
|
72
|
|
|
self::$recipeSource = file_get_contents($path, true); |
|
73
|
|
|
|
|
74
|
|
|
$root = array_filter(Yaml::parse(self::$recipeSource), static function (string $key) { |
|
75
|
|
|
return !str_starts_with($key, '.'); |
|
76
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
77
|
|
|
|
|
78
|
|
|
foreach (array_keys($root) as $key) { |
|
79
|
|
|
static::$key($root[$key]); |
|
80
|
|
|
} |
|
81
|
|
|
} else { |
|
82
|
|
|
throw new Exception("Unknown file format: $path\nOnly .php and .yaml supported."); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected static function hosts(array $hosts) |
|
88
|
|
|
{ |
|
89
|
|
|
foreach ($hosts as $alias => $config) { |
|
90
|
|
|
if ($config['local'] ?? false) { |
|
91
|
|
|
$host = localhost($alias); |
|
92
|
|
|
} else { |
|
93
|
|
|
$host = host($alias); |
|
94
|
|
|
} |
|
95
|
|
|
if (is_array($config)) { |
|
96
|
|
|
foreach ($config as $key => $value) { |
|
97
|
|
|
$host->set($key, $value); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected static function config(array $config) |
|
104
|
|
|
{ |
|
105
|
|
|
foreach ($config as $key => $value) { |
|
106
|
|
|
set($key, $value); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected static function tasks(array $tasks) |
|
111
|
|
|
{ |
|
112
|
|
|
$buildTask = function ($name, $steps) { |
|
113
|
|
|
$body = function () {}; |
|
114
|
|
|
$task = task($name, $body); |
|
115
|
|
|
|
|
116
|
|
|
foreach ($steps as $step) { |
|
117
|
|
|
$buildStep = function ($step) use (&$body, $task) { |
|
118
|
|
|
extract($step); |
|
119
|
|
|
|
|
120
|
|
|
if (isset($cd)) { |
|
121
|
|
|
$prev = $body; |
|
122
|
|
|
$body = function () use ($cd, $prev) { |
|
123
|
|
|
$prev(); |
|
124
|
|
|
cd($cd); |
|
125
|
|
|
}; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (isset($run)) { |
|
129
|
|
|
$has = 'run'; |
|
130
|
|
|
$prev = $body; |
|
131
|
|
|
$body = function () use ($run, $prev) { |
|
132
|
|
|
$prev(); |
|
133
|
|
|
try { |
|
134
|
|
|
run($run); |
|
135
|
|
|
} catch (Exception $e) { |
|
136
|
|
|
$e->setTaskFilename(self::$recipeFilename); |
|
137
|
|
|
$e->setTaskLineNumber(find_line_number(self::$recipeSource, $run)); |
|
138
|
|
|
throw $e; |
|
139
|
|
|
} |
|
140
|
|
|
}; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (isset($run_locally)) { |
|
144
|
|
|
if (isset($has)) { |
|
145
|
|
|
throw new ConfigurationException("Task step can not have both $has and run_locally."); |
|
146
|
|
|
} |
|
147
|
|
|
$has = 'run_locally'; |
|
148
|
|
|
$prev = $body; |
|
149
|
|
|
$body = function () use ($run_locally, $prev) { |
|
150
|
|
|
$prev(); |
|
151
|
|
|
try { |
|
152
|
|
|
runLocally($run_locally); |
|
153
|
|
|
} catch (Exception $e) { |
|
154
|
|
|
$e->setTaskFilename(self::$recipeFilename); |
|
155
|
|
|
$e->setTaskLineNumber(find_line_number(self::$recipeSource, $run_locally)); |
|
156
|
|
|
throw $e; |
|
157
|
|
|
} |
|
158
|
|
|
}; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (isset($upload)) { |
|
162
|
|
|
if (isset($has)) { |
|
163
|
|
|
throw new ConfigurationException("Task step can not have both $has and upload."); |
|
164
|
|
|
} |
|
165
|
|
|
$has = 'upload'; |
|
166
|
|
|
$prev = $body; |
|
167
|
|
|
$body = function () use ($upload, $prev) { |
|
168
|
|
|
$prev(); |
|
169
|
|
|
upload($upload['src'], $upload['dest']); |
|
170
|
|
|
}; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (isset($download)) { |
|
174
|
|
|
if (isset($has)) { |
|
175
|
|
|
throw new ConfigurationException("Task step can not have both $has and download."); |
|
176
|
|
|
} |
|
177
|
|
|
$has = 'download'; |
|
178
|
|
|
$prev = $body; |
|
179
|
|
|
$body = function () use ($download, $prev) { |
|
180
|
|
|
$prev(); |
|
181
|
|
|
download($download['src'], $download['dest']); |
|
182
|
|
|
}; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$methods = [ |
|
186
|
|
|
'desc', |
|
187
|
|
|
'once', |
|
188
|
|
|
'hidden', |
|
189
|
|
|
'limit', |
|
190
|
|
|
'select', |
|
191
|
|
|
]; |
|
192
|
|
|
foreach ($methods as $method) { |
|
193
|
|
|
if (isset($$method)) { |
|
194
|
|
|
$task->$method($$method); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
}; |
|
198
|
|
|
|
|
199
|
|
|
$buildStep($step); |
|
200
|
|
|
$task->setCallback($body); |
|
201
|
|
|
} |
|
202
|
|
|
}; |
|
203
|
|
|
|
|
204
|
|
|
foreach ($tasks as $name => $config) { |
|
205
|
|
|
foreach ($config as $key => $value) { |
|
206
|
|
|
if (!is_int($key) || !is_string($value)) { |
|
207
|
|
|
goto not_a_group_task; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// Create a group task. |
|
212
|
|
|
task($name, $config); |
|
213
|
|
|
continue; |
|
214
|
|
|
|
|
215
|
|
|
not_a_group_task: |
|
216
|
|
|
$buildTask($name, $config); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
protected static function after(array $after) |
|
221
|
|
|
{ |
|
222
|
|
|
foreach ($after as $key => $value) { |
|
223
|
|
|
if (is_array($value)) { |
|
224
|
|
|
foreach (array_reverse($value) as $v) { |
|
225
|
|
|
after($key, $v); |
|
226
|
|
|
} |
|
227
|
|
|
} else { |
|
228
|
|
|
after($key, $value); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected static function before(array $before) |
|
234
|
|
|
{ |
|
235
|
|
|
foreach ($before as $key => $value) { |
|
236
|
|
|
if (is_array($value)) { |
|
237
|
|
|
foreach (array_reverse($value) as $v) { |
|
238
|
|
|
before($key, $v); |
|
239
|
|
|
} |
|
240
|
|
|
} else { |
|
241
|
|
|
before($key, $value); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|