-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.php
319 lines (267 loc) · 7.72 KB
/
environment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
use Kirby\Cms\App;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
class Environment
{
/**
* Returns the name of the currently active environment
*/
public static function active(): string
{
return F::read(__DIR__ . '/public/.environment');
}
/**
* Authenticates as the given user without login
*/
public static function auth(string $username): bool
{
if ($user = App::instance()->user($username)) {
$user->loginPasswordless();
return true;
}
throw new Exception('The user could not be found');
}
/**
* Deletes an available environment from the `environments` folder
*/
public static function delete(string $environment): void
{
if (static::exists($environment) !== true) {
throw new Exception('The environment could not be found');
}
Dir::remove(static::root($environment));
}
/**
* Returns a list of all available environments with an `active`
* marker for the current environment
*/
public static function envs(): array
{
$envs = [];
$active = static::active();
foreach (Dir::read(__DIR__ . '/environments') as $env) {
$envs[] = [
'name' => $env,
'active' => $env === $active
];
}
return $envs;
}
/**
* Returns whether an environment exists in the `environments` folder
*/
public static function exists(string $environment): bool
{
$root = static::root($environment);
return is_dir($root) === true;
}
/**
* Copies an environment into the `public` dir
* and prepares it for use
*/
public static function install(string $environment): bool
{
if (static::exists($environment) !== true) {
throw new Exception('The environment could not be found');
}
$root = static::root($environment);
$public = __DIR__ . '/public';
// remove previous environment
static::remove();
$directories = [
'assets',
'content',
'site'
];
foreach ($directories as $directory) {
if (is_dir($root . '/' . $directory) === true) {
static::copyDirectory(
$root . '/' . $directory,
$public . '/' . $directory
);
}
}
// copy global plugins
foreach (Dir::read(__DIR__ . '/plugins') as $plugin) {
static::copyDirectory(
__DIR__ . '/plugins/' . $plugin,
$public . '/site/plugins/' . $plugin
);
}
// ensure installing the admin user
static::installAdminUser();
// link the submodules to the environment directory
static::linkSubmodules($public, $root);
// store the name of the environment for switching later
F::write($public . '/.environment', $environment);
return true;
}
/**
* Copies the admin user preset from `accounts` to the `public` folder
*/
public static function installAdminUser(): bool
{
$accounts = __DIR__ . '/accounts';
$account = $accounts . '/admin';
$public = __DIR__ . '/public';
$dest = $public . '/site/accounts/admin';
if (is_dir($account) === false) {
throw new Exception('The user does not exist');
}
// remove previous versions of the admin user
Dir::remove($dest);
// create the account folder if it does not exist
Dir::make(dirname($dest));
// copy the account
static::copyDirectory($account, $dest);
return true;
}
/**
* Clears the currently active installation in the `public` folder
*/
public static function remove()
{
// remove active user session
if ($user = App::instance()->user()) {
$user->logout();
}
$public = __DIR__ . '/public';
Dir::remove($public . '/assets');
Dir::remove($public . '/content');
Dir::remove($public . '/media');
Dir::remove($public . '/site');
F::remove($public . '/.environment');
}
/**
* Returns the root to an environment in the `environments` folder
*/
public static function root(string $environment): string
{
return __DIR__ . '/environments/' . basename($environment);
}
/**
* Copies the current installation from the `public` folder to
* a new or existing environment in the `environments` folder
*
* @param string|null $environment Target environment (defaults to current one)
*/
public static function store(string|null $environment = null): bool
{
$public = __DIR__ . '/public';
$environment ??= F::read($public . '/.environment');
if (!$environment) {
return false;
}
$root = static::root($environment);
// copy the submodule files to the public dir temporarily
// so we can cleanly copy them back
// (otherwise they would be deleted in the next step)
static::unlinkSubmodules($public);
// remove the previous state
Dir::remove($root);
Dir::make($root);
$directories = [
'assets',
'content',
'site'
];
foreach ($directories as $directory) {
if (is_dir($public . '/' . $directory)) {
static::copyDirectory(
$public . '/' . $directory,
$root . '/' . $directory
);
}
}
// link the submodules to the environment directory again
static::linkSubmodules($public, $root);
// remove directories that should not be stored
Dir::remove($root . '/site/accounts/admin');
Dir::remove($root . '/site/cache');
Dir::remove($root . '/site/plugins/sandbox');
Dir::remove($root . '/site/plugins/ray');
Dir::remove($root . '/site/sessions');
// store the name of the environment for switching later
F::write($public . '/.environment', $environment);
return true;
}
/**
* Copies a directory to a new destination including
* files ignored by Kirby like `.gitignore`
*/
protected static function copyDirectory(string $dir, string $target): void
{
// create a backup of the ignore list
$ignore = Dir::$ignore;
Dir::$ignore = [];
try {
Dir::copy($dir, $target);
} finally {
Dir::$ignore = $ignore;
}
}
/**
* Returns an iterator that finds every submodule in the given directory
* unless it's a nested submodule (submodule within another submodule)
*/
protected static function findSubmodules($directory): Iterator
{
// iterate through all files and directories recursively
$directory = new RecursiveDirectoryIterator(
$directory,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS
);
$recursive = new RecursiveIteratorIterator(
$directory,
RecursiveIteratorIterator::SELF_FIRST
);
// only iterate through all directories that contain .git files
return new CallbackFilterIterator($recursive, function ($fileinfo) {
// only keep directories that have a .git file or folder themselves
if (file_exists($fileinfo->getPathname() . '/.git') !== true) {
return false;
}
// but skip every directory whose parent is already a submodule
// (nested submodule which is already covered by the parent's link)
$dir = dirname($fileinfo->getPathname());
return file_exists($dir . '/.git') === false;
});
}
/**
* Updates all `.git` files of submodules recursively to point to the new
* submodule path
*
* @param string $directory Directory to operate in
* @param string $previous Source directory to link the submodules to
*/
protected static function linkSubmodules(
string $directory,
string $previous
): void {
foreach (static::findSubmodules($directory) as $path => $fileinfo) {
// find the matching submodule path in the original location
$previousPath = str_replace($directory, $previous, $path);
// replace the submodule with a symlink to the environment
Dir::remove($path);
symlink($previousPath, $path);
}
}
/**
* Replaces all links to submodules with the actual submodule worktrees
*/
protected static function unlinkSubmodules(string $directory): void
{
foreach (static::findSubmodules($directory) as $path => $fileinfo) {
if ($fileinfo->isLink() !== true) {
continue;
}
$source = $fileinfo->getLinkTarget();
if (is_string($source) !== true) {
continue;
}
unlink($path);
static::copyDirectory($source, $path);
}
}
}