1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Cycle\ORM; |
11
|
|
|
|
12
|
|
|
use Cycle\ORM\Config\RelationConfig; |
13
|
|
|
use Cycle\ORM\Mapper\Mapper; |
14
|
|
|
use Cycle\ORM\Relation\RelationInterface; |
15
|
|
|
use Cycle\ORM\Select\LoaderInterface; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
use Spiral\Core\Container; |
18
|
|
|
use Spiral\Core\FactoryInterface as CoreFactory; |
19
|
|
|
use Spiral\Database\DatabaseInterface; |
20
|
|
|
use Spiral\Database\DatabaseManager; |
21
|
|
|
|
22
|
|
|
class Factory implements FactoryInterface |
23
|
|
|
{ |
24
|
|
|
/** @var RelationConfig */ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** @var CoreFactory */ |
28
|
|
|
private $factory; |
29
|
|
|
|
30
|
|
|
/** @var ContainerInterface */ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
/** @var DatabaseManager */ |
34
|
|
|
private $dbal; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param DatabaseManager $dbal |
38
|
|
|
* @param RelationConfig $config |
39
|
|
|
* @param CoreFactory|null $factory |
40
|
|
|
* @param ContainerInterface|null $container |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
DatabaseManager $dbal, |
44
|
|
|
RelationConfig $config, |
45
|
|
|
CoreFactory $factory = null, |
46
|
|
|
ContainerInterface $container = null |
47
|
|
|
) { |
48
|
|
|
$this->dbal = $dbal; |
49
|
|
|
$this->config = $config; |
50
|
|
|
$this->factory = $factory ?? new Container(); |
51
|
|
|
$this->container = $container ?? new Container(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function has($id) |
58
|
|
|
{ |
59
|
|
|
return $this->container->has($id); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function get($id) |
66
|
|
|
{ |
67
|
|
|
return $this->container->get($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function mapper( |
74
|
|
|
ORMInterface $orm, |
75
|
|
|
SchemaInterface $schema, |
76
|
|
|
string $role |
77
|
|
|
): MapperInterface { |
78
|
|
|
$class = $schema->define($role, Schema::MAPPER) ?? Mapper::class; |
79
|
|
|
|
80
|
|
|
return $this->factory->make($class, [ |
|
|
|
|
81
|
|
|
'orm' => $orm, |
82
|
|
|
'role' => $role, |
83
|
|
|
'schema' => $schema->define($role, Schema::SCHEMA) |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function loader( |
91
|
|
|
ORMInterface $orm, |
92
|
|
|
SchemaInterface $schema, |
93
|
|
|
string $role, |
94
|
|
|
string $relation |
95
|
|
|
): LoaderInterface { |
96
|
|
|
$schema = $schema->defineRelation($role, $relation); |
97
|
|
|
|
98
|
|
|
return $this->config->getLoader($schema[Relation::TYPE])->resolve($this->factory, [ |
|
|
|
|
99
|
|
|
'orm' => $orm, |
100
|
|
|
'name' => $relation, |
101
|
|
|
'target' => $schema[Relation::TARGET], |
102
|
|
|
'schema' => $schema[Relation::SCHEMA] |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function relation( |
110
|
|
|
ORMInterface $orm, |
111
|
|
|
SchemaInterface $schema, |
112
|
|
|
string $role, |
113
|
|
|
string $relation |
114
|
|
|
): RelationInterface { |
115
|
|
|
$relSchema = $schema->defineRelation($role, $relation); |
116
|
|
|
$type = $relSchema[Relation::TYPE]; |
117
|
|
|
|
118
|
|
|
return $this->config->getRelation($type)->resolve($this->factory, [ |
|
|
|
|
119
|
|
|
'orm' => $orm, |
120
|
|
|
'name' => $relation, |
121
|
|
|
'target' => $relSchema[Relation::TARGET], |
122
|
|
|
'schema' => $relSchema[Relation::SCHEMA] |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
|
|
public function database(string $database): DatabaseInterface |
130
|
|
|
{ |
131
|
|
|
return $this->dbal->database($database); |
132
|
|
|
} |
133
|
|
|
} |