1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelMultiLang\Console; |
6
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
|
10
|
|
|
use function date; |
11
|
|
|
use function file_exists; |
12
|
|
|
use function file_get_contents; |
13
|
|
|
use function file_put_contents; |
14
|
|
|
use function str_replace; |
15
|
|
|
|
16
|
|
|
use const PHP_EOL; |
17
|
|
|
|
18
|
|
|
class MigrationCommand extends Command |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The console command name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $name = 'multilang:migration'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Creates a migration following the multilang specifications.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function handle() |
40
|
|
|
{ |
41
|
|
|
$table = Config::get('multilang.db.texts_table'); |
42
|
|
|
|
43
|
|
|
if ($table === '') { |
44
|
|
|
$this->error('Couldn\'t create migration.' . PHP_EOL . 'Table name can\'t be empty. Check your configuration.'); |
45
|
|
|
|
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->line(''); |
50
|
|
|
$this->info('Tables: ' . $table); |
51
|
|
|
|
52
|
|
|
$message = 'A migration that creates "' . $table . '" tables will be created in database/migrations directory'; |
53
|
|
|
|
54
|
|
|
$this->comment($message); |
55
|
|
|
$this->line(''); |
56
|
|
|
|
57
|
|
|
if ($this->confirm('Proceed with the migration creation? [Yes|no]', true)) { |
58
|
|
|
$this->line(''); |
59
|
|
|
|
60
|
|
|
$this->info('Creating migration...'); |
61
|
|
|
if ($this->createMigration($table)) { |
62
|
|
|
$this->info('Migration successfully created!'); |
63
|
|
|
} else { |
64
|
|
|
$this->error( |
65
|
|
|
'Couldn\'t create migration.' . PHP_EOL . ' Check the write permissions |
66
|
|
|
within the database/migrations directory.', |
67
|
|
|
); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->line(''); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create the migration. |
76
|
|
|
* |
77
|
|
|
* @param string $table |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
protected function createMigration(string $table): bool |
81
|
|
|
{ |
82
|
|
|
$migrationFile = base_path('database/migrations') . '/' . date('Y_m_d_His') . '_create_multi_lang_texts_table.php'; |
83
|
|
|
|
84
|
|
|
if (file_exists($migrationFile)) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$stubPath = __DIR__ . '/../../stubs/migrations/texts.stub'; |
89
|
|
|
$content = file_get_contents($stubPath); |
90
|
|
|
if (empty($content)) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$data = str_replace('{{TEXTS_TABLE}}', $table, $content); |
95
|
|
|
|
96
|
|
|
if (! file_put_contents($migrationFile, $data)) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|