1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/orm package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Orm; |
11
|
|
|
|
12
|
|
|
use Slick\Database\Adapter\AdapterInterface; |
13
|
|
|
use Slick\Orm\Descriptor\EntityDescriptor; |
14
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorRegistry; |
15
|
|
|
use Slick\Orm\Exception\InvalidArgumentException; |
16
|
|
|
use Slick\Orm\Mapper\EntityMapper; |
17
|
|
|
use Slick\Orm\Mapper\MappersMap; |
18
|
|
|
use Slick\Orm\Repository\EntityRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Orm registry |
22
|
|
|
* |
23
|
|
|
* @package Slick\Orm |
24
|
|
|
* @author Filipe Silva <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class Orm |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MappersMap|EntityMapperInterface[] |
31
|
|
|
*/ |
32
|
|
|
private $mappers; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Orm |
36
|
|
|
*/ |
37
|
|
|
private static $instance; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var AdaptersMap |
41
|
|
|
*/ |
42
|
|
|
private $adapters; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialize Orm registry with empty lists |
46
|
|
|
*/ |
47
|
2 |
|
private function __construct() |
48
|
|
|
{ |
49
|
2 |
|
$this->mappers = new MappersMap(); |
50
|
2 |
|
$this->adapters = new AdaptersMap(); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Avoid clone on a singleton |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
*/ |
57
|
|
|
private function __clone() |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets a ORM registry instance |
64
|
|
|
* |
65
|
|
|
* @return Orm |
66
|
|
|
*/ |
67
|
6 |
|
public static function getInstance() |
68
|
|
|
{ |
69
|
6 |
|
if (null === self::$instance) { |
70
|
2 |
|
self::$instance = new static; |
71
|
1 |
|
} |
72
|
6 |
|
return self::$instance; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieves the mapper for provided entity |
77
|
|
|
* |
78
|
|
|
* If mapper does not exists it will be created and stored in the |
79
|
|
|
* mapper map. |
80
|
|
|
* |
81
|
|
|
* @param EntityInterface $entity |
82
|
|
|
* @return EntityMapper |
83
|
|
|
*/ |
84
|
4 |
|
public static function getMapper(EntityInterface $entity) |
85
|
|
|
{ |
86
|
4 |
|
return self::getInstance()->getMapperFor($entity); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets repository for provided entity class name |
91
|
|
|
* |
92
|
|
|
* @param string $entityClass FQ entity class name |
93
|
|
|
* |
94
|
|
|
* @return EntityRepository |
95
|
|
|
* |
96
|
|
|
* @throws InvalidArgumentException If provide class name is not |
97
|
|
|
* from a class that implements the EntityInterface interface. |
98
|
|
|
*/ |
99
|
|
|
public static function getRepository($entityClass) |
100
|
|
|
{ |
101
|
|
|
return self::getInstance()->getRepositoryFor($entityClass); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Gets repository for provided entity class name |
106
|
|
|
* |
107
|
|
|
* @param string $entityClass FQ entity class name |
108
|
|
|
* |
109
|
|
|
* @return EntityRepository |
110
|
|
|
* |
111
|
|
|
* @throws InvalidArgumentException If provide class name is not |
112
|
|
|
* from a class that implements the EntityInterface interface. |
113
|
|
|
*/ |
114
|
|
|
public function getRepositoryFor($entityClass) |
115
|
|
|
{ |
116
|
|
|
if (!is_subclass_of($entityClass, EntityInterface::class)) { |
117
|
|
|
throw new InvalidArgumentException( |
118
|
|
|
'Cannot create ORM repository for a class that does not '. |
119
|
|
|
'implement EntityInterface.' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
$repository = new EntityRepository(); |
123
|
|
|
$repository->setAdapter( |
124
|
|
|
$this->adapters->get($this->getAdapterAlias($entityClass)) |
125
|
|
|
) |
126
|
|
|
->setEntityMapper($this->getMapperFor($entityClass)) |
127
|
|
|
->setEntityDescriptor(EntityDescriptorRegistry::getInstance() |
128
|
|
|
->getDescriptorFor($entityClass)); |
129
|
|
|
return $repository; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieves the mapper for provided entity |
134
|
|
|
* |
135
|
|
|
* If mapper does not exists it will be created and stored in the |
136
|
|
|
* mapper map. |
137
|
|
|
* |
138
|
|
|
* @param string $entity |
139
|
|
|
* @return EntityMapper |
140
|
|
|
*/ |
141
|
4 |
|
public function getMapperFor($entity) |
142
|
|
|
{ |
143
|
4 |
|
return $this->mappers->containsKey($entity) |
144
|
2 |
|
? $this->mappers->get($entity) |
145
|
4 |
|
: $this->createMapper($entity); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Sets default adapter |
150
|
|
|
* |
151
|
|
|
* @param AdapterInterface $adapter |
152
|
|
|
* @return $this|Orm|self |
153
|
|
|
*/ |
154
|
6 |
|
public function setDefaultAdapter(AdapterInterface $adapter) |
155
|
|
|
{ |
156
|
6 |
|
return $this->setAdapter('default', $adapter); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets an adapter mapped with alias name |
161
|
|
|
* |
162
|
|
|
* @param string $alias |
163
|
|
|
* @param AdapterInterface $adapter |
164
|
|
|
* |
165
|
|
|
* @return $this|Orm|self |
166
|
|
|
*/ |
167
|
6 |
|
public function setAdapter($alias, AdapterInterface $adapter) |
168
|
|
|
{ |
169
|
6 |
|
$this->adapters->set($alias, $adapter); |
170
|
6 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Creates entity map for provided entity |
175
|
|
|
* |
176
|
|
|
* @param string $entity |
177
|
|
|
* @return EntityMapper |
178
|
|
|
*/ |
179
|
4 |
|
private function createMapper($entity) |
180
|
|
|
{ |
181
|
4 |
|
$mapper = new EntityMapper(); |
182
|
4 |
|
$mapper->setAdapter( |
183
|
4 |
|
$this->adapters->get( |
184
|
4 |
|
$this->getAdapterAlias($entity) |
185
|
2 |
|
) |
186
|
2 |
|
); |
187
|
4 |
|
$this->mappers->set($entity, $mapper); |
188
|
4 |
|
return $mapper; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gets the adapter alias for current working entity |
193
|
|
|
* |
194
|
|
|
* @param string $entity |
195
|
|
|
* |
196
|
|
|
* @return EntityDescriptor|string |
197
|
|
|
*/ |
198
|
4 |
|
private function getAdapterAlias($entity) |
199
|
|
|
{ |
200
|
4 |
|
$descriptor = EntityDescriptorRegistry::getInstance() |
201
|
4 |
|
->getDescriptorFor($entity); |
202
|
4 |
|
return $descriptor->getAdapterAlias() |
203
|
4 |
|
? $descriptor->getAdapterAlias() |
204
|
4 |
|
: 'default'; |
205
|
|
|
} |
206
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: