Skip to content

Instantly share code, notes, and snippets.

@qadirpervez
Last active July 13, 2022 13:44
Show Gist options
  • Select an option

  • Save qadirpervez/b9e24d5d1093d50d0c3181655bb76d07 to your computer and use it in GitHub Desktop.

Select an option

Save qadirpervez/b9e24d5d1093d50d0c3181655bb76d07 to your computer and use it in GitHub Desktop.

Revisions

  1. qadirpervez revised this gist Mar 17, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions final_AppInstaller.php
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,7 @@ public function handle()
    }

    $this->alert('Application is installed successfully.');
    return 1;
    }


  2. qadirpervez created this gist Mar 17, 2022.
    155 changes: 155 additions & 0 deletions final_AppInstaller.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,155 @@
    <?php

    namespace App\Console\Commands;

    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\File;
    use Illuminate\Support\Facades\Artisan;
    use Illuminate\Support\Facades\Config;
    use Illuminate\Support\Facades\DB;

    class AppInstaller extends Command
    {
    /**
    * The name and signature of the console command.
    *
    * @var string
    */
    protected $signature = 'app:install
    {--db-host=localhost : Database Host}
    {--db-port=3306 : Port for the database}
    {--db-database= : Name for the database}
    {--db-username=root : Username for accessing the database}
    {--db-password= : Password for accessing the database, it can be blank}
    ';

    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = 'To install the application via CLI';

    /**
    * Execute the console command.
    *
    * @return int
    */
    public function handle()
    {
    if ($this->missingRequiredOptions()) {
    $this->error('Missing required options');
    $this->line('please run');
    $this->line('php artisan app:install --help');
    $this->line('to see the command usage.');
    return 0;
    }

    $this->alert('Application is installing...');

    static::copyEnvExampleToEnv();
    $this->generateAppKey();
    $this->updateEnvVariablesFromOptions();

    $this->info('Env file created successfully.');

    $this->info('Runnning migrations and seeders...');

    if (!static::runMigrationsWithSeeders()) {
    $this->error('Your database credentials are wrong!');
    return 0;
    }

    $this->alert('Application is installed successfully.');
    }


    public function missingRequiredOptions()
    {
    return !$this->option('db-database');
    }

    public static function copyEnvExampleToEnv()
    {

    if (!is_file(base_path('.env')) && is_file(base_path('.env.example'))) {
    File::copy(base_path('.env.example'), base_path('.env'));
    }
    }

    public static function generateAppKey()
    {
    Artisan::call('key:generate');
    }

    public function updateEnvVariablesFromOptions()
    {
    $this->updateEnv([
    'DB_HOST' => $this->option('db-host'),
    'DB_PORT' => $this->option('db-port'),
    'DB_DATABASE' => $this->option('db-database'),
    'DB_USERNAME' => $this->option('db-username'),
    'DB_PASSWORD' => $this->option('db-password'),
    ]);

    $conn = config('database.default', 'mysql');

    $dbConfig = Config::get("database.connections.$conn");

    $dbConfig['host'] = $this->option('db-host');
    $dbConfig['port'] = $this->option('db-port');
    $dbConfig['database'] = $this->option('db-database');
    $dbConfig['username'] = $this->option('db-username');
    $dbConfig['password'] = $this->option('db-password');

    Config::set("database.connections.$conn", $dbConfig);

    DB::purge($conn);
    DB::reconnect($conn);
    }

    public static function updateEnv($data)
    {
    $env = file_get_contents(base_path('.env'));

    $env = explode("\n", $env);

    foreach ($data as $dataKey => $dataValue) {
    $alreadyExistInEnv = false;

    foreach ($env as $envKey => $envValue) {
    $entry = explode('=', $envValue, 2);

    // Check if exists or not in env file
    if ($entry[0] == $dataKey) {
    $env[$envKey] = $dataKey . '=' . $dataValue;
    $alreadyExistInEnv = true;
    } else {
    $env[$envKey] = $envValue;
    }
    }

    // add the variable if not exists in env
    if (!$alreadyExistInEnv) {
    $env[] = $dataKey . '=' . $dataValue;
    }
    }

    $env = implode("\n", $env);

    file_put_contents(base_path('.env'), $env);

    return true;
    }

    public static function runMigrationsWithSeeders()
    {
    try {
    Artisan::call('migrate:fresh', ['--force' => true]);
    Artisan::call('db:seed', ['--force' => true]);
    } catch (\Exception $e) {
    return false;
    }
    return true;
    }
    }