|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Reedware\LaravelBlueprints\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
trait Indexes |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Creates a Foreign Key for this Blueprint. |
|
11
|
|
|
* |
|
12
|
|
|
* @param string|array $columns |
|
13
|
|
|
* @param string $name |
|
14
|
|
|
* |
|
15
|
|
|
* @return \Illuminate\Support\Fluent |
|
16
|
|
|
*/ |
|
17
|
|
|
public function foreign($columns, $name = null, $on = null) |
|
18
|
|
|
{ |
|
19
|
|
|
// Index the Foreign Key |
|
20
|
|
|
$index = $this->indexCommand('foreign', $columns, $name, $on); |
|
21
|
|
|
|
|
22
|
|
|
// Automatically Reference ID |
|
23
|
|
|
$index->references('id'); |
|
24
|
|
|
|
|
25
|
|
|
// Declare ON if specified |
|
26
|
|
|
if(!is_null($on)) { |
|
27
|
|
|
$index->on($on); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// Return the Index |
|
31
|
|
|
return $index; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Creates a new Index on this Table. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $type |
|
38
|
|
|
* @param string|array $columns |
|
39
|
|
|
* @param string $index |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Illuminate\Support\Fluent |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function indexCommand($type, $columns, $index, $foreign = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$columns = (array) $columns; |
|
46
|
|
|
|
|
47
|
|
|
// If no name was specified for this index, we will create one using a basic |
|
48
|
|
|
// convention of the table name, followed by the columns, followed by an |
|
49
|
|
|
// index type, such as primary or index, which makes the index unique. |
|
50
|
|
|
if (is_null($index)) { |
|
|
|
|
|
|
51
|
|
|
$index = $this->createIndexName($type, $columns, $foreign); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->addCommand($type, compact('index', 'columns')); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create a default index name for the table. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $type |
|
61
|
|
|
* @param array $columns |
|
62
|
|
|
* @param string|null $foreign |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function createIndexName($type, array $columns, $foreign = null) |
|
67
|
|
|
{ |
|
68
|
|
|
// Determine the Type Abbreviation |
|
69
|
|
|
$abbreviation = $this->createIndexTypeName($type); |
|
70
|
|
|
|
|
71
|
|
|
// Determine the Foreign Key Table Reference |
|
72
|
|
|
if($type == 'foreign' && is_null($foreign)) { |
|
73
|
|
|
$foreign = $this->createForeignIndexName($columns); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Convert the Columns |
|
77
|
|
|
$columns = implode('_', $columns); |
|
78
|
|
|
|
|
79
|
|
|
// Determine the Index Name |
|
80
|
|
|
if(isset($foreign)) { |
|
81
|
|
|
$index = $abbreviation . strtolower("_{$this->table}_{$foreign}_{$columns}"); |
|
82
|
|
|
} else { |
|
83
|
|
|
$index = $abbreviation . strtolower("_{$this->table}_{$columns}"); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Replace Illegal Characters |
|
87
|
|
|
return str_replace(['-', '.'], '_', $index); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the Index Type Abbreviation for the specified Type. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function createIndexTypeName($type) |
|
98
|
|
|
{ |
|
99
|
|
|
// Convert Type to Abbreviation |
|
100
|
|
|
switch($type) { |
|
101
|
|
|
|
|
102
|
|
|
// Primary Key |
|
103
|
|
|
case 'primary': |
|
104
|
|
|
return 'PK'; |
|
105
|
|
|
|
|
106
|
|
|
// Unique Key |
|
107
|
|
|
case 'unique': |
|
108
|
|
|
return 'UX'; |
|
109
|
|
|
|
|
110
|
|
|
// Foreign Key |
|
111
|
|
|
case 'foreign': |
|
112
|
|
|
return 'FK'; |
|
113
|
|
|
|
|
114
|
|
|
// General Index |
|
115
|
|
|
case 'index': |
|
116
|
|
|
return 'IX'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Unknown Type |
|
120
|
|
|
return $type; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the Foreign Key Table Reference for the specified Columns. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $columns |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function createForeignIndexName(array $columns) |
|
131
|
|
|
{ |
|
132
|
|
|
// Initialize the List of Tables |
|
133
|
|
|
$tables = []; |
|
134
|
|
|
|
|
135
|
|
|
// Determine the Tables being References |
|
136
|
|
|
foreach($columns as $column) { |
|
137
|
|
|
|
|
138
|
|
|
// Derive the Table Name from the Column |
|
139
|
|
|
$table = $this->getTableNameFromColumn($column); |
|
140
|
|
|
|
|
141
|
|
|
// Use the Table Name, or the Column if one couldn't be found |
|
142
|
|
|
$tables[] = !is_null($table) ? $table : $column; |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// Flatten the Array |
|
147
|
|
|
return implode('_', $tables); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns the Derived Table Name from the Column. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $column |
|
154
|
|
|
* |
|
155
|
|
|
* @return string|null |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getTableNameFromColumn($column) |
|
158
|
|
|
{ |
|
159
|
|
|
// Make sure the Table has an ID Suffix |
|
160
|
|
|
if(!Str::endsWith($column, '_id')) { |
|
161
|
|
|
return null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Strip the '_id' Suffix |
|
165
|
|
|
$column = substr($column, 0, -3); |
|
166
|
|
|
|
|
167
|
|
|
// Pluralize the Column Name |
|
168
|
|
|
$column = Str::plural($column); |
|
169
|
|
|
|
|
170
|
|
|
// Use the Plural form of the ID-less Column Name as the Table Name |
|
171
|
|
|
return $column; |
|
172
|
|
|
} |
|
173
|
|
|
} |