-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathFactory.php
More file actions
315 lines (273 loc) · 10.8 KB
/
Factory.php
File metadata and controls
315 lines (273 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
declare(strict_types=1);
namespace Cycle\ORM;
use Cycle\Database\DatabaseInterface;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\ORM\Collection\ArrayCollectionFactory;
use Cycle\ORM\Collection\CollectionFactoryInterface;
use Cycle\ORM\Config\RelationConfig;
use Cycle\ORM\Exception\FactoryTypecastException;
use Cycle\ORM\Exception\TypecastException;
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Parser\CompositeTypecast;
use Cycle\ORM\Parser\Typecast;
use Cycle\ORM\Parser\TypecastInterface;
use Cycle\ORM\Service\SourceProviderInterface;
use Cycle\ORM\Relation\RelationInterface;
use Cycle\ORM\Select\Loader\ParentLoader;
use Cycle\ORM\Select\Loader\SubclassLoader;
use Cycle\ORM\Select\LoaderInterface;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\ScopeInterface;
use Cycle\ORM\Select\Source;
use Cycle\ORM\Select\SourceInterface;
use Spiral\Core\Container;
use Spiral\Core\FactoryInterface as CoreFactory;
final class Factory implements FactoryInterface
{
private RelationConfig $config;
private CoreFactory $factory;
/** @var array<int, string> */
private array $defaults = [
SchemaInterface::REPOSITORY => Repository::class,
SchemaInterface::SOURCE => Source::class,
SchemaInterface::MAPPER => Mapper::class,
SchemaInterface::SCOPE => null,
SchemaInterface::TYPECAST_HANDLER => null,
];
/** @var array<string, CollectionFactoryInterface> */
private array $collectionFactoryAlias = [];
/**
* @var array<string, CollectionFactoryInterface>
*
* @psalm-var array<class-string, CollectionFactoryInterface>
*/
private array $collectionFactoryInterface = [];
private CollectionFactoryInterface $defaultCollectionFactory;
public function __construct(
private DatabaseProviderInterface $dbal,
?RelationConfig $config = null,
?CoreFactory $factory = null,
?CollectionFactoryInterface $defaultCollectionFactory = null,
) {
$this->config = $config ?? RelationConfig::getDefault();
$this->factory = $factory ?? new Container();
$this->defaultCollectionFactory = $defaultCollectionFactory ?? new ArrayCollectionFactory();
}
public function make(
string $alias,
array $parameters = [],
): mixed {
return $this->factory->make($alias, $parameters);
}
public function typecast(SchemaInterface $schema, DatabaseInterface $database, string $role): ?TypecastInterface
{
// Get parent's typecast
$parent = $schema->define($role, SchemaInterface::PARENT);
$parentHandler = $parent === null ? null : $this->typecast($schema, $database, $parent);
$handlers = [];
// Schema's `typecast` option
$rules = (array) $schema->define($role, SchemaInterface::TYPECAST);
$handler = $schema->define($role, SchemaInterface::TYPECAST_HANDLER)
?? $this->defaults[SchemaInterface::TYPECAST_HANDLER];
// Create basic typecast implementation
try {
if ($handler === null) {
if (!$rules) {
return $parentHandler;
}
$handlers[] = new Typecast($role, $database);
} elseif (\is_array($handler)) { // We need to use composite typecast for array
foreach ($handler as $type) {
$handlers[] = $this->makeTypecastHandler($type, $database, $schema, $role);
}
} else {
$handlers[] = $this->makeTypecastHandler($handler, $database, $schema, $role);
}
} catch (\Throwable $e) {
throw new FactoryTypecastException(
message: \sprintf(
'Bad typecast handler declaration for the `%s` role. %s',
$role,
$e->getMessage(),
),
code: $e->getCode(),
previous: $e,
);
}
$handler = \count($handlers) === 1 ? \reset($handlers) : new CompositeTypecast(...$handlers);
$handler->setRules($rules);
return $parentHandler === null
? $handler
: new CompositeTypecast($parentHandler, $handler);
}
public function mapper(ORMInterface $orm, string $role): MapperInterface
{
$schema = $orm->getSchema();
$class = $schema->define($role, SchemaInterface::MAPPER) ?? $this->defaults[SchemaInterface::MAPPER];
if (!\is_subclass_of($class, MapperInterface::class)) {
throw new TypecastException(\sprintf('%s does not implement %s.', $class, MapperInterface::class));
}
return $this->factory->make(
$class,
[
'orm' => $orm,
'role' => $role,
'schema' => $schema->define($role, SchemaInterface::SCHEMA),
],
);
}
public function loader(
SchemaInterface $schema,
SourceProviderInterface $sourceProvider,
string $role,
string $relation,
): LoaderInterface {
if ($relation === self::PARENT_LOADER) {
$parent = $schema->define($role, SchemaInterface::PARENT);
return new ParentLoader($schema, $sourceProvider, $this, $role, $parent);
}
if ($relation === self::CHILD_LOADER) {
$parent = $schema->define($role, SchemaInterface::PARENT);
return new SubclassLoader($schema, $sourceProvider, $this, $parent, $role);
}
$definition = $schema->defineRelation($role, $relation);
return $this->config->getLoader($definition[Relation::TYPE])->resolve(
$this->factory,
[
'ormSchema' => $schema,
'sourceProvider' => $sourceProvider,
'factory' => $this,
'role' => $role,
'name' => $relation,
'target' => $definition[Relation::TARGET],
'schema' => $definition[Relation::SCHEMA],
],
);
}
public function collection(
?string $name = null,
): CollectionFactoryInterface {
if ($name === null) {
return $this->defaultCollectionFactory;
}
if (\array_key_exists($name, $this->collectionFactoryAlias)) {
return $this->collectionFactoryAlias[$name];
}
// Find by interface
if (\class_exists($name)) {
foreach ($this->collectionFactoryInterface as $interface => $factory) {
if (\is_subclass_of($name, $interface, true)) {
return $this->collectionFactoryAlias[$name] = $factory->withCollectionClass($name);
}
}
}
return $this->collectionFactoryAlias[$name] = $this->factory->make($name);
}
public function relation(
ORMInterface $orm,
SchemaInterface $schema,
string $role,
string $relation,
): RelationInterface {
$relSchema = $schema->defineRelation($role, $relation);
$type = $relSchema[Relation::TYPE];
return $this->config->getRelation($type)->resolve(
$this->factory,
[
'orm' => $orm,
'role' => $role,
'name' => $relation,
'target' => $relSchema[Relation::TARGET],
'schema' => $relSchema[Relation::SCHEMA]
+ [Relation::LOAD => $relSchema[Relation::LOAD] ?? null]
+ [Relation::COLLECTION_TYPE => $relSchema[Relation::COLLECTION_TYPE] ?? null],
],
);
}
public function database(?string $database = null): DatabaseInterface
{
return $this->dbal->database($database);
}
public function repository(
ORMInterface $orm,
SchemaInterface $schema,
string $role,
?Select $select,
): RepositoryInterface {
$class = $schema->define($role, SchemaInterface::REPOSITORY) ?? $this->defaults[SchemaInterface::REPOSITORY];
if (!\is_subclass_of($class, RepositoryInterface::class)) {
throw new TypecastException($class . ' does not implement ' . RepositoryInterface::class);
}
return $this->factory->make(
$class,
[
'select' => $select,
'orm' => $orm,
'role' => $role,
],
);
}
public function source(
SchemaInterface $schema,
string $role,
): SourceInterface {
/** @var class-string<SourceInterface> $source */
$source = $schema->define($role, SchemaInterface::SOURCE) ?? $this->defaults[SchemaInterface::SOURCE];
if (!\is_subclass_of($source, SourceInterface::class)) {
throw new TypecastException($source . ' does not implement ' . SourceInterface::class);
}
$table = $schema->define($role, SchemaInterface::TABLE);
$database = $this->database($schema->define($role, SchemaInterface::DATABASE));
$source = $source !== Source::class
? $this->factory->make($source, ['role' => $role, 'table' => $table, 'database' => $database])
: new Source($database, $table);
/** @var class-string<ScopeInterface>|ScopeInterface|null $scope */
$scope = $schema->define($role, SchemaInterface::SCOPE) ?? $this->defaults[SchemaInterface::SCOPE];
if ($scope === null) {
return $source;
}
if (!\is_subclass_of($scope, ScopeInterface::class)) {
throw new TypecastException(\sprintf('%s does not implement %s.', $scope, ScopeInterface::class));
}
return $source->withScope(\is_object($scope) ? $scope : $this->factory->make($scope));
}
public function withDefaultSchemaClasses(array $defaults): self
{
$clone = clone $this;
$clone->defaults = $defaults + $this->defaults;
return $clone;
}
public function withCollectionFactory(
string $alias,
CollectionFactoryInterface $factory,
?string $interface = null,
): self {
$clone = clone $this;
$interface = $interface ?? $factory->getInterface();
$clone->collectionFactoryAlias[$alias] = $factory;
if ($interface !== null) {
$clone->collectionFactoryInterface[$interface] = $factory;
}
return $clone;
}
/**
* Make typecast handler from giver string or object
*/
private function makeTypecastHandler(
string|TypecastInterface $handler,
DatabaseInterface $database,
SchemaInterface $schema,
string $role,
): TypecastInterface {
// If handler is an object we don't need to use factory, we should return it as is
if (\is_object($handler)) {
return $handler;
}
return $this->factory->make($handler, [
'database' => $database,
'schema' => $schema,
'role' => $role,
]);
}
}