From 3cedbf28c5c9d1d3e8f01d3a0fdfe49ca20bb71b Mon Sep 17 00:00:00 2001 From: Oleksii Prudkyi Date: Thu, 22 Dec 2016 11:55:11 +0200 Subject: [PATCH 1/3] proper env file use - load env file based on environment set via --env, i.e. --env=something will load settings from .env.something - support Laravel 5.1 (tested with 5.3) --- composer.json | 10 ++++----- src/Commands/SetupTestDb.php | 41 ++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 7848b12..e686ca5 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,11 @@ ], "require": { "php": ">=5.4.0", - "illuminate/support": "5.x", - "illuminate/config": "5.x", - "illuminate/console": "5.x", - "illuminate/database": "5.x", - "illuminate/filesystem": "5.x" + "illuminate/support": "^5.1", + "illuminate/config": "^5.1", + "illuminate/console": "^5.1", + "illuminate/database": "^5.1", + "illuminate/filesystem": "^5.1" }, "require-dev": { diff --git a/src/Commands/SetupTestDb.php b/src/Commands/SetupTestDb.php index fc9cd2c..9b88a6e 100644 --- a/src/Commands/SetupTestDb.php +++ b/src/Commands/SetupTestDb.php @@ -6,11 +6,11 @@ class SetupTestDb extends Command { /** - * The console command name. + * The name and signature of the console command. * * @var string */ - protected $name = 'db:seed-test'; + protected $signature = 'db:seed-test'; /** * The console command description. @@ -19,19 +19,47 @@ class SetupTestDb extends Command */ protected $description = 'Sets up and seeds db for testing once per execution to save on re-seeding'; + /** + * loads env file (.env.something) based on environment set via --env=something. + * + * @return void + */ + public function reloadEnvironment() + { + //$this->info("d1" . env('DB_CONNECTION')); + $this->info("Reload environment : " . \App::environment()); + putenv('APP_ENV=' . \App::environment()); + $this->laravel->make('Illuminate\Foundation\Bootstrap\DetectEnvironment')->bootstrap($this->laravel); + $envFile = \App::environmentFile(); + if($envFile != ".env." . \App::environment()) { + $envFile = ".env." . \App::environment(); + } + (new \Dotenv\Dotenv(\App::environmentPath(), $envFile ))->overload(); + $this->laravel->make('Illuminate\Foundation\Bootstrap\LoadConfiguration')->bootstrap($this->laravel); + //$this->info("loaded DB_DATABASE : " . env('DB_DATABASE')); + //$this->info("d2" . \App::environmentPath()); + //$this->info("d2" . \App::environmentFile()); + } + /** * Execute the console command. * * @return mixed */ - public function fire() + public function handle() { - $this->line("[{$this->name}] starting the seeding"); + $this->line("[{$this->signature}] starting the seeding"); + + $this->reloadEnvironment(); + $config = $this->config(); $defaultConn = $config->get('database.default'); + //$this->info("defaultConn :" . $defaultConn); $database = $config->get("database.connections.{$defaultConn}.database"); + //$this->info("database :" . $database); + $driver = $config->get("database.connections.{$defaultConn}.driver"); if ($driver !== 'sqlite') { $this->info("Non-file based db detected: $driver"); @@ -52,11 +80,12 @@ public function fire() ]; $this->artisan('db:seed', $options); - $this->line("[{$this->name}] db seeded!"); + $this->line("[{$this->signature}] db seeded!"); } private function createDb($dbPath) { + //$this->info("Delete: {$dbPath}"); $file = $this->fileSystem(); $file->delete($dbPath); $file->put($dbPath, ''); @@ -118,6 +147,6 @@ private function db() */ private function fileSystem() { - return $this->laravel['file']; + return $this->laravel['files']; } } From eb94f264d278009d62e5232bfd98b84676698438 Mon Sep 17 00:00:00 2001 From: Oleksii Prudkyi Date: Fri, 23 Dec 2016 11:18:49 +0200 Subject: [PATCH 2/3] remove reloadEnvironment() as it unneeded with Laravel 5.3 --- src/Commands/SetupTestDb.php | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/Commands/SetupTestDb.php b/src/Commands/SetupTestDb.php index 9b88a6e..525b85f 100644 --- a/src/Commands/SetupTestDb.php +++ b/src/Commands/SetupTestDb.php @@ -19,28 +19,6 @@ class SetupTestDb extends Command */ protected $description = 'Sets up and seeds db for testing once per execution to save on re-seeding'; - /** - * loads env file (.env.something) based on environment set via --env=something. - * - * @return void - */ - public function reloadEnvironment() - { - //$this->info("d1" . env('DB_CONNECTION')); - $this->info("Reload environment : " . \App::environment()); - putenv('APP_ENV=' . \App::environment()); - $this->laravel->make('Illuminate\Foundation\Bootstrap\DetectEnvironment')->bootstrap($this->laravel); - $envFile = \App::environmentFile(); - if($envFile != ".env." . \App::environment()) { - $envFile = ".env." . \App::environment(); - } - (new \Dotenv\Dotenv(\App::environmentPath(), $envFile ))->overload(); - $this->laravel->make('Illuminate\Foundation\Bootstrap\LoadConfiguration')->bootstrap($this->laravel); - //$this->info("loaded DB_DATABASE : " . env('DB_DATABASE')); - //$this->info("d2" . \App::environmentPath()); - //$this->info("d2" . \App::environmentFile()); - } - /** * Execute the console command. * @@ -50,9 +28,6 @@ public function handle() { $this->line("[{$this->signature}] starting the seeding"); - $this->reloadEnvironment(); - - $config = $this->config(); $defaultConn = $config->get('database.default'); From 6311cb8e55ea50e156640ce1d4f513116bac62f2 Mon Sep 17 00:00:00 2001 From: Oleksii Prudkyi Date: Tue, 27 Dec 2016 22:30:29 +0200 Subject: [PATCH 3/3] remove unneded changes --- composer.json | 10 +++++----- src/Commands/SetupTestDb.php | 14 +++++--------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index e686ca5..7848b12 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,11 @@ ], "require": { "php": ">=5.4.0", - "illuminate/support": "^5.1", - "illuminate/config": "^5.1", - "illuminate/console": "^5.1", - "illuminate/database": "^5.1", - "illuminate/filesystem": "^5.1" + "illuminate/support": "5.x", + "illuminate/config": "5.x", + "illuminate/console": "5.x", + "illuminate/database": "5.x", + "illuminate/filesystem": "5.x" }, "require-dev": { diff --git a/src/Commands/SetupTestDb.php b/src/Commands/SetupTestDb.php index 525b85f..b06b534 100644 --- a/src/Commands/SetupTestDb.php +++ b/src/Commands/SetupTestDb.php @@ -6,11 +6,11 @@ class SetupTestDb extends Command { /** - * The name and signature of the console command. + * The console command name. * * @var string */ - protected $signature = 'db:seed-test'; + protected $name = 'db:seed-test'; /** * The console command description. @@ -24,17 +24,14 @@ class SetupTestDb extends Command * * @return mixed */ - public function handle() + public function fire() { - $this->line("[{$this->signature}] starting the seeding"); + $this->line("[{$this->name}] starting the seeding"); $config = $this->config(); $defaultConn = $config->get('database.default'); - //$this->info("defaultConn :" . $defaultConn); $database = $config->get("database.connections.{$defaultConn}.database"); - //$this->info("database :" . $database); - $driver = $config->get("database.connections.{$defaultConn}.driver"); if ($driver !== 'sqlite') { $this->info("Non-file based db detected: $driver"); @@ -55,12 +52,11 @@ public function handle() ]; $this->artisan('db:seed', $options); - $this->line("[{$this->signature}] db seeded!"); + $this->line("[{$this->name}] db seeded!"); } private function createDb($dbPath) { - //$this->info("Delete: {$dbPath}"); $file = $this->fileSystem(); $file->delete($dbPath); $file->put($dbPath, '');