6
6
use Jackiedo \DotenvEditor \Exceptions \InvalidValueException ;
7
7
use Jackiedo \DotenvEditor \Facades \DotenvEditor ;
8
8
9
+ /**
10
+ * Class SetEnvCommand
11
+ * @package Rabol\LaravelSetupLocalDev\Console\Commands
12
+ */
9
13
class SetEnvCommand extends Command
10
14
{
11
15
/**
@@ -39,33 +43,14 @@ public function __construct()
39
43
*/
40
44
public function handle ()
41
45
{
42
-
43
- if (!file_exists (base_path () . '/.env ' ))
44
- {
45
- copy ('.env.example ' , '.env ' );
46
- }
46
+ $ this ->validateOrCopyEnvFile ();
47
+ $ default_env_vars_file = $ this ->validateOrUserEnvFile ();
47
48
48
49
$ this ->info ('Installing default .env vars. ' );
49
-
50
- if ($ this ->option ('file ' ) !== null )
51
- {
52
- $ default_env_vars_file = $ this ->option ('file ' );
53
- }
54
- else
55
- {
56
- $ default_env_vars_file = get_home_directory ();
57
- $ default_env_vars_file .= '/.default_vars.env ' ;
58
- }
59
-
60
- if (!file_exists ($ default_env_vars_file ))
61
- {
62
- $ this ->error ('File: \'' . $ default_env_vars_file . '\' does not exists. ' );
63
- exit ();
64
- }
65
-
66
50
$ this ->info ('Reading from: ' . $ default_env_vars_file );
67
51
68
52
$ user_vars = DotenvEditor::load ($ default_env_vars_file );
53
+
69
54
try {
70
55
$ user_keys = $ user_vars ->getKeys ();
71
56
}
@@ -76,7 +61,6 @@ public function handle()
76
61
exit ();
77
62
}
78
63
79
-
80
64
// Load the .env
81
65
try {
82
66
$ app_env = DotenvEditor::load ();
@@ -87,15 +71,14 @@ public function handle()
87
71
exit ();
88
72
}
89
73
90
-
91
74
// Now apply the user vars
92
75
foreach ($ user_keys as $ user_key => $ user_value )
93
76
{
94
77
$ newValue = $ user_value ['value ' ];
95
78
96
79
if ($ newValue == '[ASK_FOR_VALUE] ' )
97
80
{
98
- $ newValue = $ this ->ask ("Please ente value for ' $ user_key' " );
81
+ $ newValue = $ this ->ask ("Please enter a value for ' $ user_key' " );
99
82
}
100
83
101
84
if ($ app_env ->keyExists ($ user_key ))
@@ -116,4 +99,47 @@ public function handle()
116
99
$ this ->call ('config:clear ' );
117
100
$ this ->info ('Done! ' );
118
101
}
102
+
103
+ /**
104
+ *
105
+ */
106
+ public function validateOrCopyEnvFile ()
107
+ {
108
+ if (!file_exists (base_path () . '/.env ' ))
109
+ {
110
+ if (!file_exists ('.env.example ' ))
111
+ {
112
+ $ this ->error ('.env or .evn.example does not exists, please create .env and try again ' );
113
+ exit ();
114
+
115
+ }
116
+ copy ('.env.example ' , '.env ' );
117
+ }
118
+ }
119
+
120
+ /**
121
+ * @return string
122
+ */
123
+ public function validateOrUserEnvFile (): string
124
+ {
125
+ $ default_env_vars_file = "" ;
126
+
127
+ if ($ this ->option ('file ' ) !== null )
128
+ {
129
+ $ default_env_vars_file = $ this ->option ('file ' );
130
+ }
131
+ else
132
+ {
133
+ $ default_env_vars_file = get_home_directory ();
134
+ $ default_env_vars_file .= '/.default_vars.env ' ;
135
+ }
136
+
137
+ if (!file_exists ($ default_env_vars_file ))
138
+ {
139
+ $ this ->error ('File: \'' . $ default_env_vars_file . '\' does not exists. ' );
140
+ exit ();
141
+ }
142
+
143
+ return $ default_env_vars_file ;
144
+ }
119
145
}
0 commit comments