├── README.md ├── composer.json ├── screenshot.png └── src ├── DotEnvGenCommand.php ├── DotEnvGenServiceProvider.php └── config └── dotenvgen.php /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Dot Env Generator 2 | A Laravel 5 command that generates a `.env.gen` file containing all environment 3 | variables defined in `.env` as well as any undefined variables that are being 4 | used throughout the project. 5 | 6 | # Installation 7 | 8 | Install the package using composer: 9 | 10 | ```bash 11 | composer require mathiasgrimm/laravel-dot-env-gen:dev-master 12 | ``` 13 | 14 | Add the service provider: 15 | 16 | ```php 17 | // config/app.php 18 | 19 | 'providers' => [ 20 | ... 21 | 'MathiasGrimm\LaravelDotEnvGen\DotEnvGenServiceProvider', 22 | ... 23 | ], 24 | ``` 25 | 26 | Add `.env.gen` to your `.gitignore` 27 | 28 | ## Configuration 29 | 30 | You can control which directories are scanned by providing exclusion rules in 31 | the `dotenvgen.php` config file. For a fresh Laravel install, we suggest that 32 | all subdirectories inside `vendor` are ignored except for `vendor/laravel`. 33 | 34 | Publish the config file: 35 | 36 | ```bash 37 | php artisan vendor:publish --provider="MathiasGrimm\LaravelDotEnvGen\DotEnvGenServiceProvider" --tag="config" 38 | ``` 39 | 40 | Example config: 41 | 42 | ```php 43 | // config/dotenvgen.php 44 | 45 | 'rules' => [ 46 | // Ignores all files inside `vendor` except for those in `vendor/laravel` 47 | 'vendor' => ['laravel'], 48 | 49 | // Ignores the `database/seeds` directory 50 | 'database/seeds' => [], 51 | ], 52 | ``` 53 | 54 | # Usage 55 | 56 | From the command line, run `php artisan env:gen`. 57 | 58 | A `.env.gen` file will be generated in your project's root folder. Make any 59 | changes you may need, then rename the file to `.env`. 60 | 61 | Along with generating the `.env.gen` file, the command will notify you if a 62 | defined environment variable is unused as well as alert you if an undefined 63 | environment variable is being used. 64 | 65 | ## Screenshot 66 | 67 | ![Screenshot](screenshot.png) 68 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathiasgrimm/laravel-dot-env-gen", 3 | "description": "A Laravel 5 artisan command to generate a .env.gen file based on the existing project.", 4 | "keywords": ["artisan", "command", "dotenv", "env", "environment"], 5 | "homepage": "https://github.com/mathiasgrimm/laravel-dot-env-gen/", 6 | "type": "library", 7 | "authors": [ 8 | { 9 | "name": "Mathias Grimm", 10 | "email": "mathiasgrimm@gmail.com", 11 | "homepage": "http://phpEmpregos.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "MathiasGrimm\\LaravelDotEnvGen\\": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathiasgrimm/laravel-dot-env-gen/1283fa70fcfc8271e7281f53133869dcf0a814c9/screenshot.png -------------------------------------------------------------------------------- /src/DotEnvGenCommand.php: -------------------------------------------------------------------------------- 1 | gatherFiles(); 61 | $this->scanFiles(); 62 | $this->scanEnv(); 63 | $this->generateFile(); 64 | $this->info('Done. Results:'); 65 | $this->showResults(); 66 | 67 | $this->info('By Laravel Dot Env Generator - https://github.com/mathiasgrimm/laravel-dot-env-gen'); 68 | } 69 | 70 | protected function gatherFiles() 71 | { 72 | $this->info('Gathering PHP files...'); 73 | $this->info('You can speed up this process by excluding folders. Check the README for more info.'); 74 | 75 | $directory = new \RecursiveDirectoryIterator(base_path()); 76 | $iterator = new \RecursiveIteratorIterator($directory); 77 | $rules = \Config::get('dotenvgen.rules'); 78 | $ignore = implode('|', array_map(function ($path) use ($rules) { 79 | if (!empty($rules[$path])) { 80 | $excludes = $rules[$path]; 81 | 82 | if (is_array($excludes)) { 83 | $excluded = implode('|', array_map(function ($sub) { 84 | return preg_quote(DIRECTORY_SEPARATOR . $sub, '/'); 85 | }, array_filter($excludes))); 86 | } else { 87 | $excluded = preg_quote(DIRECTORY_SEPARATOR . $excludes, '/'); 88 | } 89 | } 90 | 91 | return preg_quote($path, '/') . (!empty($excluded) ? '(?!' . $excluded . ')' : ''); 92 | }, array_filter(array_keys($rules)))); 93 | 94 | if (!empty($ignore)) { 95 | $regex = '/^(?!' . preg_quote(base_path() . DIRECTORY_SEPARATOR, '/') . '(' . $ignore . ')' . ').+\.php$/i'; 96 | } else { 97 | $regex = '/^.+\.php$/i'; 98 | } 99 | 100 | $this->iterator = new \RegexIterator($iterator, $regex, \RecursiveRegexIterator::GET_MATCH); 101 | } 102 | 103 | protected function scanFiles() 104 | { 105 | $count = iterator_count($this->iterator); 106 | 107 | $this->info("Scanning $count files..."); 108 | 109 | $this->progressBar = new ProgressBar($this->output, $count); 110 | $this->progressBar->start(); 111 | 112 | foreach ($this->iterator as $i => $v) { 113 | $this->progressBar->advance(); 114 | 115 | $contents = file_get_contents($i); 116 | $matches = null; 117 | 118 | if (preg_match_all('/[^\w_]env\s*\((\'|").*?(\'|")\s*.*?\)/sim', $contents, $matches)) { 119 | foreach ($matches[0] as $match) { 120 | $matches2 = null; 121 | 122 | preg_match('/\(\s*(\'|")(?P.*?)(\'|")(,(?P.*))?\)/', $match, $matches2); 123 | 124 | $this->found[$matches2['name']] = ''; 125 | $this->defaults[$matches2['name']] = isset($matches2['default']) ? trim($matches2['default']) : null; 126 | } 127 | } 128 | } 129 | 130 | $this->progressBar->finish(); 131 | 132 | $this->info(''); 133 | } 134 | 135 | protected function scanEnv() 136 | { 137 | $this->info('Scanning `.env` file...'); 138 | 139 | if (!file_exists(base_path('.env'))) { 140 | return; 141 | } 142 | 143 | foreach (file(base_path('.env')) as $line) { 144 | if (strpos(trim($line), '#') === 0 || strpos($line, '=') === false) { 145 | continue; 146 | } 147 | 148 | list($name, $value) = array_map('trim', explode('=', $line, 2)); 149 | 150 | $this->defined[$name] = $value; 151 | } 152 | } 153 | 154 | protected function generateFile() 155 | { 156 | $this->info('Generating `.env.gen` file...'); 157 | 158 | $this->all = array_merge($this->found, $this->defined); 159 | 160 | ksort($this->all); 161 | 162 | $content = ''; 163 | 164 | foreach ($this->all as $key => $val) { 165 | $content .= "$key=$val\n"; 166 | } 167 | 168 | file_put_contents(base_path('.env.gen'), $content); 169 | } 170 | 171 | protected function showResults() 172 | { 173 | $table = new Table($this->output); 174 | 175 | $table->setHeaders([ 176 | 'Name', 177 | 'In .env', 178 | 'In source', 179 | 'Default' 180 | ]); 181 | 182 | $rows = []; 183 | 184 | foreach ($this->all as $key => $val) { 185 | $row = [$key]; 186 | 187 | if (array_key_exists($key, $this->defined)) { 188 | $row[] = 'Yes'; 189 | } else { 190 | $row[0] = "$key"; 191 | $row[] = 'No'; 192 | } 193 | 194 | if (array_key_exists($key, $this->found)) { 195 | $row[] = 'Yes'; 196 | } else { 197 | $row[0] = "$key"; 198 | $row[] = 'No'; 199 | } 200 | 201 | $row[] = array_get($this->defaults, $key); 202 | $rows[] = $row; 203 | } 204 | 205 | $table->setRows($rows); 206 | $table->render(); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /src/DotEnvGenServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 10 | __DIR__ . '/config/dotenvgen.php' => config_path('dotenvgen.php'), 11 | ], 'config'); 12 | } 13 | 14 | public function register() 15 | { 16 | $this->mergeConfigFrom(__DIR__ . '/config/dotenvgen.php', 'dotenvgen'); 17 | 18 | $this->app->singleton('command.env.gen', function ($app) { 19 | return $app['MathiasGrimm\LaravelDotEnvGen\DotEnvGenCommand']; 20 | }); 21 | 22 | $this->commands('command.env.gen'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/config/dotenvgen.php: -------------------------------------------------------------------------------- 1 | [ 23 | | 'vendor' => ['laravel'], 24 | | ], 25 | | 26 | | Or if you want to ignore a specific vendor package like `mathiasgrimm/*`, 27 | | you may use the path to the vendor's root or package folder like so: 28 | | 29 | | 'rules' => [ 30 | | 'vendor/mathiasgrimm' => [], 31 | | ], 32 | | 33 | */ 34 | 35 | 'rules' => [ 36 | 37 | /** 38 | * We recommend uncommenting this line to improve performance in a 39 | * fresh Laravel install. If you are using any packages that use 40 | * environment variables, you may simply add the vendor/package folder 41 | * name before, after, or in place of `'laravel'`: 42 | * 43 | * 'vendor' => ['laravel', 'foodeveloper/barpackage'], 44 | * 45 | * Or if you want to scan all of foodeveloper's packages: 46 | * 47 | * 'vendor' => ['laravel', 'foodeveloper'], 48 | * 49 | */ 50 | // 'vendor' => ['laravel'], 51 | 52 | ], 53 | 54 | ]; 55 | --------------------------------------------------------------------------------