1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Drivers\MongoDB; |
4
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Database\Drivers\MongoDB\Traits\IndexTrait; |
6
|
|
|
use MongoDB\Collection; |
|
|
|
|
7
|
|
|
use MongoDB\Model\IndexInfo; |
|
|
|
|
8
|
|
|
|
9
|
|
|
class Index |
10
|
|
|
{ |
11
|
|
|
use IndexTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get Indexes. |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
public static function getIndexes(Collection $collection) |
19
|
|
|
{ |
20
|
|
|
$listIndexes = $collection->listIndexes(); |
21
|
|
|
$indexes = []; |
22
|
|
|
foreach ($listIndexes as $index) { |
23
|
|
|
$indexes[] = [ |
24
|
|
|
'name' => $index->getName(), |
25
|
|
|
'oldName' => $index->getName(), |
26
|
|
|
'columns' => array_keys($index->getKey()), |
27
|
|
|
'type' => static::getType($index), |
28
|
|
|
'isPrimary' => false, |
29
|
|
|
'isUnique' => $index->isUnique(), |
30
|
|
|
'isComposite' => (count($index->getKey()) > 1) ? true : false, |
31
|
|
|
'flags' => [], |
32
|
|
|
'options' => [], |
33
|
|
|
'namespace' => $index->getNamespace(), |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $indexes; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get index type. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public static function getType(IndexInfo $index) |
46
|
|
|
{ |
47
|
|
|
$type = static::getCommonType($index); |
48
|
|
|
|
49
|
|
|
if (empty($type)) { |
50
|
|
|
$type = static::getSpecialType($index); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (empty($type)) { |
54
|
|
|
$type = static::getDefaultType($index); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $type; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set Indexes. |
62
|
|
|
* |
63
|
|
|
* @param \MongoDB\Collection $collection |
64
|
|
|
* @param array $indexes |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public static function setIndexes(Collection $collection, $indexes) |
69
|
|
|
{ |
70
|
|
|
$collection->dropIndexes(); |
71
|
|
|
|
72
|
|
|
foreach ($indexes as $index) { |
73
|
|
|
$columns = $index['columns']; |
74
|
|
|
$name = $index['name']; |
|
|
|
|
75
|
|
|
$type = $index['type']; |
76
|
|
|
|
77
|
|
|
foreach ($columns as $column) { |
78
|
|
|
if ($column == '_id') { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$indexDetails = static::getIndexDetails($type); |
83
|
|
|
$indexType = $indexDetails['type']; |
84
|
|
|
$options = $indexDetails['options']; |
85
|
|
|
|
86
|
|
|
$options['name'] = strtolower($collection->getCollectionName().'_'.$column.'_'.$type); |
87
|
|
|
|
88
|
|
|
$options['ns'] = $collection->getNamespace(); |
89
|
|
|
|
90
|
|
|
$collection->createIndex([$column => $indexType], $options); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get Index Details. |
99
|
|
|
* |
100
|
|
|
* @param string $type |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public static function getIndexDetails($type) |
105
|
|
|
{ |
106
|
|
|
$indexType = 1; |
107
|
|
|
$options = []; |
108
|
|
|
|
109
|
|
|
switch ($type) { |
110
|
|
|
|
111
|
|
|
case 'TEXT': |
112
|
|
|
$indexType = 'text'; |
113
|
|
|
break; |
114
|
|
|
case 'INDEX': |
115
|
|
|
$indexType = 1; |
116
|
|
|
break; |
117
|
|
|
case 'UNIQUE': |
118
|
|
|
$indexType = 1; |
119
|
|
|
$options['unique'] = true; |
120
|
|
|
break; |
121
|
|
|
case 'UNIQUE_DESC': |
122
|
|
|
$indexType = -1; |
123
|
|
|
$options['unique'] = true; |
124
|
|
|
break; |
125
|
|
|
case 'TTL': |
126
|
|
|
$indexType = 1; |
127
|
|
|
$options['expireAfterSeconds'] = 3600; |
128
|
|
|
break; |
129
|
|
|
case 'SPARSE': |
130
|
|
|
$indexType = 1; |
131
|
|
|
$options['sparse'] = true; |
132
|
|
|
break; |
133
|
|
|
case 'SPARSE_DESC': |
134
|
|
|
$indexType = -1; |
135
|
|
|
$options['sparse'] = true; |
136
|
|
|
break; |
137
|
|
|
case 'SPARSE_UNIQUE': |
138
|
|
|
$indexType = 1; |
139
|
|
|
$options['sparse'] = true; |
140
|
|
|
$options['unique'] = true; |
|
|
|
|
141
|
|
|
case 'SPARSE_UNIQUE_DESC': |
142
|
|
|
$indexType = -1; |
143
|
|
|
$options['sparse'] = true; |
144
|
|
|
$options['unique'] = true; |
145
|
|
|
break; |
146
|
|
|
case 'ASC': |
147
|
|
|
$indexType = 1; |
148
|
|
|
break; |
149
|
|
|
case 'DESC': |
150
|
|
|
$indexType = -1; |
151
|
|
|
break; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return ['type' => $indexType, 'options' => $options]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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