generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor optionBool method to set a default value in Commandable.php
- Loading branch information
1 parent
aabfde9
commit b806dd8
Showing
4 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters