1
|
|
|
<?php namespace Xethron\MigrationsGenerator\Syntax; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Config; |
4
|
|
|
use KitLoong\MigrationsGenerator\Generators\Decorator; |
5
|
|
|
use KitLoong\MigrationsGenerator\Generators\Platform; |
6
|
|
|
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting; |
7
|
|
|
use KitLoong\MigrationsGenerator\Repositories\MySQLRepository; |
8
|
|
|
use Way\Generators\Compilers\TemplateCompiler; |
9
|
|
|
use Way\Generators\Syntax\Table as WayTable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Table |
13
|
|
|
* @package Xethron\MigrationsGenerator\Syntax |
14
|
|
|
*/ |
15
|
|
|
abstract class Table extends WayTable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $table; |
21
|
|
|
|
22
|
|
|
private $mySQLRepository; |
23
|
|
|
|
24
|
|
|
public function __construct(MySQLRepository $mySQLRepository, TemplateCompiler $compiler, Decorator $decorator) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($compiler, $decorator); |
27
|
|
|
|
28
|
|
|
$this->mySQLRepository = $mySQLRepository; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function run(array $fields, string $table, string $connection, $method = 'table'): string |
32
|
|
|
{ |
33
|
|
|
$table = $this->decorator->tableWithoutPrefix($table); |
34
|
|
|
$this->table = $table; |
35
|
|
|
if ($connection !== Config::get('database.default')) { |
36
|
|
|
$method = 'connection(\''.$connection.'\')->'.$method; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$compiled = $this->compiler->compile($this->getTemplate($method), ['table' => $table, 'method' => $method]); |
40
|
|
|
|
41
|
|
|
$content = $this->getItems($fields); |
42
|
|
|
|
43
|
|
|
if ($method === 'create') { |
44
|
|
|
$tableCollation = $this->getTableCollation($table); |
45
|
|
|
if (!empty($tableCollation)) { |
46
|
|
|
$content = array_merge($tableCollation, $content); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->replaceFieldsWith($content, $compiled); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return string for adding all foreign keys |
55
|
|
|
* |
56
|
|
|
* @param array $items |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function getItems(array $items): array |
60
|
|
|
{ |
61
|
|
|
$result = []; |
62
|
|
|
foreach ($items as $item) { |
63
|
|
|
$result[] = $this->getItem($item); |
64
|
|
|
} |
65
|
|
|
return $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get table collation migration lines if not equal to DB collation. |
70
|
|
|
* |
71
|
|
|
* @param string $tableName |
72
|
|
|
* @return array|string[] |
73
|
|
|
*/ |
74
|
|
|
protected function getTableCollation(string $tableName): array |
75
|
|
|
{ |
76
|
|
|
$setting = app(MigrationsGeneratorSetting::class); |
77
|
|
|
if ($setting->getPlatform() === Platform::MYSQL) { |
78
|
|
|
$dbCollation = $this->mySQLRepository->getDatabaseCollation(); |
79
|
|
|
$tableCollation = $setting->getSchema()->listTableDetails($tableName)->getOptions()['collation']; |
80
|
|
|
|
81
|
|
|
if ($tableCollation !== $dbCollation['collation']) { |
82
|
|
|
$tableCharset = explode('_', $tableCollation)[0]; |
83
|
|
|
return [ |
84
|
|
|
'$table->charset = \''.$tableCharset.'\';', |
85
|
|
|
'$table->collation = \''.$tableCollation.'\';' |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return []; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $item |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
abstract protected function getItem(array $item): string; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string[] $decorators |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function addDecorators(array $decorators): string |
103
|
|
|
{ |
104
|
|
|
$output = ''; |
105
|
|
|
foreach ($decorators as $decorator) { |
106
|
|
|
$output .= sprintf("->%s", $decorator); |
107
|
|
|
// Do we need to tack on the parentheses? |
108
|
|
|
if (strpos($decorator, '(') === false) { |
109
|
|
|
$output .= '()'; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return $output; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|