-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetEnv.php
116 lines (103 loc) · 3.17 KB
/
SetEnv.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
<?php
namespace SoftBricks\CLI;
/**
* Class SetEnv
* Helps setting params into .EVN files
* @package SoftBricks\CLI
*/
class SetEnv extends ConsoleApp
{
/**
* Returns full filepath of target .env file
*
* @return string|null
*/
private function getEnvFile()
{
foreach(scandir(getcwd()) as $file) {
if (is_file($file) && strtolower($file) === '.env') {
return $file;
}
}
return null;
}
/**
* Prints error message and quits script execution with error code
*
* @param $text
*/
private function printError($text)
{
$this->_print($text);
$this->println(' Failure', Color::$RED);
exit(1);
}
/**
* Prints success message and quites script execution with success code
*
* @param $text
*/
private function printSuccess($text)
{
$this->_print($text);
$this->println(' Ok', Color::$LIGHTGREEN);
exit(0);
}
/**
* Rewrites .env file with given config
*
* @param $config
* @param $filePath
* @return bool
*/
private function writeEnvFile($envContent, $filePath)
{
$content = "";
// we want a cleanly sorted env file in the end
ksort($envContent);
// we add configs without that are outside of categories first
foreach($envContent as $confKey => $config) {
if (!is_array($config)) {
$content .= "${confKey}=${config}\n";
}
}
// we add configs inside of categories second
foreach($envContent as $confKey => $config) {
if (is_array($config)) {
$content .= "\n[${confKey}]\n";
foreach ($config as $key => $value) {
$content .= "${key}=${value}\n";
}
}
}
return file_put_contents($filePath, $content) !== false;
}
protected function run()
{
// try to find ENV file in local directory
$envFileName = $this->getEnvFile();
$envPath = getcwd() . '/' . $envFileName;
if ($envFileName === null) {
$this->printError('Could not find .env file in current directory.');
}
// we analyze passed in arguments to check if we got everything we need
$key = $this->getArg('--key');
$value = $this->getArg('--value');
$category = $this->getArg('--category');
if ($key === null) $this->printError('no key argument provided');
if ($value === null) $this->printError('no value argument provided');
// we parse current .env file and add the config we need to add
$envContent = parse_ini_file($envPath, true);
if ($category !== null) {
$envContent[$category][$key] = $value;
} else {
$envContent[$key] = $value;
}
// write back changed config
if (!$this->writeEnvFile($envContent, $envPath)) {
$this->printError('Could not update '.$envFileName);
}
// script executed successfully, so we are setting a success status
$this->printSuccess($envFileName . ' updated');
}
}