|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\ODM\Schemas; |
|
8
|
|
|
|
|
9
|
|
|
use MongoDB\Driver\Exception\RuntimeException as DriverException; |
|
10
|
|
|
use MongoDB\Exception\UnsupportedException; |
|
11
|
|
|
use Spiral\ODM\Exceptions\SchemaException; |
|
12
|
|
|
use Spiral\ODM\MongoManager; |
|
13
|
|
|
use Spiral\ODM\ODMInterface; |
|
14
|
|
|
|
|
15
|
|
|
class SchemaBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var MongoManager |
|
19
|
|
|
*/ |
|
20
|
|
|
private $manager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var SchemaInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $schemas = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param MongoManager $manager |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(MongoManager $manager) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->manager = $manager; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Add new model schema into pool. |
|
37
|
|
|
* |
|
38
|
|
|
* @param SchemaInterface $schema |
|
39
|
|
|
* |
|
40
|
|
|
* @return self|$this |
|
41
|
|
|
*/ |
|
42
|
|
|
public function addSchema(SchemaInterface $schema): SchemaBuilder |
|
43
|
|
|
{ |
|
44
|
|
|
$this->schemas[$schema->getClass()] = $schema; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $class |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function hasSchema(string $class): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return isset($this->schemas[$class]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $class |
|
61
|
|
|
* |
|
62
|
|
|
* @return SchemaInterface |
|
63
|
|
|
* |
|
64
|
|
|
* @throws SchemaException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getSchema(string $class): SchemaInterface |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$this->hasSchema($class)) { |
|
69
|
|
|
throw new SchemaException("Unable to find schema for class '{$class}'"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->schemas[$class]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* All available document schemas. |
|
77
|
|
|
* |
|
78
|
|
|
* @return SchemaInterface[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getSchemas(): array |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->schemas; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Pack declared schemas in a normalized form. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function packSchema(): array |
|
91
|
|
|
{ |
|
92
|
|
|
$result = []; |
|
93
|
|
|
foreach ($this->schemas as $class => $schema) { |
|
94
|
|
|
$item = [ |
|
95
|
|
|
//Instantiator class |
|
96
|
|
|
ODMInterface::D_INSTANTIATOR => $schema->getInstantiator(), |
|
97
|
|
|
|
|
98
|
|
|
//Instantiator and entity specific schema |
|
99
|
|
|
ODMInterface::D_SCHEMA => $schema->packSchema($this) |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
|
|
if (!$schema->isEmbedded()) { |
|
103
|
|
|
$item[ODMInterface::D_DATABASE] = $schema->getDatabase(); |
|
104
|
|
|
$item[ODMInterface::D_COLLECTION] = $schema->getCollection(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$result[$class] = $item; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $result; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Create all declared indexes. |
|
115
|
|
|
* |
|
116
|
|
|
* @throws UnsupportedException |
|
117
|
|
|
* @throws DriverException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function createIndexes() |
|
120
|
|
|
{ |
|
121
|
|
|
foreach ($this->schemas as $class => $schema) { |
|
122
|
|
|
if ($schema->isEmbedded()) { |
|
123
|
|
|
continue; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$collection = $this->manager->database( |
|
127
|
|
|
$schema->getDatabase() |
|
128
|
|
|
)->selectCollection( |
|
129
|
|
|
$schema->getCollection() |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
//Declaring needed indexes |
|
133
|
|
|
foreach ($schema->getIndexes() as $index) { |
|
134
|
|
|
$collection->createIndex($index->getIndex(), $index->getOptions()); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |