1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Longman\TelegramBot\Console; |
5
|
|
|
|
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
use function array_merge; |
9
|
|
|
use function collect; |
10
|
|
|
|
11
|
|
|
use const DIRECTORY_SEPARATOR; |
12
|
|
|
|
13
|
|
|
class MigrateCommand extends Command |
14
|
|
|
{ |
15
|
|
|
protected $signature = 'migrate |
16
|
|
|
{--path= : The path to the migrations files to be executed} |
17
|
|
|
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} |
18
|
|
|
{--pretend : Dump the SQL queries that would be run} |
19
|
|
|
{--step : Force the migrations to be run so they can be rolled back individually}'; |
20
|
|
|
|
21
|
|
|
protected $description = 'Run the database migrations'; |
22
|
|
|
|
23
|
|
|
/** @var \Illuminate\Database\Migrations\Migrator */ |
24
|
|
|
protected $migrator; |
25
|
|
|
|
26
|
|
|
public function handle(): void |
27
|
|
|
{ |
28
|
|
|
$app = $this->getApplication()->getLaravel(); |
|
|
|
|
29
|
|
|
$this->migrator = $app['migrator']; |
30
|
|
|
|
31
|
|
|
$this->prepareDatabase(); |
32
|
|
|
|
33
|
|
|
$this->migrator->setOutput($this->output) |
34
|
|
|
->run($this->getMigrationPaths(), [ |
35
|
|
|
'pretend' => $this->option('pretend'), |
36
|
|
|
'step' => $this->option('step'), |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function prepareDatabase(): void |
41
|
|
|
{ |
42
|
|
|
$this->migrator->setConnection('default'); |
43
|
|
|
|
44
|
|
|
if (! $this->migrator->repositoryExists()) { |
45
|
|
|
$this->call('migrate:install'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getMigrationPaths(): array |
50
|
|
|
{ |
51
|
|
|
// Here, we will check to see if a path option has been defined. If it has we will |
52
|
|
|
// use the path relative to the root of the installation folder so our database |
53
|
|
|
// migrations may be run for any customized path from within the application. |
54
|
|
|
if ($this->input->hasOption('path') && $this->option('path')) { |
55
|
|
|
return collect($this->option('path'))->map(function ($path) { |
56
|
|
|
return ! $this->usingRealPath() ? $this->laravel->basePath() . '/' . $path : $path; |
57
|
|
|
})->all(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return array_merge( |
61
|
|
|
$this->migrator->paths(), |
62
|
|
|
[$this->getMigrationPath()] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function usingRealPath(): bool |
67
|
|
|
{ |
68
|
|
|
return $this->input->hasOption('realpath') && $this->option('realpath'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getMigrationPath(): string |
72
|
|
|
{ |
73
|
|
|
return $this->laravel->basePath() . DIRECTORY_SEPARATOR . 'migrations'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: