1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelEnvScanner; |
4
|
|
|
|
5
|
|
|
use RecursiveDirectoryIterator; |
6
|
|
|
use RecursiveIteratorIterator; |
7
|
|
|
use RegexIterator; |
8
|
|
|
|
9
|
|
|
class LaravelEnvScanner |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The results of performed scan |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public $results = [ |
17
|
|
|
'files' => 0, |
18
|
|
|
'empty' => 0, |
19
|
|
|
'has_value' => 0, |
20
|
|
|
'depending_on_default' => 0, |
21
|
|
|
'data' => [] |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Current file being processed |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $currentFile; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Root directory to start recursive search for env()'s from |
33
|
|
|
* Defaults to config_path() |
34
|
|
|
* |
35
|
|
|
* @var string $dir |
36
|
|
|
*/ |
37
|
|
|
public $dir; |
38
|
|
|
|
39
|
|
|
public function __construct(string $dir = null) |
40
|
|
|
{ |
41
|
|
|
$this->dir = $dir ?? config_path(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute the console command. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function scan() |
51
|
|
|
{ |
52
|
|
|
$files = $this->recursiveDirSearch($this->dir, '/.*?.php/'); |
53
|
|
|
|
54
|
|
|
foreach ($files as $file) { |
55
|
|
|
$values = array_filter( |
56
|
|
|
preg_split("#[\n]+#", shell_exec("tr -d '\n' < $file | grep -oP 'env\(\K[^)]+'")) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
foreach ($values as $value) { |
60
|
|
|
$result = $this->getResult( |
61
|
|
|
explode(',', str_replace(["'", '"', ' '], '', $value)) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->storeResult($file, $result); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get result based on comma separated parsed env() parameters |
73
|
|
|
* |
74
|
|
|
* @param array $values |
75
|
|
|
* @return object |
76
|
|
|
*/ |
77
|
|
|
private function getResult(array $values) |
78
|
|
|
{ |
79
|
|
|
return (object)[ |
80
|
|
|
'envVar' => $values[0], |
81
|
|
|
'hasValue' => (bool)env($values[0]), |
82
|
|
|
'hasDefault' => isset($values[1]), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Store result and optional runtime output |
88
|
|
|
* |
89
|
|
|
* @param string $file |
90
|
|
|
* @param $result |
91
|
|
|
*/ |
92
|
|
|
private function storeResult(string $file, $result) |
93
|
|
|
{ |
94
|
|
|
$resultData = [ |
95
|
|
|
'filename' => $this->getFilename($file), |
96
|
|
|
'has_value' => '-', |
97
|
|
|
'depending_on_default' => '-', |
98
|
|
|
'empty' => '-', |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if ($result->hasValue) { |
102
|
|
|
$resultData['has_value'] = $result->envVar; |
103
|
|
|
$this->results['has_value']++; |
104
|
|
|
} else if ($result->hasDefault) { |
105
|
|
|
$resultData['depending_on_default'] = $result->envVar; |
106
|
|
|
$this->results['depending_on_default']++; |
107
|
|
|
} else { |
108
|
|
|
$resultData['empty'] = $result->envVar; |
109
|
|
|
$this->results['empty']++; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->results['data'][] = $resultData; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getFilename(string $file) |
116
|
|
|
{ |
117
|
|
|
$basename = basename($file); |
118
|
|
|
|
119
|
|
|
if ($this->currentFile === $basename) { |
120
|
|
|
return '-'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->results['files']++; |
124
|
|
|
|
125
|
|
|
return $this->currentFile = $basename; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function recursiveDirSearch(string $folder, string $pattern): array |
129
|
|
|
{ |
130
|
|
|
if (! file_exists($folder)) { |
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$files = new RegexIterator( |
135
|
|
|
new RecursiveIteratorIterator( |
136
|
|
|
new RecursiveDirectoryIterator($folder) |
137
|
|
|
), |
138
|
|
|
$pattern, RegexIterator::GET_MATCH |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$list = []; |
142
|
|
|
|
143
|
|
|
foreach($files as $file) { |
144
|
|
|
$list = array_merge($list, $file); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $list; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|