1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraGeoData\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class MakeMigrationCommand extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'geonames:make:migration |
17
|
|
|
{type : Migration type. } |
18
|
|
|
{--suffix= : Suffix used for specify country if need. } |
19
|
|
|
'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Publish specific migrations.'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Path where stored all templates. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected string $templatesPath = __DIR__ . '/../../../database/migrations/'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The filesystem instance. |
37
|
|
|
* |
38
|
|
|
* @var Filesystem |
39
|
|
|
*/ |
40
|
|
|
protected Filesystem $files; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new command instance. |
44
|
|
|
* |
45
|
|
|
* @param Filesystem $files |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
6 |
|
public function __construct(Filesystem $files) |
50
|
|
|
{ |
51
|
6 |
|
parent::__construct(); |
52
|
|
|
|
53
|
6 |
|
$this->files = $files; |
54
|
6 |
|
} |
55
|
|
|
|
56
|
2 |
|
public function handle() |
57
|
|
|
{ |
58
|
2 |
|
$type = $this->argument('type'); |
59
|
2 |
|
$methodName = 'make' . Str::ucfirst(Str::camel($type)) . 'Migration'; |
|
|
|
|
60
|
2 |
|
if (!method_exists($this, $methodName)) { |
61
|
|
|
throw new \Exception("Migration type [{$type}] not supported"); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
return $this->{$methodName}(); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
protected function makeGeonamesMigration(): int |
68
|
|
|
{ |
69
|
2 |
|
$suffix = $this->option('suffix'); |
70
|
2 |
|
if ($suffix) { |
71
|
1 |
|
$suffix = Str::snake($suffix); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$template = $this->templatesPath . 'create_geonames_table.php.stub'; |
75
|
2 |
|
$saveTo = database_path('migrations/' . date('Y_m_d_His', time()) . '_create_geonames_table' . ($suffix ? "_{$suffix}" : '') . '.php'); |
76
|
|
|
|
77
|
2 |
|
$this->files->copy($template, $saveTo); |
78
|
|
|
|
79
|
2 |
|
$text = $this->files->get($saveTo); |
80
|
2 |
|
$text = str_replace('/** class_suffix **/', $suffix ? Str::ucfirst(Str::camel($suffix)) : '', $text); |
81
|
2 |
|
$text = str_replace('/** table_suffix **/', $suffix ? " . '_{$suffix}'" : '', $text); |
82
|
2 |
|
$this->files->put($saveTo, $text); |
83
|
|
|
|
84
|
2 |
|
return 0; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|