66use Jackiedo \DotenvEditor \Exceptions \InvalidValueException ;
77use Jackiedo \DotenvEditor \Facades \DotenvEditor ;
88
9+ /**
10+ * Class SetEnvCommand
11+ * @package Rabol\LaravelSetupLocalDev\Console\Commands
12+ */
913class SetEnvCommand extends Command
1014{
1115 /**
@@ -39,33 +43,14 @@ public function __construct()
3943 */
4044 public function handle ()
4145 {
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 ();
4748
4849 $ 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-
6650 $ this ->info ('Reading from: ' . $ default_env_vars_file );
6751
6852 $ user_vars = DotenvEditor::load ($ default_env_vars_file );
53+
6954 try {
7055 $ user_keys = $ user_vars ->getKeys ();
7156 }
@@ -76,7 +61,6 @@ public function handle()
7661 exit ();
7762 }
7863
79-
8064 // Load the .env
8165 try {
8266 $ app_env = DotenvEditor::load ();
@@ -87,15 +71,14 @@ public function handle()
8771 exit ();
8872 }
8973
90-
9174 // Now apply the user vars
9275 foreach ($ user_keys as $ user_key => $ user_value )
9376 {
9477 $ newValue = $ user_value ['value ' ];
9578
9679 if ($ newValue == '[ASK_FOR_VALUE] ' )
9780 {
98- $ newValue = $ this ->ask ("Please ente value for ' $ user_key' " );
81+ $ newValue = $ this ->ask ("Please enter a value for ' $ user_key' " );
9982 }
10083
10184 if ($ app_env ->keyExists ($ user_key ))
@@ -116,4 +99,47 @@ public function handle()
11699 $ this ->call ('config:clear ' );
117100 $ this ->info ('Done! ' );
118101 }
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+ }
119145}
0 commit comments