-
Notifications
You must be signed in to change notification settings - Fork 16
/
web.php
78 lines (64 loc) · 2.41 KB
/
web.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
<?php
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
Route::redirect('/', 'https://laravel.com/docs', 302);
Route::get('/{name}', function (Request $request, $name) {
$availableServices = [
'mysql',
'pgsql',
'mariadb',
'redis',
'memcached',
'meilisearch',
'typesense',
'minio',
'mailpit',
'selenium',
'soketi',
];
$php = $request->query('php', '83');
$with = array_unique(explode(',', $request->query('with', 'mysql,redis,meilisearch,mailpit,selenium')));
try {
Validator::validate(
[
'name' => $name,
'php' => $php,
'with' => $with,
],
[
'name' => 'string|alpha_dash',
'php' => ['string', Rule::in(['74', '80', '81', '82', '83'])],
'with' => 'array',
'with.*' => [
'required',
'string',
count($with) === 1 && in_array('none', $with) ? Rule::in(['none']) : Rule::in($availableServices)
],
]
);
} catch (ValidationException $e) {
$errors = Arr::undot($e->errors());
if (array_key_exists('name', $errors)) {
return response('Invalid site name. Please only use alpha-numeric characters, dashes, and underscores.', 400);
}
if (array_key_exists('php', $errors)) {
return response('Invalid PHP version. Please specify a supported version (74, 80, 81, 82 or 83).', 400);
}
if (array_key_exists('with', $errors)) {
return response('Invalid service name. Please provide one or more of the supported services ('.implode(', ', $availableServices).') or "none".', 400);
}
}
$services = implode(' ', $with);
$with = implode(',', $with);
$devcontainer = $request->has('devcontainer') ? '--devcontainer' : '';
$script = str_replace(
['{{ php }}', '{{ name }}', '{{ with }}', '{{ devcontainer }}', '{{ services }}'],
[$php, $name, $with, $devcontainer, $services],
file_get_contents(resource_path('scripts/php.sh'))
);
return response($script, 200, ['Content-Type' => 'text/plain']);
});