forked from kuaifan/dootask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·89 lines (82 loc) · 2.67 KB
/
run
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
#!/usr/bin/env php
<?php
/**
* Class runLoader
*/
class runLoader
{
public function randString($length = 16)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$passwordstr = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$passwordstr .= $chars[mt_rand(0, $max)];
}
return $passwordstr;
}
function getEnv(string $key)
{
if (empty($key) || !is_string($key)) {
return '';
}
$envPath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '.env';
if (!file_exists($envPath)) {
return false;
}
$envContent = file_get_contents($envPath);
preg_match_all("/^" . $key . "\s*=\s*(.*?)$/m", $envContent, $matchs);
return $matchs[1] ?: '';
}
function modifyEnv(array $data)
{
if (empty($data) || !is_array($data)) {
return false;
}
$envPath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '.env';
if (!file_exists($envPath)) {
return false;
}
$envContent = file_get_contents($envPath);
foreach ($data as $key => $val) {
$envContent = preg_replace("/^" . $key . "\s*=\s*(.*?)$/m", $key . "=" . $val, $envContent);
}
file_put_contents($envPath, $envContent);
return true;
}
function modifyMode($type)
{
$filePath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '/docker/php/php.conf';
if (!file_exists($filePath)) {
return false;
}
$envContent = file_get_contents($filePath);
$envContent = str_replace("#command=php bin/laravels start -i", "command=php bin/laravels start -i", $envContent);
$envContent = str_replace("#command=./bin/inotify ./app", "command=./bin/inotify ./app", $envContent);
if ($type == "dev") {
$envContent = str_replace("command=php bin/laravels start -i", "#command=php bin/laravels start -i", $envContent);
$this->modifyEnv([
'APP_DEBUG' => 'true'
]);
} else {
$envContent = str_replace("command=./bin/inotify ./app", "#command=./bin/inotify ./app", $envContent);
$this->modifyEnv([
'APP_DEBUG' => 'false'
]);
}
file_put_contents($filePath, $envContent);
return true;
}
}
$array = getopt('', ['port:', 'mode:']);
$loader = new runLoader();
if (isset($array['mode'])) {
$loader->modifyMode($array['mode']);
}
$data = [];
if (isset($array['port'])) {
$data['APP_PORT'] = $array['port'];
}
if ($data) {
$loader->modifyEnv($data);
}