We are going to create a command with Laravel that will allow us to leave our project ready to use simply with an artisan command.
First, we have to create the file, as usually, Laravel provide us an artisan command to do it:
1 |
php artisan make:command BlogInstaller |
Open the file and put this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use DB; class BlogInstaller extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'blog:install'; /** * The console command description. * * @var string */ protected $description = 'Install the blog'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $this->line('Let\'s install the blog!'); $this->line(' '); $this->call('config:cache'); $this->info("Database configuration..."); $dbName = $this->ask('Enter the database name'); $dbUser = $this->ask('Enter the database user', 'root'); $dbPassword = $this->ask('Enter the database password', false); if($dbPassword == false) { $dbPassword = ''; } // http://laravel-tricks.com/tricks/change-the-env-dynamically $env_update = $this->changeEnv([ 'DB_DATABASE' => $dbName, 'DB_USERNAME' => $dbUser, 'DB_PASSWORD' => $dbPassword ]); if ( $env_update ) { $this->call('migrate'); $this->call('db:seed'); $this->line('Tables and fake data were created!'); $this->line('You can see the user in the database. Every test user has the same password that it is 123456.'); $this->line(' '); $this->info('Publishing vendor packages...'); $this->call('vendor:publish'); $this->line(' '); $this->info('Installing npm packages and compiling assets...'); system('npm install'); $this->line('npm packages installed!'); system('npm run dev'); $this->line('Assests compiled as development environment! If you have in production you will have to run npm run prod!'); $this->line(' '); $this->line('Blog installed!'); } else { $this->line('Error!'); } } // http://laravel-tricks.com/tricks/change-the-env-dynamically protected function changeEnv($data = array()){ if(count($data) > 0){ $filenameEnv = (file_exists(base_path(".env"))) ? '.env' : '.env.example'; // Read .env-file $env = file_get_contents(base_path() . '/'.$filenameEnv); // Split string on every " " and write into array $env = preg_split('/\s+/', $env);; // Loop through given data foreach((array)$data as $key => $value){ // Loop through .env-data foreach($env as $env_key => $env_value){ // Turn the value into an array and stop after the first split // So it's not possible to split e.g. the App-Key by accident $entry = explode("=", $env_value, 2); // Check, if new key fits the actual .env-key if($entry[0] == $key){ // If yes, overwrite it with the new one $env[$env_key] = $key . "=" . $value; } else { // If not, keep the old one $env[$env_key] = $env_value; } } } // Turn the array back to an String $env = implode("\n", $env); // And overwrite the .env with the new data file_put_contents(base_path() . '/'.$filenameEnv, $env); return true; } else { return false; } } } |
- The variable $signature indicates the artisan command that we must set to execute.
- The handle function is what the command executes (cleans cache, asks for data, inserts them in .env, executes migrations and seeds, executes npm and compiles assets).
- The changeEnv function I got it from laravel-tricks.com and what it does is changing the indicated variables in the .env file. I have add one line to detect if the .env file exists, if not it takes .env.example.
As I said, in my case, I have named it blog: installer, so we must run:
1 |
php artisan blog:installer |
Simply with this command, it should install and execute everything you need (db, publish configs, compilation assets, etc.). Obviously, the code can be improved a lot, but this entry is simply an example of how it could be done.
Remember that before executing the command you must create the database.
-
– http://laravel-tricks.com/tricks/change-the-env-dynamically