|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Cache\CacheInterface; |
|
6
|
|
|
use Bdf\Prime\Entity\Hydrator\MapperHydrator; |
|
7
|
|
|
use Bdf\Prime\Entity\Hydrator\MapperHydratorInterface; |
|
8
|
|
|
use Bdf\Prime\Mapper\NameResolver\ResolverInterface; |
|
9
|
|
|
use Bdf\Prime\Mapper\NameResolver\SuffixResolver; |
|
10
|
|
|
use Bdf\Prime\ServiceLocator; |
|
11
|
|
|
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @package Bdf\Prime\Mapper |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
class MapperFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ResolverInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $nameResolver; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Psr16CacheInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $metadataCache; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var CacheInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $resultCache; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ResolverInterface $nameResolver |
|
35
|
|
|
* @param Psr16CacheInterface|null $metadataCache |
|
36
|
|
|
* @param CacheInterface|null $resultCache |
|
37
|
|
|
*/ |
|
38
|
107 |
|
public function __construct(ResolverInterface $nameResolver = null, Psr16CacheInterface $metadataCache = null, CacheInterface $resultCache = null) |
|
39
|
|
|
{ |
|
40
|
107 |
|
$this->nameResolver = $nameResolver ?: new SuffixResolver(); |
|
|
|
|
|
|
41
|
107 |
|
$this->metadataCache = $metadataCache; |
|
42
|
107 |
|
$this->resultCache = $resultCache; |
|
43
|
107 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get associated entity mapper |
|
47
|
|
|
* |
|
48
|
|
|
* @param ServiceLocator $serviceLocator |
|
49
|
|
|
* @param string $entityClass |
|
50
|
|
|
* |
|
51
|
|
|
* @return Mapper |
|
52
|
|
|
*/ |
|
53
|
128 |
|
public function build(ServiceLocator $serviceLocator, $entityClass) |
|
54
|
|
|
{ |
|
55
|
128 |
|
return $this->createMapper($serviceLocator, $this->nameResolver->resolve($entityClass), $entityClass); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get mapper object |
|
60
|
|
|
* |
|
61
|
|
|
* @param ServiceLocator $serviceLocator |
|
62
|
|
|
* @param string $mapperClass |
|
63
|
|
|
* @param string $entityClass |
|
64
|
|
|
* |
|
65
|
|
|
* @return Mapper |
|
66
|
|
|
*/ |
|
67
|
149 |
|
public function createMapper(ServiceLocator $serviceLocator, $mapperClass, $entityClass = null) |
|
68
|
|
|
{ |
|
69
|
149 |
|
if (!$this->isMapper($mapperClass)) { |
|
70
|
7 |
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
142 |
|
if ($entityClass === null) { |
|
74
|
21 |
|
$entityClass = $this->nameResolver->reverse($mapperClass); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
142 |
|
$metadata = null; |
|
78
|
142 |
|
if ($this->metadataCache !== null) { |
|
79
|
2 |
|
$cacheKey = $this->getCacheKey($mapperClass); |
|
80
|
2 |
|
$metadata = $this->metadataCache->get($cacheKey); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
142 |
|
$hydrator = $serviceLocator->hydrator($entityClass); |
|
84
|
|
|
|
|
85
|
142 |
|
if (!($hydrator instanceof MapperHydratorInterface)) { |
|
86
|
141 |
|
$hydrator = new MapperHydrator(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** @var Mapper $mapper */ |
|
|
|
|
|
|
90
|
142 |
|
$mapper = new $mapperClass($serviceLocator, $entityClass, $metadata, $hydrator, $this->resultCache); |
|
91
|
|
|
|
|
92
|
142 |
|
if ($this->metadataCache !== null && $metadata === null) { |
|
93
|
1 |
|
$this->metadataCache->set($cacheKey, $mapper->metadata()); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
142 |
|
if ($mapper instanceof MapperFactoryAwareInterface) { |
|
97
|
25 |
|
$mapper->setMapperFactory($this); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
142 |
|
return $mapper; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check if class is a mapper |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $className |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
|
|
|
|
|
109
|
|
|
*/ |
|
110
|
248 |
|
public function isMapper($className) |
|
111
|
|
|
{ |
|
112
|
248 |
|
return is_subclass_of($className, Mapper::class); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Check if class has a mapper |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $className |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool |
|
|
|
|
|
|
121
|
|
|
*/ |
|
122
|
114 |
|
public function isEntity($className) |
|
123
|
|
|
{ |
|
124
|
114 |
|
return $this->isMapper($this->nameResolver->resolve($className)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Create the valid cache key from mapperClass |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $mapperClass |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
2 |
|
private function getCacheKey($mapperClass) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
2 |
|
return str_replace('\\', '.', $mapperClass); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the mapper name resolver |
|
141
|
|
|
* |
|
142
|
|
|
* @return ResolverInterface |
|
143
|
|
|
*/ |
|
144
|
8 |
|
public function getNameResolver() |
|
145
|
|
|
{ |
|
146
|
8 |
|
return $this->nameResolver; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set meta cache |
|
151
|
|
|
* |
|
152
|
|
|
* @param null|Psr16CacheInterface $cache |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public function setMetadataCache(?Psr16CacheInterface $cache) |
|
155
|
|
|
{ |
|
156
|
4 |
|
$this->metadataCache = $cache; |
|
157
|
4 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get meta cache |
|
161
|
|
|
* |
|
162
|
|
|
* @return Psr16CacheInterface |
|
163
|
|
|
*/ |
|
164
|
5 |
|
public function getMetadataCache(): ?Psr16CacheInterface |
|
165
|
|
|
{ |
|
166
|
5 |
|
return $this->metadataCache; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Set the result cache |
|
171
|
|
|
* |
|
172
|
|
|
* @param null|CacheInterface $cache |
|
173
|
|
|
*/ |
|
174
|
3 |
|
public function setResultCache(?CacheInterface $cache) |
|
175
|
|
|
{ |
|
176
|
3 |
|
$this->resultCache = $cache; |
|
177
|
3 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the resul cache |
|
181
|
|
|
* |
|
182
|
|
|
* @return CacheInterface |
|
183
|
|
|
*/ |
|
184
|
3 |
|
public function getResultCache(): ?CacheInterface |
|
185
|
|
|
{ |
|
186
|
3 |
|
return $this->resultCache; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|