├── README.md
└── app
└── Console
├── Commands
└── EnvCommand.php
└── Kernel.php
/README.md:
--------------------------------------------------------------------------------
1 | # Laravel 5 env
2 |
3 | An artisan command for managing multiple `.env` of your Laravel 5 app.
4 |
5 | # Installation
6 |
7 | 1. Copy `EnvCommand.php` to `app/Console/Commands/` of your Laravel 5 app.
8 | 2. Register the command in `app/Console/Kernel.php` (or copy the one from this repo)
9 |
10 | # Usage
11 |
12 | Save the current `.env` into `.$APP_ENV.env`.
13 |
14 | ```bash
15 | $ php artisan env
16 | Current application environment: local
17 | $ php artisan env:switch --save
18 | Environmental config file .local.env saved
19 | ```
20 |
21 | Switch to another environment, given `.$TARGET_ENV.env` exists.
22 |
23 | ```bash
24 | $ php artisan env
25 | Current application environment: test
26 | $ php artisan env:switch local
27 | Successfully switched from test to local.
28 | $ php artisan env
29 | Current application environment: local
30 | ```
31 |
32 | ## Thank you
33 |
34 | * Thanks [@leonel](http://blog.tommyku.com/blog/setting-up-laravel-5-0-for-openshift#comment-1905666612) for raising the issue, which inspired me to create this command.
35 |
--------------------------------------------------------------------------------
/app/Console/Commands/EnvCommand.php:
--------------------------------------------------------------------------------
1 | laravel['env'];
43 | if ($this->option('save')) {
44 | // save current env
45 | $targetPath = base_path().'/.'.$envir.'.env';
46 | File::put($targetPath, File::get($envPath), true);
47 | $this->info('Environmental config file .'.$envir.'.env saved');
48 | } else {
49 | // switch to a different env
50 | $targetEnv = $this->argument('env');
51 | $targetPath = base_path().'/.'.$targetEnv.'.env';
52 | if (!File::exists($targetPath)) {
53 | $this->error('Cannot switch to environment: '.$targetEnv.' because .'.$targetEnv.'.env doesn\'t exist');
54 | return;
55 | }
56 | File::put($envPath, File::get($targetPath), true);
57 | $this->info('Successfully switched from '.$envir.' to '.$targetEnv.'.');
58 | }
59 | }
60 |
61 | /**
62 | * Get the console command arguments.
63 | *
64 | * @return array
65 | */
66 | protected function getArguments()
67 | {
68 | return [
69 | ['env', InputArgument::OPTIONAL, 'The environment name to switch to'],
70 | ];
71 | }
72 |
73 | /**
74 | * Get the console command options.
75 | *
76 | * @return array
77 | */
78 | protected function getOptions()
79 | {
80 | return [
81 | ['save', null, InputOption::VALUE_NONE, 'Save the current .env to a \$APP_ENV.env file before switching.', null],
82 | ];
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/Console/Kernel.php:
--------------------------------------------------------------------------------
1 | command('inspire')
27 | ->hourly();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------