1 | <?php |
||
2 | /** |
||
3 | * Cycle DataMapper ORM |
||
4 | * |
||
5 | * @license MIT |
||
6 | * @author Anton Titov (Wolfy-J) |
||
7 | */ |
||
8 | declare(strict_types=1); |
||
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\DatabaseProviderInterface; |
||
21 | |||
22 | final 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 DatabaseProviderInterface */ |
||
34 | private $dbal; |
||
35 | |||
36 | /** |
||
37 | * @param DatabaseProviderInterface $dbal |
||
38 | * @param RelationConfig $config |
||
39 | * @param CoreFactory|null $factory |
||
40 | * @param ContainerInterface|null $container |
||
41 | */ |
||
42 | public function __construct( |
||
43 | DatabaseProviderInterface $dbal, |
||
44 | RelationConfig $config = null, |
||
45 | CoreFactory $factory = null, |
||
46 | ContainerInterface $container = null |
||
47 | ) { |
||
48 | $this->dbal = $dbal; |
||
49 | $this->config = $config ?? RelationConfig::getDefault(); |
||
50 | $this->factory = $factory ?? new Container(); |
||
51 | $this->container = $container ?? new Container(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | public function make(string $alias, array $parameters = []) |
||
58 | { |
||
59 | return $this->factory->make($alias, $parameters); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | public function mapper( |
||
66 | ORMInterface $orm, |
||
67 | SchemaInterface $schema, |
||
68 | string $role |
||
69 | ): MapperInterface { |
||
70 | $class = $schema->define($role, Schema::MAPPER) ?? Mapper::class; |
||
71 | |||
72 | return $this->factory->make($class, [ |
||
73 | 'orm' => $orm, |
||
74 | 'role' => $role, |
||
75 | 'schema' => $schema->define($role, Schema::SCHEMA) |
||
76 | ]); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function loader( |
||
83 | ORMInterface $orm, |
||
84 | SchemaInterface $schema, |
||
85 | string $role, |
||
86 | string $relation |
||
87 | ): LoaderInterface { |
||
88 | $schema = $schema->defineRelation($role, $relation); |
||
89 | |||
90 | return $this->config->getLoader($schema[Relation::TYPE])->resolve($this->factory, [ |
||
91 | 'orm' => $orm, |
||
92 | 'name' => $relation, |
||
93 | 'target' => $schema[Relation::TARGET], |
||
94 | 'schema' => $schema[Relation::SCHEMA] |
||
95 | ]); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | public function relation( |
||
102 | ORMInterface $orm, |
||
103 | SchemaInterface $schema, |
||
104 | string $role, |
||
105 | string $relation |
||
106 | ): RelationInterface { |
||
107 | $relSchema = $schema->defineRelation($role, $relation); |
||
108 | $type = $relSchema[Relation::TYPE]; |
||
109 | |||
110 | return $this->config->getRelation($type)->resolve($this->factory, [ |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
111 | 'orm' => $orm, |
||
112 | 'name' => $relation, |
||
113 | 'target' => $relSchema[Relation::TARGET], |
||
114 | 'schema' => $relSchema[Relation::SCHEMA] |
||
115 | ]); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | public function database(string $database = null): DatabaseInterface |
||
122 | { |
||
123 | return $this->dbal->database($database); |
||
124 | } |
||
125 | } |