|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BeyondCode\SelfDiagnosis\Checks; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use RecursiveDirectoryIterator; |
| 8 | +use RecursiveIteratorIterator; |
| 9 | +use RegexIterator; |
| 10 | + |
| 11 | +class UsedEnvironmentVariablesAreDefined implements Check |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Stores processed var names |
| 15 | + * |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + private $processed = []; |
| 19 | + |
| 20 | + /** |
| 21 | + * Stores undefined var names |
| 22 | + * |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + public $undefined = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * The amount of undefined .env variables |
| 29 | + * |
| 30 | + * @var integer |
| 31 | + */ |
| 32 | + public $amount = 0; |
| 33 | + |
| 34 | + /** |
| 35 | + * The name of the check. |
| 36 | + * |
| 37 | + * @param array $config |
| 38 | + * @return string |
| 39 | + */ |
| 40 | + public function name(array $config): string |
| 41 | + { |
| 42 | + return trans('self-diagnosis::checks.used_env_variables_are_defined.name'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * The error message to display in case the check does not pass. |
| 47 | + * |
| 48 | + * @param array $config |
| 49 | + * @return string |
| 50 | + */ |
| 51 | + public function message(array $config): string |
| 52 | + { |
| 53 | + return trans('self-diagnosis::checks.used_env_variables_are_defined.message', [ |
| 54 | + 'amount' => $this->amount, |
| 55 | + 'undefined' => implode(PHP_EOL, $this->undefined), |
| 56 | + ]); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Perform the actual verification of this check. |
| 61 | + * |
| 62 | + * @param array $config |
| 63 | + * @return bool |
| 64 | + * @throws \Exception |
| 65 | + */ |
| 66 | + public function check(array $config): bool |
| 67 | + { |
| 68 | + $paths = Collection::make(Arr::get($config, 'directories', [])); |
| 69 | + |
| 70 | + foreach ($paths as $path) { |
| 71 | + $files = $this->recursiveDirSearch($path, '/.*?.php/'); |
| 72 | + |
| 73 | + foreach ($files as $file) { |
| 74 | + preg_match_all( |
| 75 | + '# env\((.*?)\)| getenv\((.*?)\)#', |
| 76 | + str_replace(["\n", "\r"], '', file_get_contents($file)), |
| 77 | + $values |
| 78 | + ); |
| 79 | + |
| 80 | + $values = array_filter( |
| 81 | + array_merge($values[1], $values[2]) |
| 82 | + ); |
| 83 | + |
| 84 | + foreach ($values as $value) { |
| 85 | + $result = $this->getResult( |
| 86 | + explode(',', str_replace(["'", '"', ' '], '', $value)) |
| 87 | + ); |
| 88 | + |
| 89 | + if (!$result) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + $this->storeResult($result); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $this->amount === 0; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get result based on comma separated env() or getenv() parameters |
| 103 | + * |
| 104 | + * @param array $values |
| 105 | + * @return object|bool |
| 106 | + */ |
| 107 | + private function getResult(array $values) |
| 108 | + { |
| 109 | + $envVar = $values[0]; |
| 110 | + |
| 111 | + if (in_array($envVar, $this->processed, true)) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + $this->processed[] = $envVar; |
| 116 | + |
| 117 | + return (object)[ |
| 118 | + 'envVar' => $envVar, |
| 119 | + 'hasValue' => env($envVar) !== null, |
| 120 | + 'hasDefault' => isset($values[1]), |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Store result based on getResult's return value |
| 126 | + * |
| 127 | + * @param $result |
| 128 | + */ |
| 129 | + private function storeResult($result) |
| 130 | + { |
| 131 | + if (!$result->hasValue && !$result->hasDefault) { |
| 132 | + $this->undefined[] = $result->envVar; |
| 133 | + $this->amount++; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Recursively search folder(s) for files matching pattern |
| 139 | + * |
| 140 | + * @param string $folder |
| 141 | + * @param string $pattern |
| 142 | + * @return array |
| 143 | + */ |
| 144 | + private function recursiveDirSearch(string $folder, string $pattern): array |
| 145 | + { |
| 146 | + if (!file_exists($folder)) { |
| 147 | + return []; |
| 148 | + } |
| 149 | + |
| 150 | + $files = new RegexIterator( |
| 151 | + new RecursiveIteratorIterator( |
| 152 | + new RecursiveDirectoryIterator($folder) |
| 153 | + ), |
| 154 | + $pattern, RegexIterator::GET_MATCH |
| 155 | + ); |
| 156 | + |
| 157 | + $list = [[]]; |
| 158 | + |
| 159 | + foreach ($files as $file) { |
| 160 | + $list[] = $file; |
| 161 | + } |
| 162 | + |
| 163 | + $list = array_merge(...$list); |
| 164 | + |
| 165 | + return $list; |
| 166 | + } |
| 167 | +} |
0 commit comments