1 | <?php |
||
2 | |||
3 | namespace Ijeffro\LaraLocker\Commands; |
||
4 | |||
5 | use Laralocker; |
||
6 | use Illuminate\Console\Command; |
||
7 | use Illuminate\Filesystem\Filesystem; |
||
8 | use Symfony\Component\Process\Process; |
||
9 | use Symfony\Component\Console\Input\InputOption; |
||
10 | use Ijeffro\LaraLocker\LaraLockerServiceProvider; |
||
11 | use Ijeffro\Laralocker\LearningLocker\API\APIHandler; |
||
12 | use Ijeffro\Laralocker\Constants\LearningLockerConstants; |
||
13 | |||
14 | class SetupClientommand extends Command |
||
15 | { |
||
16 | |||
17 | protected $seedersPath = __DIR__.'/../../publishable/database/seeds/'; |
||
18 | protected $migrationsPath = __DIR__.'/../../publishable/database/migrations/'; |
||
19 | |||
20 | /** |
||
21 | * The console command name. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name = 'learninglocker:client'; |
||
26 | |||
27 | /** |
||
28 | * The console command description. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Learning LockerĀ® Client'; |
||
33 | |||
34 | protected function getOptions() |
||
35 | { |
||
36 | return [ |
||
37 | ['setup', null, InputOption::VALUE_NONE, 'Add new Learning LockerĀ® Client', null], |
||
38 | ]; |
||
39 | } |
||
40 | |||
41 | public function fire(Filesystem $filesystem) |
||
42 | { |
||
43 | return $this->handle($filesystem); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Execute the console command. |
||
48 | * |
||
49 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function handle(Filesystem $filesystem) |
||
54 | { |
||
55 | |||
56 | if () { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
57 | $this->setupClient(); |
||
58 | } |
||
59 | |||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Setup Learning Locker Environment Variables |
||
64 | * |
||
65 | */ |
||
66 | protected function setupClient() |
||
67 | { |
||
68 | $this->line("Setup Learning LockerĀ® Client "); echo "\n"; |
||
69 | $this->question(" How would you like to store your project's client credentials? "); echo "\n"; |
||
70 | |||
71 | |||
72 | if ($this->confirm("Setup a single Learning LockerĀ® client with env variables?")): |
||
73 | |||
74 | $this->line(" << Setup Client with Env Variables "); echo "\n"; |
||
75 | $this->setupEnv(); |
||
76 | |||
77 | elseif ($this->confirm("Setup multiply Learning LockerĀ® clients with the database?")): |
||
78 | |||
79 | |||
80 | $this->line(" << Setup with Database "); echo "\n"; |
||
81 | |||
82 | if ($this->confirm("Create new database table to store all your Learning LockerĀ® details?")) { |
||
83 | |||
84 | } elseif ($this->confirm("Add new table columns to an existing database table?")) { |
||
85 | |||
86 | } elseif ($this->confirm("Use existing table columns to store client credentials?")) { |
||
87 | |||
88 | } else { |
||
89 | $this->error(" << Not sure of any other way to to setup an single client in an existing database"); |
||
90 | } |
||
91 | |||
92 | |||
93 | else: |
||
94 | |||
95 | $this->error(" << Unable to setup client credentials for Learning LockerĀ® "); |
||
96 | |||
97 | if ($this->confirm("Would you like to run through the client setup again?")) { |
||
98 | return $this->setupClient(); |
||
99 | } else { |
||
100 | $this->error(" << Failed to connect with Learning LockerĀ® automatically"); |
||
101 | $this->error(" << Learning LockerĀ® Docs: (https://docs.learninglocker.net/http-clients/) "); |
||
102 | } |
||
103 | |||
104 | endif; |
||
105 | |||
106 | |||
107 | |||
108 | |||
109 | |||
110 | |||
111 | |||
112 | elseif ($this->confirm("Setup a single Learning LockerĀ® client in the database?")): |
||
113 | |||
114 | |||
115 | |||
116 | |||
117 | if ($this->confirm("Create new database migration to store all Learning LockerĀ® details?")) { |
||
118 | |||
119 | } elseif ($this->confirm("Add new table columns to an existing database table?")) { |
||
120 | |||
121 | } elseif ($this->confirm("Use existing table columns to store client credentials?")) { |
||
122 | |||
123 | } else { |
||
124 | $this->error(" << Not sure of any other way to to setup an single client in an existing database"); |
||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | |||
130 | |||
131 | elseif ($this->confirm("Setup multiply Learning LockerĀ® clients in the database?")): |
||
132 | |||
133 | |||
134 | |||
135 | |||
136 | if ($this->confirm("Add new columns to an existing database table?")) { |
||
137 | |||
138 | } elseif ($this->confirm("Add new columns to an existing database table?")) { |
||
139 | |||
140 | } elseif ($this->confirm("Use existing columns to an existing database table?")) { |
||
141 | |||
142 | } else { |
||
143 | $this->error(" << Not sure of any other way to to setup an single client in an existing database"); |
||
144 | } |
||
145 | |||
146 | |||
147 | |||
148 | |||
149 | else: |
||
150 | $this->error(" << Not sure of any other way to to setup an single client in an existing database"); |
||
151 | |||
152 | if ($this->confirm("Would you like to run through the setup process again?")) { |
||
153 | } |
||
154 | |||
155 | endif; |
||
156 | |||
157 | |||
158 | |||
159 | elseif ($this->confirm("Setup a single client with database?")): |
||
160 | |||
161 | if($this->confirm("Add new columns to an existing database table?")) { |
||
162 | |||
163 | $migratedDatabaseTables = \DB::connection()->getDoctrineSchemaManager()->listTableNames(); |
||
164 | $selectedTable = $this->anticipate("Which database table?", $migratedDatabaseTables); echo "\n"; |
||
165 | |||
166 | $tableColumns = \Schema::getColumnListing($selectedTable); |
||
167 | |||
168 | $this->line(ucfirst($selectedTable) . " Table"); echo "\n"; |
||
169 | $this->table($tableColumns, []); |
||
170 | |||
171 | $keyColumn = $this->anticipate("Which column for the Learning Locker client key?", $tableColumns); echo "\n"; |
||
172 | $secretColumn = $this->anticipate("Which column for the Learning Locker client secret?", $tableColumns); echo "\n"; |
||
173 | |||
174 | $headers = [$keyColumn, $secretColumn]; |
||
175 | |||
176 | $key = str_random(10); |
||
177 | $secret = str_random(10); |
||
178 | $tableColumns = [$key, $secret]; |
||
179 | |||
180 | $this->table($headers, $tableColumns); |
||
181 | dd($tableColumns); |
||
182 | |||
183 | // ->getConnection() |
||
184 | // ->getSchemaBuilder() |
||
185 | // ->getColumnListing($this->getTable()); |
||
186 | |||
187 | $migrationsDirectory = database_path("/migrations/*"); |
||
188 | $migrationFiles = glob($migrationsDirectory); |
||
189 | |||
190 | $migrationFileNames = []; |
||
191 | $addColumnMigrations = []; |
||
192 | |||
193 | //Loop through the array that glob returned. |
||
194 | foreach($migrationFiles as $migrationFile) { |
||
195 | |||
196 | if(strpos($migrationFile, "add_learning_locker_client_to_$table")) { |
||
197 | $addColumnMigrations[] = $migrationFile; |
||
198 | } |
||
199 | |||
200 | $migrationFileNames[] = $migrationFile; |
||
201 | } |
||
202 | dd($addColumnMigrations); |
||
203 | |||
204 | |||
205 | // Check .env for Learning Locker url status |
||
206 | switch ($migrationsDirectory) { |
||
207 | |||
208 | case isset($addColumnMigrations) && count($addColumnMigrations) > 1: |
||
209 | |||
210 | $this->question(" << You have " . count($addColumnMigrations) . " migrations which will add client credential columns to the $table table "); |
||
211 | |||
212 | |||
213 | break; |
||
214 | case !isset($addColumnMigrations) && count($addColumnMigrations) === 0: |
||
215 | |||
216 | break; |
||
217 | } |
||
218 | |||
219 | if(isset($addColumnMigrations) && count($addColumnMigrations) > 1) { |
||
220 | |||
221 | $this->question(" << You have " . count($addColumnMigrations) . " migrations which will add client credential columns to the $table table "); |
||
222 | |||
223 | if($this->confirm("Would you like to overwrite these files with the new database migration?")) { |
||
224 | |||
225 | |||
226 | foreach($addColumnMigrations as $addColumnMigrations) { |
||
227 | if(is_file($addColumnMigrations)) |
||
228 | unlink($addColumnMigrations); //delete file |
||
229 | } |
||
230 | |||
231 | $makeMigration = $this->call("make:migration", ['name' => "add_learning_locker_client_to_$table"]); |
||
232 | |||
233 | } elseif($this->confirm("Would you like to save the new database migration and keep existing migrations?")) { |
||
234 | |||
235 | $makeMigration = $this->call("make:migration", ['name' => "add_learning_locker_client_to_$table"]); |
||
236 | |||
237 | } |
||
238 | |||
239 | |||
240 | } |
||
241 | |||
242 | die; |
||
243 | // dd($addColumnMigrations); |
||
244 | // $migration_contents = $filesystem->get(database_path("migrations/add_learning_locker_client_to_$table.php")); |
||
245 | |||
246 | $migration = database_path("migrations/add_learning_locker_client_to_$table.php"); |
||
247 | $contents = file_get_contents($migration); |
||
248 | |||
249 | $find = 'public function up() |
||
250 | { |
||
251 | Schema::table('. $table . ', function (Blueprint $table) { |
||
252 | // |
||
253 | }); |
||
254 | }'; |
||
255 | |||
256 | $replace = 'public function up() |
||
257 | { |
||
258 | Schema::table('. $table . ', function (Blueprint $table) { |
||
259 | // Nailed it |
||
260 | }); |
||
261 | }'; |
||
262 | |||
263 | $contents = str_replace($find, $replace, $contents); |
||
264 | file_put_contents($migration, $contents); |
||
265 | |||
266 | $find = 'public function down() |
||
267 | { |
||
268 | Schema::table('. $table . ', function (Blueprint $table) { |
||
269 | // |
||
270 | }); |
||
271 | }'; |
||
272 | |||
273 | $replace = 'public function down() |
||
274 | { |
||
275 | Schema::table('. $table . ', function (Blueprint $table) { |
||
276 | // Nailed it |
||
277 | }); |
||
278 | }'; |
||
279 | |||
280 | $contents = str_replace($find, $replace, $contents); |
||
281 | file_put_contents($migration, $contents); |
||
282 | |||
283 | |||
284 | |||
285 | dd('yay'); |
||
286 | |||
287 | $migrationClass = "AddLearningLockerClientTo" . ucfirst($table); |
||
288 | |||
289 | $contents = file_get_contents($env); |
||
290 | |||
291 | |||
292 | } elseif ($this->confirm("Add Learning Locker client credentials to an existing database table?")) { |
||
293 | dump("Single client in the database"); |
||
294 | } else { |
||
295 | dump("Single client in the database"); |
||
296 | } |
||
297 | |||
298 | elseif ($this->confirm("Setup multiple Learning Locker client's with database?")): |
||
299 | dump("database setup"); |
||
300 | |||
301 | else: |
||
302 | |||
303 | $this->error(" << Client Setup Failed: Laralocker requires a Learning Locker url, client key and secret "); |
||
304 | $this->error(" << Docs: (https://docs.learninglocker.net/http-clients/) "); |
||
305 | |||
306 | if($this->confirm("Attempt to conigure client details?")) { |
||
307 | $this->setupClient(); |
||
308 | } |
||
309 | |||
310 | return $this->error(" << Setup Failed: Laralocker requires Learning Locker Client details"); |
||
311 | |||
312 | endif; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Setup Learning Locker Environment Variables |
||
317 | * |
||
318 | * @return LEARNING_LOCKER_URL |
||
319 | * @return LEARNING_LOCKER_KEY |
||
320 | * @return LEARNING_LOCKER_SECRET |
||
321 | */ |
||
322 | public function setupEnv() |
||
323 | { |
||
324 | $env = base_path('.env'); |
||
325 | $filesystem = new Filesystem; |
||
326 | $env_contents = $filesystem->get($env); |
||
327 | |||
328 | $learning_locker_url = getEnv(strtoupper(LearningLockerConstants::URL)); |
||
329 | $learning_locker_key = getEnv(strtoupper(LearningLockerConstants::KEY)); |
||
330 | $learning_locker_secret = getEnv(strtoupper(LearningLockerConstants::SECRET)); |
||
331 | |||
332 | // Has variable and value |
||
333 | $url_has_variable_and_value = isset($learning_locker_url) && !empty($learning_locker_url); |
||
334 | $key_has_variable_and_value = isset($learning_locker_key) && !empty($learning_locker_key); |
||
335 | $secret_has_variable_and_value = isset($learning_locker_secret) && !empty($learning_locker_secret); |
||
336 | |||
337 | // Has variable with no value |
||
338 | $url_has_variable_with_no_value = false !== strpos($env_contents, strtoupper(LearningLockerConstants::URL)) && empty($learning_locker_url); |
||
339 | $key_has_variable_with_no_value = false !== strpos($env_contents, strtoupper(LearningLockerConstants::KEY)) && empty($learning_locker_key); |
||
340 | $secret_has_variable_with_no_value = false !== strpos($env_contents, strtoupper(LearningLockerConstants::SECRET)) && empty($learning_locker_secret); |
||
341 | |||
342 | // Has no variable and no value |
||
343 | $url_has_no_variable_and_no_value = true !== strpos($env_contents, strtoupper(LearningLockerConstants::URL)); |
||
344 | $key_has_no_variable_and_no_value = true !== strpos($env_contents, strtoupper(LearningLockerConstants::KEY)); |
||
345 | $secret_has_no_variable_and_no_value = true !== strpos($env_contents, strtoupper(LearningLockerConstants::SECRET)); |
||
346 | |||
347 | // Do the env smarts |
||
348 | if ($url_has_variable_and_value && $key_has_variable_and_value && $secret_has_variable_and_value) { |
||
349 | |||
350 | if ($this->confirm('Update Learning Locker api connection?')) { |
||
351 | $domain = $this->anticipate("What's the new learning Locker url?", [ |
||
352 | 'https://saas.learninglocker.net', 'http://saas.learninglocker.net' |
||
353 | ]); |
||
354 | $this->setEnv([strtoupper(LearningLockerConstants::URL) => $domain]); |
||
355 | |||
356 | $key = $this->ask("What's the new learning Locker client key?"); |
||
357 | $this->setEnv([strtoupper(LearningLockerConstants::KEY) => $key]); |
||
358 | |||
359 | $secret = $this->secret("What's the new learning Locker client secret"); |
||
360 | $this->setEnv([strtoupper(LearningLockerConstants::SECRET) => $secret]); |
||
361 | } |
||
362 | |||
363 | } else { |
||
364 | try { |
||
365 | |||
366 | // Check .env for Learning Locker url status |
||
367 | switch (file_exists($env)) { |
||
368 | case $url_has_variable_and_value: |
||
369 | if ($this->confirm('Update Learning Locker domain?')) { |
||
370 | $domain = $this->anticipate("What's the new learning Locker domain?", [ |
||
371 | 'https://saas.learninglocker.net', 'http://saas.learninglocker.net' |
||
372 | ]); |
||
373 | $this->setEnv([strtoupper(LearningLockerConstants::URL) => $domain]); |
||
374 | } |
||
375 | break; |
||
376 | case $url_has_variable_with_no_value: |
||
377 | $domain = $this->anticipate("What's your Learning Locker url?", [ |
||
378 | 'https://saas.learninglocker.net', 'http://saas.learninglocker.net' |
||
379 | ]); |
||
380 | $this->setEnv([strtoupper(LearningLockerConstants::URL) => $domain]); |
||
381 | break; |
||
382 | case $url_has_no_variable_and_no_value: |
||
383 | $domain = $this->anticipate("What's your Learning Locker url?", [ |
||
384 | 'https://saas.learninglocker.net', 'http://saas.learninglocker.net' |
||
385 | ]); |
||
386 | $this->createEnv(strtoupper(LearningLockerConstants::URL), $domain); |
||
387 | break; |
||
388 | } |
||
389 | |||
390 | // Check .env for Learning Locker client key status |
||
391 | switch (file_exists($env)) { |
||
392 | case $key_has_variable_and_value: |
||
393 | if ($this->confirm('Update Learning Locker client key?')) { |
||
394 | $key = $this->ask("What's the new learning Locker client key?"); |
||
395 | $this->setEnv([strtoupper(LearningLockerConstants::KEY) => $key]); |
||
396 | } |
||
397 | break; |
||
398 | case $key_has_variable_with_no_value: |
||
399 | $key = $this->ask("What's your Learning Locker client key?"); |
||
400 | $this->setEnv([strtoupper(LearningLockerConstants::KEY) => $key]); |
||
401 | break; |
||
402 | case $key_has_no_variable_and_no_value: |
||
403 | $key = $this->ask("What's your Learning Locker client key?"); |
||
404 | $this->createEnv(strtoupper(LearningLockerConstants::KEY), $key); |
||
405 | break; |
||
406 | } |
||
407 | |||
408 | // Check .env for Learning Locker client secret status |
||
409 | switch (file_exists($env)) { |
||
410 | case $secret_has_variable_and_value: |
||
411 | if ($this->confirm('Update Learning Locker client secret?')) { |
||
412 | $secret = $this->secret("What's the new learning Locker client secret"); |
||
413 | $this->setEnv([strtoupper(LearningLockerConstants::KEY) => $key]); |
||
414 | } |
||
415 | break; |
||
416 | case $secret_has_variable_with_no_value: |
||
417 | $secret = $this->secret("What's your Learning Locker client secret"); |
||
418 | $this->setEnv([strtoupper(LearningLockerConstants::KEY) => $secret]); |
||
419 | break; |
||
420 | case $secret_has_no_variable_and_no_value: |
||
421 | $secret = $this->secret("What's your Learning Locker client secret"); |
||
422 | $this->createEnv(strtoupper(LearningLockerConstants::SECRET), $secret); |
||
423 | break; |
||
424 | } |
||
425 | |||
426 | } catch (Exception $e) { |
||
427 | return $e->getMessage(); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | return $this->info(" << Connecting to Learning Locker..."); |
||
432 | |||
433 | $connectionCheck = \LearningLocker::connection()->check(env(strtoupper(LearningLockerConstants::URL))); |
||
434 | |||
435 | if ($connectionCheck) { |
||
436 | return $this->info(" << Connected to Learning Locker Succussfully"); |
||
437 | } |
||
438 | |||
439 | if ($this->confirm('Try updating Learning Locker details again?')) { |
||
440 | return $this->setupEnv(); |
||
441 | } |
||
442 | |||
443 | return $this->error("Unable to connection with Learning Locker"); |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Get the composer command for the environment. |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | protected function findComposer() |
||
452 | { |
||
453 | if (file_exists(getcwd().'/composer.phar')) { |
||
454 | return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar'; |
||
455 | } |
||
456 | |||
457 | return 'composer'; |
||
458 | } |
||
459 | |||
460 | public function addLearningLockerRouting(Filesystem $filesystem) |
||
461 | { |
||
462 | $routes_contents = $filesystem->get(base_path('routes/api.php')); |
||
463 | |||
464 | if (false === strpos($routes_contents, 'LearningLocker::routes()')) { |
||
465 | |||
466 | if ($this->confirm('Create Laravel api routes for Learning LockerĀ®?')) { |
||
467 | $this->info(' << Adding Learning LockerĀ® routes to routes/api.php'); |
||
468 | $filesystem->append(base_path('routes/api.php'), "\nLearningLocker::routes();\n"); |
||
469 | } |
||
470 | } |
||
471 | } |
||
472 | |||
473 | public function publishVendor() |
||
474 | { |
||
475 | $tags = ['config']; |
||
476 | $this->call('vendor:publish', ['--provider' => LaraLockerServiceProvider::class, '--tag' => $tags]); |
||
477 | } |
||
478 | |||
479 | public function checkLearningLockerConnection() |
||
480 | { |
||
481 | |||
482 | $this->learning_locker_api = new APIHandler; |
||
483 | |||
484 | if ($this->learning_locker_api->check()) { |
||
485 | $this->info('Successfully connected to Learning LockerĀ®'); |
||
486 | } else { |
||
487 | return $this->error(' << Could not connect to Learning LockerĀ® '); |
||
488 | } |
||
489 | |||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Get a Laralocker env variable |
||
494 | * |
||
495 | */ |
||
496 | public function getEnv($key = null) |
||
497 | { |
||
498 | $env = base_path('.env'); |
||
499 | |||
500 | if (file_exists($env) && !is_null($key)) { |
||
501 | return env($key); |
||
502 | } |
||
503 | |||
504 | if (file_exists($env) && is_null($key)) { |
||
505 | $env_variables = parse_ini_file($env, true, INI_SCANNER_RAW); |
||
506 | return $env_variables; |
||
507 | } |
||
508 | |||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Set the Laralocker env variables |
||
513 | * |
||
514 | */ |
||
515 | public function setEnv($data) |
||
516 | { |
||
517 | $env = base_path('.env'); |
||
518 | $variables = array_keys($data); |
||
519 | |||
520 | if (file_exists($env)) { |
||
521 | foreach ($variables as $variable) { |
||
522 | $env_variable = $this->getEnv($variable); |
||
523 | $contents = file_get_contents($env); |
||
524 | $contents = str_replace(strtoupper($variable) . '=' . $env_variable, strtoupper($variable) . '=' . $data[$variable], $contents); |
||
525 | file_put_contents($env, $contents); |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Create a Laralocker env variable |
||
532 | * |
||
533 | */ |
||
534 | public function createEnv($key, $value) |
||
535 | { |
||
536 | $env = base_path('.env'); |
||
537 | |||
538 | if (file_exists($env)) { |
||
539 | |||
540 | $filesystem = new Filesystem; |
||
541 | $env_contents = $filesystem->get($env); |
||
542 | |||
543 | if (false === strpos($env_contents, $key)) { |
||
544 | $filesystem->append($env, "\n" . strtoupper($key) . '=' . strtolower($value)); |
||
545 | return $this->info("Created Environment variable $value successfully."); |
||
546 | } |
||
547 | } |
||
548 | } |
||
549 | |||
550 | |||
551 | } |
||
552 |