Skip to content

Commit

Permalink
Refactor optionBool method to set a default value in Commandable.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Oct 9, 2024
1 parent aabfde9 commit b806dd8
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Commands/Commandable.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function optionInt(string $option, ?int $default = null): ?int
return (int) $this->optionArgument($option, $default);
}

public function optionBool(string $option, ?bool $default = null): ?bool
public function optionBool(string $option, ?bool $default = false): ?bool
{
return (bool) $this->optionArgument($option, $default);
}
Expand Down
136 changes: 136 additions & 0 deletions src/Commands/Db/DbTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Kiwilan\Steward\Commands\Db;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Kiwilan\Steward\Commands\Commandable;

class DbTestCommand extends Commandable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:test
{--c|credentials : Show database credentials}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Test database connection';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->title();

$credentials = $this->optionBool('credentials');
$success = false;

$this->info('Testing database connection...');
$this->newLine();

$connection = Schema::getConnection();

$driver = $connection->getDriverName();
$database = $connection->getDatabaseName();
$host = $connection->getConfig('host');
$port = $connection->getConfig('port');
$username = $connection->getConfig('username');
$password = $connection->getConfig('password');

$this->comment("Driver: {$driver}");
$this->comment("Database: {$database}");
$this->comment("Host: {$host}");
$this->comment("Port: {$port}");

if ($credentials) {
$this->comment("Username: {$username}");
$this->comment("Password: {$password}");
}

$this->newLine();

$this->comment("Try to ping database at {$host}:{$port}...");
$available = $this->pingDatabase($host, $port);
$this->info($available ? 'Database is available.' : 'Database is not available.');

$this->newLine();

if ($available) {
$success = $this->testConnection($connection);
} else {
$this->comment('Try to get error information...');
$this->getError($connection);

$success = false;
}

if (! $success) {
return Command::FAILURE;
}

return Command::SUCCESS;
}

private function pingDatabase(string $url, int|string $port): bool
{
$connection = @fsockopen($url, $port, $errno, $errstr, 1);
if (is_resource($connection)) {
fclose($connection);

return true;
}

return false;
}

private function getError(\Illuminate\Database\Connection $connection): void
{
try {
$connection->getPdo();
} catch (\Throwable $th) {
$this->error($th->getMessage());
}
}

private function testConnection(\Illuminate\Database\Connection $connection): bool
{
$driverName = null;
$serverInfo = null;
$clientVersion = null;
$serverVersion = null;
$connectionStatus = null;

try {
$pdo = $connection->getPdo();

$driverName = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
$serverInfo = $pdo->getAttribute(\PDO::ATTR_SERVER_INFO);
$clientVersion = $pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION);
$serverVersion = $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
$connectionStatus = $pdo->getAttribute(\PDO::ATTR_CONNECTION_STATUS);
} catch (\Exception $e) {
$this->error('Connection failed.');

return false;
}

$this->alert('Connection successful.');
$this->comment("Driver name: {$driverName}");
$this->comment("Server info: {$serverInfo}");
$this->comment("Client version: {$clientVersion}");
$this->comment("Server version: {$serverVersion}");
$this->comment("Connection status: {$connectionStatus}");

return true;
}
}
86 changes: 86 additions & 0 deletions src/Commands/Setup/SetupInstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Kiwilan\Steward\Commands\Setup;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Kiwilan\Steward\Commands\Commandable;

class SetupInstallCommand extends Commandable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'setup:install
{--p|production : run in production mode}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Execute main setup commands.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->title();

$production = $this->optionBool('production', false);

$this->info('Create .env file...');

if (! file_exists(base_path('.env'))) {
copy(base_path('.env.example'), base_path('.env'));
}

$this->info('Generating key...');

Artisan::call('key:generate', [
'--force' => true,
], $this->output);

if ($production) {
$this->dotenvProduction();
$this->info('Production mode enabled.');
}

$this->info('Linking storage...');

Artisan::call('storage:link', [
'--force' => true,
], $this->output);

$this->info('Migrating database...');

Artisan::call('migrate', [
'--force' => true,
], $this->output);

$this->info('Seeding database...');

Artisan::call('db:seed', [
'--force' => true,
], $this->output);

$this->info('Done.');

return Command::SUCCESS;
}

private function dotenvProduction(): void
{
$env = file_get_contents(base_path('.env'));

$env = str_replace('APP_ENV=local', 'APP_ENV=production', $env);
$env = str_replace('APP_DEBUG=true', 'APP_DEBUG=false', $env);

file_put_contents(base_path('.env'), $env);
}
}
2 changes: 2 additions & 0 deletions src/StewardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function configurePackage(Package $package): void
\Kiwilan\Steward\Commands\Jobs\JobsClearCommand::class,
\Kiwilan\Steward\Commands\Model\ModelBackupCommand::class,
\Kiwilan\Steward\Commands\Model\ModelRestoreCommand::class,
\Kiwilan\Steward\Commands\Setup\SetupInstallCommand::class,
\Kiwilan\Steward\Commands\Db\DbTestCommand::class,
]);
}

Expand Down

0 comments on commit b806dd8

Please sign in to comment.