1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: liow.kitloong |
5
|
|
|
* Date: 2020/03/29 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace KitLoong\MigrationsGenerator\Generators; |
9
|
|
|
|
10
|
|
|
use Doctrine\DBAL\Schema\Index; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\IndexType; |
13
|
|
|
|
14
|
|
|
class IndexGenerator |
15
|
|
|
{ |
16
|
|
|
private $decorator; |
17
|
|
|
|
18
|
39 |
|
public function __construct(Decorator $decorator) |
19
|
|
|
{ |
20
|
39 |
|
$this->decorator = $decorator; |
21
|
39 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $table |
25
|
|
|
* @param Index[] $indexes |
26
|
|
|
* @param bool $ignoreIndexNames |
27
|
|
|
* @return Collection[] |
28
|
|
|
*/ |
29
|
21 |
|
public function generate(string $table, $indexes, bool $ignoreIndexNames): array |
30
|
|
|
{ |
31
|
21 |
|
$singleColIndexes = collect([]); |
32
|
21 |
|
$multiColIndexes = collect([]); |
33
|
|
|
|
34
|
21 |
|
foreach ($indexes as $index) { |
35
|
|
|
$indexField = [ |
36
|
21 |
|
'field' => array_map([$this->decorator, 'addSlash'], $index->getColumns()), |
37
|
21 |
|
'type' => IndexType::INDEX, |
38
|
|
|
'args' => [], |
39
|
|
|
]; |
40
|
|
|
|
41
|
21 |
|
if ($index->isPrimary()) { |
42
|
3 |
|
$indexField['type'] = IndexType::PRIMARY; |
43
|
18 |
|
} elseif ($index->isUnique()) { |
44
|
3 |
|
$indexField['type'] = IndexType::UNIQUE; |
45
|
15 |
|
} elseif (count($index->getFlags()) > 0 && |
46
|
11 |
|
in_array('spatial', $index->getFlags())) { |
47
|
3 |
|
$indexField['type'] = IndexType::SPATIAL_INDEX; |
48
|
|
|
} |
49
|
|
|
|
50
|
21 |
|
if (!$ignoreIndexNames && !$this->useLaravelStyleDefaultName($table, $index, $indexField['type'])) { |
51
|
15 |
|
$indexField['args'][] = $this->decorateName($index->getName()); |
52
|
|
|
} |
53
|
|
|
|
54
|
21 |
|
if (count($index->getColumns()) === 1) { |
55
|
12 |
|
$singleColIndexes->put($this->decorator->addSlash($index->getColumns()[0]), $indexField); |
56
|
|
|
} else { |
57
|
9 |
|
$multiColIndexes->push($indexField); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
21 |
|
return ['single' => $singleColIndexes, 'multi' => $multiColIndexes]; |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
private function getLaravelStyleDefaultName(string $table, array $columns, string $type): string |
65
|
|
|
{ |
66
|
18 |
|
if ($type === IndexType::PRIMARY) { |
67
|
3 |
|
return 'PRIMARY'; |
68
|
|
|
} |
69
|
|
|
|
70
|
15 |
|
$index = strtolower($table.'_'.implode('_', $columns).'_'.$type); |
71
|
15 |
|
return str_replace(['-', '.'], '_', $index); |
72
|
|
|
} |
73
|
|
|
|
74
|
18 |
|
private function useLaravelStyleDefaultName(string $table, Index $index, string $type): bool |
75
|
|
|
{ |
76
|
18 |
|
return $this->getLaravelStyleDefaultName($table, $index->getColumns(), $type) === $index->getName(); |
77
|
|
|
} |
78
|
|
|
|
79
|
15 |
|
private function decorateName(string $name): string |
80
|
|
|
{ |
81
|
15 |
|
return "'".$this->decorator->addSlash($name)."'"; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|