1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Drivers\MongoDB\Schema; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
6
|
|
|
|
7
|
|
|
class Blueprint extends \Illuminate\Database\Schema\Blueprint |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The MongoConnection object for this blueprint. |
11
|
|
|
* @var \Illuminate\Database\Connection |
12
|
|
|
*/ |
13
|
|
|
protected $connection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The MongoCollection object for this blueprint. |
17
|
|
|
* @var \MongoDB\Collection |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
protected $collection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Fluent columns. |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $columns = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Connection $connection, $collection) |
31
|
|
|
{ |
32
|
|
|
$this->connection = $connection; |
33
|
|
|
|
34
|
|
|
$this->collection = $this->connection->getCollection($collection); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function index($columns = null, $name = null, $algorithm = null, $options = []) |
41
|
|
|
{ |
42
|
|
|
$columns = $this->fluent($columns); |
43
|
|
|
|
44
|
|
|
// Columns are passed as a default array. |
45
|
|
|
if (is_array($columns) && is_int(key($columns))) { |
46
|
|
|
// Transform the columns to the required array format. |
47
|
|
|
$transform = []; |
48
|
|
|
|
49
|
|
|
foreach ($columns as $column) { |
50
|
|
|
$transform[$column] = 1; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$columns = $transform; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($name !== null) { |
57
|
|
|
$options['name'] = $name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->collection->createIndex($columns, $options); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function primary($columns = null, $name = null, $algorithm = null, $options = []) |
69
|
|
|
{ |
70
|
|
|
return $this->unique($columns, $name, $algorithm, $options); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function dropIndex($indexOrColumns = null) |
77
|
|
|
{ |
78
|
|
|
$indexOrColumns = $this->transformColumns($indexOrColumns); |
79
|
|
|
|
80
|
|
|
$this->collection->dropIndex($indexOrColumns); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Indicate that the given index should be dropped, but do not fail if it didn't exist. |
87
|
|
|
* |
88
|
|
|
* @param string|array $indexOrColumns |
89
|
|
|
* @return Blueprint |
90
|
|
|
*/ |
91
|
|
|
public function dropIndexIfExists($indexOrColumns = null) |
92
|
|
|
{ |
93
|
|
|
if ($this->hasIndex($indexOrColumns)) { |
94
|
|
|
$this->dropIndex($indexOrColumns); |
95
|
|
|
} |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check whether the given index exists. |
101
|
|
|
* |
102
|
|
|
* @param string|array $indexOrColumns |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function hasIndex($indexOrColumns = null) |
106
|
|
|
{ |
107
|
|
|
$indexOrColumns = $this->transformColumns($indexOrColumns); |
108
|
|
|
foreach ($this->collection->listIndexes() as $index) { |
109
|
|
|
if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string|array $indexOrColumns |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
protected function transformColumns($indexOrColumns) |
125
|
|
|
{ |
126
|
|
|
if (is_array($indexOrColumns)) { |
127
|
|
|
$indexOrColumns = $this->fluent($indexOrColumns); |
128
|
|
|
|
129
|
|
|
// Transform the columns to the index name. |
130
|
|
|
$transform = []; |
131
|
|
|
|
132
|
|
|
foreach ($indexOrColumns as $column) { |
133
|
|
|
$transform[$column] = $column . '_1'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$indexOrColumns = implode('_', $transform); |
137
|
|
|
} |
138
|
|
|
return $indexOrColumns; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritdoc |
143
|
|
|
*/ |
144
|
|
|
public function unique($columns = null, $name = null, $algorithm = null, $options = []) |
145
|
|
|
{ |
146
|
|
|
$columns = $this->fluent($columns); |
147
|
|
|
|
148
|
|
|
$options['unique'] = true; |
149
|
|
|
|
150
|
|
|
$this->index($columns, $name, $algorithm, $options); |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Specify a non blocking index for the collection. |
157
|
|
|
* @param string|array $columns |
158
|
|
|
* @return Blueprint |
159
|
|
|
*/ |
160
|
|
|
public function background($columns = null) |
161
|
|
|
{ |
162
|
|
|
$columns = $this->fluent($columns); |
163
|
|
|
|
164
|
|
|
$this->index($columns, null, null, ['background' => true]); |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Specify a sparse index for the collection. |
171
|
|
|
* @param string|array $columns |
172
|
|
|
* @param array $options |
173
|
|
|
* @return Blueprint |
174
|
|
|
*/ |
175
|
|
|
public function sparse($columns = null, $options = []) |
176
|
|
|
{ |
177
|
|
|
$columns = $this->fluent($columns); |
178
|
|
|
|
179
|
|
|
$options['sparse'] = true; |
180
|
|
|
|
181
|
|
|
$this->index($columns, null, null, $options); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Specify a geospatial index for the collection. |
188
|
|
|
* @param string|array $columns |
189
|
|
|
* @param string $index |
190
|
|
|
* @param array $options |
191
|
|
|
* @return Blueprint |
192
|
|
|
*/ |
193
|
|
|
public function geospatial($columns = null, $index = '2d', $options = []) |
194
|
|
|
{ |
195
|
|
|
if ($index == '2d' || $index == '2dsphere') { |
196
|
|
|
$columns = $this->fluent($columns); |
197
|
|
|
|
198
|
|
|
$columns = array_flip($columns); |
199
|
|
|
|
200
|
|
|
foreach ($columns as $column => $value) { |
201
|
|
|
$columns[$column] = $index; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->index($columns, null, null, $options); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Specify the number of seconds after wich a document should be considered expired based, |
212
|
|
|
* on the given single-field index containing a date. |
213
|
|
|
* @param string|array $columns |
214
|
|
|
* @param int $seconds |
215
|
|
|
* @return Blueprint |
216
|
|
|
*/ |
217
|
|
|
public function expire($columns, $seconds) |
218
|
|
|
{ |
219
|
|
|
$columns = $this->fluent($columns); |
220
|
|
|
|
221
|
|
|
$this->index($columns, null, null, ['expireAfterSeconds' => $seconds]); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritdoc |
228
|
|
|
*/ |
229
|
|
|
public function create() |
230
|
|
|
{ |
231
|
|
|
$collection = $this->collection->getCollectionName(); |
232
|
|
|
|
233
|
|
|
$db = $this->connection->getMongoDB(); |
|
|
|
|
234
|
|
|
|
235
|
|
|
// Ensure the collection is created. |
236
|
|
|
$db->createCollection($collection); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritdoc |
241
|
|
|
*/ |
242
|
|
|
public function drop() |
243
|
|
|
{ |
244
|
|
|
$this->collection->drop(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @inheritdoc |
249
|
|
|
*/ |
250
|
|
|
public function addColumn($type, $name, array $parameters = []) |
251
|
|
|
{ |
252
|
|
|
$this->fluent($name); |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Specify a sparse and unique index for the collection. |
259
|
|
|
* @param string|array $columns |
260
|
|
|
* @param array $options |
261
|
|
|
* @return Blueprint |
262
|
|
|
*/ |
263
|
|
|
public function sparse_and_unique($columns = null, $options = []) |
264
|
|
|
{ |
265
|
|
|
$columns = $this->fluent($columns); |
266
|
|
|
|
267
|
|
|
$options['sparse'] = true; |
268
|
|
|
$options['unique'] = true; |
269
|
|
|
|
270
|
|
|
$this->index($columns, null, null, $options); |
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Allow fluent columns. |
277
|
|
|
* @param string|array $columns |
278
|
|
|
* @return string|array |
279
|
|
|
*/ |
280
|
|
|
protected function fluent($columns = null) |
281
|
|
|
{ |
282
|
|
|
if ($columns === null) { |
283
|
|
|
return $this->columns; |
284
|
|
|
} elseif (is_string($columns)) { |
285
|
|
|
return $this->columns = [$columns]; |
286
|
|
|
} else { |
287
|
|
|
return $this->columns = $columns; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Allows the use of unsupported schema methods. |
293
|
|
|
* @param $method |
294
|
|
|
* @param $args |
295
|
|
|
* @return Blueprint |
296
|
|
|
*/ |
297
|
|
|
public function __call($method, $args) |
298
|
|
|
{ |
299
|
|
|
// Dummy. |
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths