|
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
|
7 |
|
public function __construct(Filesystem $files) |
|
50
|
|
|
{ |
|
51
|
7 |
|
parent::__construct(); |
|
52
|
|
|
|
|
53
|
7 |
|
$this->files = $files; |
|
54
|
7 |
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function handle() |
|
57
|
|
|
{ |
|
58
|
3 |
|
$type = $this->argument('type'); |
|
59
|
3 |
|
$methodName = 'make' . Str::ucfirst(Str::camel($type)) . 'Migration'; |
|
|
|
|
|
|
60
|
3 |
|
if (!method_exists($this, $methodName)) { |
|
61
|
|
|
throw new \Exception("Migration type [{$type}] not supported"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
return $this->{$methodName}(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
protected function makeGeonamesMigration(): int |
|
68
|
|
|
{ |
|
69
|
3 |
|
$suffix = $this->option('suffix'); |
|
70
|
3 |
|
if ($suffix) { |
|
71
|
2 |
|
$suffix = Str::snake($suffix); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
$template = $this->templatesPath . 'create_geonames_table.php.stub'; |
|
75
|
3 |
|
$saveTo = database_path('migrations/' . date('Y_m_d_His', time()) . '_create_geonames_table' . ($suffix ? "_{$suffix}" : '') . '.php'); |
|
76
|
|
|
|
|
77
|
3 |
|
$this->files->copy($template, $saveTo); |
|
78
|
|
|
|
|
79
|
3 |
|
$text = $this->files->get($saveTo); |
|
80
|
3 |
|
$text = str_replace('/** class_suffix **/', $suffix ? Str::ucfirst(Str::camel($suffix)) : '', $text); |
|
81
|
3 |
|
$text = str_replace('/** table_suffix **/', $suffix ? " . '_{$suffix}'" : '', $text); |
|
82
|
3 |
|
$this->files->put($saveTo, $text); |
|
83
|
|
|
|
|
84
|
3 |
|
return 0; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|