|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cycle DataMapper ORM |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Cycle\ORM; |
|
13
|
|
|
|
|
14
|
|
|
use Cycle\ORM\Config\RelationConfig; |
|
15
|
|
|
use Cycle\ORM\Exception\TypecastException; |
|
16
|
|
|
use Cycle\ORM\Mapper\Mapper; |
|
17
|
|
|
use Cycle\ORM\Relation\RelationInterface; |
|
18
|
|
|
use Cycle\ORM\Select\ConstrainInterface; |
|
19
|
|
|
use Cycle\ORM\Select\LoaderInterface; |
|
20
|
|
|
use Cycle\ORM\Select\Repository; |
|
21
|
|
|
use Cycle\ORM\Select\Source; |
|
22
|
|
|
use Cycle\ORM\Select\SourceInterface; |
|
23
|
|
|
use Psr\Container\ContainerInterface; |
|
24
|
|
|
use Spiral\Core\Container; |
|
25
|
|
|
use Spiral\Core\FactoryInterface as CoreFactory; |
|
26
|
|
|
use Spiral\Database\DatabaseInterface; |
|
27
|
|
|
use Spiral\Database\DatabaseProviderInterface; |
|
28
|
|
|
|
|
29
|
|
|
final class Factory implements FactoryInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var RelationConfig */ |
|
32
|
|
|
private $config; |
|
33
|
|
|
|
|
34
|
|
|
/** @var CoreFactory */ |
|
35
|
|
|
private $factory; |
|
36
|
|
|
|
|
37
|
|
|
/** @var ContainerInterface */ |
|
38
|
|
|
private $container; |
|
39
|
|
|
|
|
40
|
|
|
/** @var DatabaseProviderInterface */ |
|
41
|
|
|
private $dbal; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array<string, string> */ |
|
44
|
|
|
private $defaults = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param DatabaseProviderInterface $dbal |
|
48
|
|
|
* @param RelationConfig $config |
|
49
|
|
|
* @param CoreFactory|null $factory |
|
50
|
|
|
* @param ContainerInterface|null $container |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
DatabaseProviderInterface $dbal, |
|
54
|
|
|
RelationConfig $config = null, |
|
55
|
|
|
CoreFactory $factory = null, |
|
56
|
|
|
ContainerInterface $container = null |
|
57
|
|
|
) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->dbal = $dbal; |
|
60
|
|
|
$this->config = $config ?? RelationConfig::getDefault(); |
|
61
|
|
|
$this->factory = $factory ?? new Container(); |
|
62
|
|
|
$this->container = $container ?? new Container(); |
|
63
|
|
|
$this->defaults = [ |
|
64
|
|
|
Schema::REPOSITORY => Repository::class, |
|
65
|
|
|
Schema::SOURCE => Source::class, |
|
66
|
|
|
Schema::MAPPER => Mapper::class, |
|
67
|
|
|
Schema::CONSTRAIN => null, |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function make(string $alias, array $parameters = []) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->factory->make($alias, $parameters); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function mapper( |
|
83
|
|
|
ORMInterface $orm, |
|
84
|
|
|
SchemaInterface $schema, |
|
85
|
|
|
string $role |
|
86
|
|
|
): MapperInterface |
|
87
|
|
|
{ |
|
88
|
|
|
$class = $schema->define($role, Schema::MAPPER) ?? $this->defaults[Schema::MAPPER]; |
|
89
|
|
|
|
|
90
|
|
|
if (!is_subclass_of($class, MapperInterface::class)) { |
|
91
|
|
|
throw new TypecastException($class . ' not implement MapperInterface'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->factory->make($class, [ |
|
|
|
|
|
|
95
|
|
|
'orm' => $orm, |
|
96
|
|
|
'role' => $role, |
|
97
|
|
|
'schema' => $schema->define($role, Schema::SCHEMA) |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
|
|
public function loader( |
|
105
|
|
|
ORMInterface $orm, |
|
106
|
|
|
SchemaInterface $schema, |
|
107
|
|
|
string $role, |
|
108
|
|
|
string $relation |
|
109
|
|
|
): LoaderInterface |
|
110
|
|
|
{ |
|
111
|
|
|
$schema = $schema->defineRelation($role, $relation); |
|
112
|
|
|
|
|
113
|
|
|
return $this->config->getLoader($schema[Relation::TYPE])->resolve($this->factory, [ |
|
|
|
|
|
|
114
|
|
|
'orm' => $orm, |
|
115
|
|
|
'name' => $relation, |
|
116
|
|
|
'target' => $schema[Relation::TARGET], |
|
117
|
|
|
'schema' => $schema[Relation::SCHEMA] |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @inheritdoc |
|
123
|
|
|
*/ |
|
124
|
|
|
public function relation( |
|
125
|
|
|
ORMInterface $orm, |
|
126
|
|
|
SchemaInterface $schema, |
|
127
|
|
|
string $role, |
|
128
|
|
|
string $relation |
|
129
|
|
|
): RelationInterface |
|
130
|
|
|
{ |
|
131
|
|
|
$relSchema = $schema->defineRelation($role, $relation); |
|
132
|
|
|
$type = $relSchema[Relation::TYPE]; |
|
133
|
|
|
|
|
134
|
|
|
return $this->config->getRelation($type)->resolve($this->factory, [ |
|
|
|
|
|
|
135
|
|
|
'orm' => $orm, |
|
136
|
|
|
'name' => $relation, |
|
137
|
|
|
'target' => $relSchema[Relation::TARGET], |
|
138
|
|
|
'schema' => $relSchema[Relation::SCHEMA] + [Relation::LOAD => $relSchema[Relation::LOAD] ?? null], |
|
139
|
|
|
]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @inheritdoc |
|
144
|
|
|
*/ |
|
145
|
|
|
public function database(string $database = null): DatabaseInterface |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->dbal->database($database); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @inheritDoc |
|
152
|
|
|
*/ |
|
153
|
|
|
public function repository( |
|
154
|
|
|
SchemaInterface $schema, string $role, ?Select $select |
|
155
|
|
|
): RepositoryInterface |
|
156
|
|
|
{ |
|
157
|
|
|
$class = $schema->define($role, Schema::REPOSITORY) ?? $this->defaults[Schema::REPOSITORY]; |
|
158
|
|
|
|
|
159
|
|
|
if (!is_subclass_of($class, RepositoryInterface::class)) { |
|
160
|
|
|
throw new TypecastException($class . ' not implement RepositoryInterface'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->factory->make($class, ['select' => $select]); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @inheritDoc |
|
168
|
|
|
*/ |
|
169
|
|
|
public function source( |
|
170
|
|
|
ORMInterface $orm, |
|
171
|
|
|
SchemaInterface $schema, |
|
172
|
|
|
string $role |
|
173
|
|
|
): SourceInterface |
|
174
|
|
|
{ |
|
175
|
|
|
$source = $schema->define($role, Schema::SOURCE) ?? $this->defaults[Schema::SOURCE]; |
|
176
|
|
|
|
|
177
|
|
|
if (!is_subclass_of($source, SourceInterface::class)) { |
|
178
|
|
|
throw new TypecastException($source . ' not implement SourceInterface'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if ($source !== Source::class) { |
|
182
|
|
|
return $this->factory->make($source, ['orm' => $orm, 'role' => $role]); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$source = new Source( |
|
186
|
|
|
$this->database($schema->define($role, Schema::DATABASE)), |
|
187
|
|
|
$schema->define($role, Schema::TABLE) |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
$constrain = $schema->define($role, Schema::CONSTRAIN) ?? $this->defaults[Schema::CONSTRAIN]; |
|
191
|
|
|
if ($constrain !== null) { |
|
192
|
|
|
|
|
193
|
|
|
if (!is_subclass_of($constrain, ConstrainInterface::class)) { |
|
194
|
|
|
throw new TypecastException($constrain . ' not implement ConstrainInterface'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$source = $source->withConstrain( |
|
198
|
|
|
is_object($constrain) ? $constrain : $this->factory->make($constrain) |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $source; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Add default classes for resolve |
|
207
|
|
|
* @param array $defaults |
|
208
|
|
|
* @return FactoryInterface |
|
209
|
|
|
*/ |
|
210
|
|
|
public function withDefaultSchemaClasses(array $defaults): FactoryInterface |
|
211
|
|
|
{ |
|
212
|
|
|
$clone = clone $this; |
|
213
|
|
|
|
|
214
|
|
|
$clone->defaults = $defaults + $this->defaults; |
|
215
|
|
|
|
|
216
|
|
|
return $clone; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|