|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Helpers; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Addendum; |
|
17
|
|
|
use Maslosoft\Addendum\Helpers\SoftIncluder; |
|
18
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
19
|
|
|
use Maslosoft\Cli\Shared\Helpers\PhpExporter; |
|
20
|
|
|
use Maslosoft\Mangan\Helpers\Index\IndexModel; |
|
21
|
|
|
use Maslosoft\Mangan\Mangan; |
|
22
|
|
|
use Maslosoft\Mangan\Meta\DocumentPropertyMeta; |
|
23
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
24
|
|
|
use Maslosoft\ManganTest\Extensions\IndexMetaCleaner; |
|
25
|
|
|
|
|
26
|
|
|
class IndexManager |
|
27
|
|
|
{ |
|
28
|
|
|
const IndexTypeHashed = 'hashed'; |
|
29
|
|
|
const IndexType2dSphere = '2dsphere'; |
|
30
|
|
|
|
|
31
|
|
|
const DefaultInstanceId = 'indexManager'; |
|
32
|
|
|
|
|
33
|
|
|
private static $instances = []; |
|
34
|
|
|
|
|
35
|
|
|
private static $paths = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* NOTE: This is public because of IndexMetaCleaner testing extension |
|
39
|
|
|
* |
|
40
|
|
|
* DO NOT TOUCH! |
|
41
|
|
|
* |
|
42
|
|
|
* @see IndexMetaCleaner |
|
43
|
|
|
* @internal |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
public static $haveIndex = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* NOTE: This is public because of IndexMetaCleaner testing extension |
|
50
|
|
|
* |
|
51
|
|
|
* DO NOT TOUCH! |
|
52
|
|
|
* |
|
53
|
|
|
* @see IndexMetaCleaner |
|
54
|
|
|
* @internal |
|
55
|
|
|
* @var bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public static $haveDir = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create flyweight instance of index manager |
|
61
|
|
|
* @param string $instanceId |
|
62
|
|
|
* @return static |
|
63
|
|
|
*/ |
|
64
|
114 |
|
public static function fly($instanceId = self::DefaultInstanceId) |
|
65
|
|
|
{ |
|
66
|
114 |
|
if (empty(self::$instances[$instanceId])) |
|
67
|
|
|
{ |
|
68
|
|
|
self::$instances[$instanceId] = new static(); |
|
69
|
|
|
} |
|
70
|
114 |
|
return self::$instances[$instanceId]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
113 |
|
public function create(AnnotatedInterface $model) |
|
74
|
|
|
{ |
|
75
|
113 |
|
$className = get_class($model); |
|
76
|
|
|
|
|
77
|
|
|
// If have or don't have indexes skip further checks |
|
78
|
113 |
|
if(array_key_exists($className, self::$haveIndex)) |
|
79
|
|
|
{ |
|
80
|
90 |
|
return self::$haveIndex[$className]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
113 |
|
$fieldMetas = ManganMeta::create($model)->fields(); |
|
84
|
|
|
|
|
85
|
|
|
// Filter out fields without index |
|
86
|
113 |
|
foreach($fieldMetas as $key => $metaProperty) |
|
87
|
|
|
{ |
|
88
|
113 |
|
if(empty($metaProperty->index)) |
|
89
|
|
|
{ |
|
90
|
113 |
|
unset($fieldMetas[$key]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Does not have indexes, mark as index-less |
|
95
|
113 |
|
if(empty($fieldMetas)) |
|
96
|
|
|
{ |
|
97
|
107 |
|
self::$haveIndex[$className] = false; |
|
98
|
107 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
$path = $this->getStoragePath($model, $className); |
|
102
|
|
|
|
|
103
|
6 |
|
$data = SoftIncluder::includeFile($path); |
|
104
|
|
|
|
|
105
|
6 |
|
if(!empty($data)) |
|
106
|
|
|
{ |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
6 |
|
$results = []; |
|
110
|
6 |
|
$indexes = []; |
|
111
|
6 |
|
foreach($fieldMetas as $fieldMeta) |
|
112
|
|
|
{ |
|
113
|
6 |
|
if(empty($fieldMeta->index)) |
|
114
|
|
|
{ |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
/* @var $fieldMeta DocumentPropertyMeta */ |
|
118
|
|
|
|
|
119
|
6 |
|
foreach($fieldMeta->index as $indexMeta) |
|
120
|
|
|
{ |
|
121
|
6 |
|
$index = new IndexModel($model, $indexMeta); |
|
122
|
6 |
|
$results[] = (int)$index->apply(); |
|
123
|
6 |
|
$indexes[] = $index->getIndexes(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
6 |
|
self::$haveIndex[$className] = true; |
|
127
|
|
|
|
|
128
|
6 |
|
$dir = dirname($path); |
|
129
|
6 |
|
if(!self::$haveDir && !file_exists($dir)) |
|
130
|
|
|
{ |
|
131
|
6 |
|
self::$haveDir = mkdir($dir); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
6 |
|
file_put_contents($path, PhpExporter::export($indexes, 'Auto generated, do not modify')); |
|
135
|
6 |
|
@chmod($path, 0666); |
|
136
|
|
|
|
|
137
|
6 |
|
return array_sum($results) === count($results); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
7 |
|
public function getStoragePath(AnnotatedInterface $model = null, $className = null) |
|
141
|
|
|
{ |
|
142
|
7 |
|
if(empty($className)) |
|
143
|
|
|
{ |
|
144
|
4 |
|
$className = __CLASS__; |
|
145
|
|
|
} |
|
146
|
|
|
if(empty(self::$paths[$className])) |
|
147
|
4 |
|
{ |
|
148
|
4 |
|
if(empty($model)) |
|
149
|
4 |
|
{ |
|
150
|
4 |
|
$mn = Mangan::fly(); |
|
151
|
4 |
|
} |
|
152
|
|
|
else |
|
153
|
4 |
|
{ |
|
154
|
|
|
$mn = Mangan::fromModel($model); |
|
155
|
7 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
$params = [ |
|
158
|
|
|
Addendum::fly()->runtimePath, |
|
159
|
|
|
str_replace('\\', '.', static::class), |
|
160
|
|
|
$mn->connectionId, |
|
161
|
|
|
$mn->dbName, |
|
162
|
|
|
str_replace('\\', '.', $className), |
|
163
|
|
|
]; |
|
164
|
|
|
self::$paths[$className] = vsprintf('%s/%s/%s.%s@%s.php', $params); |
|
165
|
|
|
} |
|
166
|
|
|
return self::$paths[$className]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
} |