1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Commands; |
6
|
|
|
|
7
|
|
|
use DB; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Console\ConfirmableTrait; |
10
|
|
|
|
11
|
|
|
use function file_exists; |
12
|
|
|
use function file_get_contents; |
13
|
|
|
|
14
|
|
|
class DbRestore extends Command |
15
|
|
|
{ |
16
|
|
|
use ConfirmableTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'db:restore {file : Dump file path to restore.} |
24
|
|
|
{--database= : The database connection to use.} |
25
|
|
|
{--force : Force the operation to run when in production.}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Restore mysql dump.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function handle(): void |
40
|
|
|
{ |
41
|
|
|
if (! $this->confirmToProceed('Application In Production! Will be imported sql file!')) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$dbConn = $this->getDatabase(); |
46
|
|
|
$connection = DB::connection($dbConn); |
47
|
|
|
|
48
|
|
|
$path = $this->getFilePath(); |
49
|
|
|
if (! file_exists($path)) { |
50
|
|
|
$this->error('File ' . $path . ' not found!'); |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$connection->unprepared(file_get_contents($path)); |
56
|
|
|
|
57
|
|
|
$this->info('Database backup restored successfully'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getDatabase(): string |
61
|
|
|
{ |
62
|
|
|
$database = $this->input->getOption('database'); |
63
|
|
|
|
64
|
|
|
return $database ?: $this->laravel['config']['database.default']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getFilePath(): string |
68
|
|
|
{ |
69
|
|
|
$file = $this->input->getArgument('file'); |
70
|
|
|
|
71
|
|
|
return base_path($file); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.