Skip to content

Commit b806dd8

Browse files
Refactor optionBool method to set a default value in Commandable.php
1 parent aabfde9 commit b806dd8

File tree

4 files changed

+225
-1
lines changed

4 files changed

+225
-1
lines changed

src/Commands/Commandable.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function optionInt(string $option, ?int $default = null): ?int
7575
return (int) $this->optionArgument($option, $default);
7676
}
7777

78-
public function optionBool(string $option, ?bool $default = null): ?bool
78+
public function optionBool(string $option, ?bool $default = false): ?bool
7979
{
8080
return (bool) $this->optionArgument($option, $default);
8181
}

src/Commands/Db/DbTestCommand.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Kiwilan\Steward\Commands\Db;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Schema;
7+
use Kiwilan\Steward\Commands\Commandable;
8+
9+
class DbTestCommand extends Commandable
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'db:test
17+
{--c|credentials : Show database credentials}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Test database connection';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @return int
30+
*/
31+
public function handle()
32+
{
33+
$this->title();
34+
35+
$credentials = $this->optionBool('credentials');
36+
$success = false;
37+
38+
$this->info('Testing database connection...');
39+
$this->newLine();
40+
41+
$connection = Schema::getConnection();
42+
43+
$driver = $connection->getDriverName();
44+
$database = $connection->getDatabaseName();
45+
$host = $connection->getConfig('host');
46+
$port = $connection->getConfig('port');
47+
$username = $connection->getConfig('username');
48+
$password = $connection->getConfig('password');
49+
50+
$this->comment("Driver: {$driver}");
51+
$this->comment("Database: {$database}");
52+
$this->comment("Host: {$host}");
53+
$this->comment("Port: {$port}");
54+
55+
if ($credentials) {
56+
$this->comment("Username: {$username}");
57+
$this->comment("Password: {$password}");
58+
}
59+
60+
$this->newLine();
61+
62+
$this->comment("Try to ping database at {$host}:{$port}...");
63+
$available = $this->pingDatabase($host, $port);
64+
$this->info($available ? 'Database is available.' : 'Database is not available.');
65+
66+
$this->newLine();
67+
68+
if ($available) {
69+
$success = $this->testConnection($connection);
70+
} else {
71+
$this->comment('Try to get error information...');
72+
$this->getError($connection);
73+
74+
$success = false;
75+
}
76+
77+
if (! $success) {
78+
return Command::FAILURE;
79+
}
80+
81+
return Command::SUCCESS;
82+
}
83+
84+
private function pingDatabase(string $url, int|string $port): bool
85+
{
86+
$connection = @fsockopen($url, $port, $errno, $errstr, 1);
87+
if (is_resource($connection)) {
88+
fclose($connection);
89+
90+
return true;
91+
}
92+
93+
return false;
94+
}
95+
96+
private function getError(\Illuminate\Database\Connection $connection): void
97+
{
98+
try {
99+
$connection->getPdo();
100+
} catch (\Throwable $th) {
101+
$this->error($th->getMessage());
102+
}
103+
}
104+
105+
private function testConnection(\Illuminate\Database\Connection $connection): bool
106+
{
107+
$driverName = null;
108+
$serverInfo = null;
109+
$clientVersion = null;
110+
$serverVersion = null;
111+
$connectionStatus = null;
112+
113+
try {
114+
$pdo = $connection->getPdo();
115+
116+
$driverName = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
117+
$serverInfo = $pdo->getAttribute(\PDO::ATTR_SERVER_INFO);
118+
$clientVersion = $pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION);
119+
$serverVersion = $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
120+
$connectionStatus = $pdo->getAttribute(\PDO::ATTR_CONNECTION_STATUS);
121+
} catch (\Exception $e) {
122+
$this->error('Connection failed.');
123+
124+
return false;
125+
}
126+
127+
$this->alert('Connection successful.');
128+
$this->comment("Driver name: {$driverName}");
129+
$this->comment("Server info: {$serverInfo}");
130+
$this->comment("Client version: {$clientVersion}");
131+
$this->comment("Server version: {$serverVersion}");
132+
$this->comment("Connection status: {$connectionStatus}");
133+
134+
return true;
135+
}
136+
}
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Kiwilan\Steward\Commands\Setup;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Artisan;
7+
use Kiwilan\Steward\Commands\Commandable;
8+
9+
class SetupInstallCommand extends Commandable
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'setup:install
17+
{--p|production : run in production mode}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Execute main setup commands.';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @return int
30+
*/
31+
public function handle()
32+
{
33+
$this->title();
34+
35+
$production = $this->optionBool('production', false);
36+
37+
$this->info('Create .env file...');
38+
39+
if (! file_exists(base_path('.env'))) {
40+
copy(base_path('.env.example'), base_path('.env'));
41+
}
42+
43+
$this->info('Generating key...');
44+
45+
Artisan::call('key:generate', [
46+
'--force' => true,
47+
], $this->output);
48+
49+
if ($production) {
50+
$this->dotenvProduction();
51+
$this->info('Production mode enabled.');
52+
}
53+
54+
$this->info('Linking storage...');
55+
56+
Artisan::call('storage:link', [
57+
'--force' => true,
58+
], $this->output);
59+
60+
$this->info('Migrating database...');
61+
62+
Artisan::call('migrate', [
63+
'--force' => true,
64+
], $this->output);
65+
66+
$this->info('Seeding database...');
67+
68+
Artisan::call('db:seed', [
69+
'--force' => true,
70+
], $this->output);
71+
72+
$this->info('Done.');
73+
74+
return Command::SUCCESS;
75+
}
76+
77+
private function dotenvProduction(): void
78+
{
79+
$env = file_get_contents(base_path('.env'));
80+
81+
$env = str_replace('APP_ENV=local', 'APP_ENV=production', $env);
82+
$env = str_replace('APP_DEBUG=true', 'APP_DEBUG=false', $env);
83+
84+
file_put_contents(base_path('.env'), $env);
85+
}
86+
}

src/StewardServiceProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function configurePackage(Package $package): void
4444
\Kiwilan\Steward\Commands\Jobs\JobsClearCommand::class,
4545
\Kiwilan\Steward\Commands\Model\ModelBackupCommand::class,
4646
\Kiwilan\Steward\Commands\Model\ModelRestoreCommand::class,
47+
\Kiwilan\Steward\Commands\Setup\SetupInstallCommand::class,
48+
\Kiwilan\Steward\Commands\Db\DbTestCommand::class,
4749
]);
4850
}
4951

0 commit comments

Comments
 (0)