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
|
|
|
ODMInterface::D_PRIMARY_CLASS => $schema->resolvePrimary($this), |
99
|
|
|
|
100
|
|
|
//Instantiator and entity specific schema |
101
|
|
|
ODMInterface::D_SCHEMA => $schema->packSchema($this) |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
if (!$schema->isEmbedded()) { |
105
|
|
|
$item[ODMInterface::D_DATABASE] = $schema->getDatabase(); |
106
|
|
|
$item[ODMInterface::D_COLLECTION] = $schema->getCollection(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$result[$class] = $item; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create all declared indexes. |
117
|
|
|
* |
118
|
|
|
* @throws UnsupportedException |
119
|
|
|
* @throws DriverException |
120
|
|
|
*/ |
121
|
|
|
public function createIndexes() |
122
|
|
|
{ |
123
|
|
|
foreach ($this->schemas as $class => $schema) { |
124
|
|
|
if ($schema->isEmbedded()) { |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$collection = $this->manager->database( |
129
|
|
|
$schema->getDatabase() |
130
|
|
|
)->selectCollection( |
131
|
|
|
$schema->getCollection() |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
//Declaring needed indexes |
135
|
|
|
foreach ($schema->getIndexes() as $index) { |
136
|
|
|
$collection->createIndex($index->getIndex(), $index->getOptions()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |