1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Commands; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use DB; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
use function is_dir; |
14
|
|
|
use function mkdir; |
15
|
|
|
|
16
|
|
|
use const DIRECTORY_SEPARATOR; |
17
|
|
|
|
18
|
|
|
class DbDump extends Command |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'db:dump {--database= : The database connection to use.} |
26
|
|
|
{--path= : Folder path for store database dump files.}'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Dump database to sql file using mysqldump CLI utility.'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function handle(): void |
41
|
|
|
{ |
42
|
|
|
$dbConn = $this->getDatabase(); |
43
|
|
|
$connection = DB::connection($dbConn); |
44
|
|
|
$dbName = $connection->getConfig('database'); |
45
|
|
|
$filename = $dbName . '_' . Carbon::now()->format('Ymd_His') . '.sql'; |
46
|
|
|
|
47
|
|
|
$path = $this->getPath($filename); |
48
|
|
|
|
49
|
|
|
$process = new Process('mysqldump --host=' . $connection->getConfig('host') . ' --user=' . $connection->getConfig('username') . ' --password=' . $connection->getConfig('password') . ' ' . $dbName . ' > ' . $path); |
|
|
|
|
50
|
|
|
$process->run(); |
51
|
|
|
|
52
|
|
|
// Executes after the command finishes |
53
|
|
|
if (! $process->isSuccessful()) { |
54
|
|
|
throw new ProcessFailedException($process); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->info('Database backup saved to: ' . $path); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getPath(string $filename): string |
61
|
|
|
{ |
62
|
|
|
$path = $this->input->getOption('path'); |
63
|
|
|
if ($path) { |
64
|
|
|
if (! is_dir(base_path($path))) { |
65
|
|
|
mkdir(base_path($path), 0777, true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$path = base_path($path . DIRECTORY_SEPARATOR . $filename); |
69
|
|
|
} else { |
70
|
|
|
$path = storage_path($filename); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $path; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getDatabase(): string |
77
|
|
|
{ |
78
|
|
|
$database = $this->input->getOption('database'); |
79
|
|
|
|
80
|
|
|
return $database ?: $this->laravel['config']['database.default']; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: