1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelEnvScanner\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Mtolhuys\LaravelEnvScanner\LaravelEnvScanner; |
7
|
|
|
|
8
|
|
|
class EnvScan extends Command |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = ' |
17
|
|
|
env:scan |
18
|
|
|
{ --d|dir= : Specify directory to scan (defaults to your config folder) } |
19
|
|
|
{ --a|all : Show result containing all used variables } |
20
|
|
|
'; |
21
|
|
|
|
22
|
|
|
private $scanner; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Check environmental variables used in your app'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the console command. |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
public function handle() |
38
|
|
|
{ |
39
|
|
|
$this->scanner = new LaravelEnvScanner( |
40
|
|
|
$this->option('dir') |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
if (! file_exists($this->scanner->dir)) { |
44
|
|
|
$this->error("{$this->scanner->dir} does not exist"); |
45
|
|
|
exit(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->output->write( |
49
|
|
|
"<fg=green>Scanning:</fg=green> <fg=white>{$this->scanner->dir}/...</fg=white>\n" |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->scanner->scan(); |
53
|
|
|
|
54
|
|
|
foreach ($this->scanner->warnings as $warning) { |
55
|
|
|
$this->warn("Warning: <fg=red>{$warning->invocation}</fg=red> found in {$warning->location}"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (empty($this->scanner->results['rows'])) { |
59
|
|
|
$this->line('Nothing there...'); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->showResult(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function showResult() |
68
|
|
|
{ |
69
|
|
|
if ($this->option('all')) { |
70
|
|
|
$this->table([ |
71
|
|
|
"Locations ({$this->scanner->results['locations']})", |
72
|
|
|
"Defined ({$this->scanner->results['defined']})", |
73
|
|
|
"Depending on default ({$this->scanner->results['depending_on_default']})", |
74
|
|
|
"Undefined ({$this->scanner->results['undefined']})", |
75
|
|
|
], $this->scanner->results['rows']); |
76
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (empty($this->scanner->warnings) && $this->scanner->results['undefined'] === 0) { |
81
|
|
|
$this->info('Looking good!'); |
82
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->warn( |
87
|
|
|
"<fg=red>{$this->scanner->results['undefined']} undefined variable(s) found in {$this->scanner->dir}/...</fg=red>" |
88
|
|
|
); |
89
|
|
|
$this->table([], $this->scanner->undefined); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|