1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk; |
4
|
|
|
|
5
|
|
|
use Mapado\RestClientSdk\Model\ModelHydrator; |
6
|
|
|
use Mapado\RestClientSdk\Model\Serializer; |
7
|
|
|
use ProxyManager\Configuration; |
8
|
|
|
use ProxyManager\Factory\LazyLoadingGhostFactory; |
9
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
10
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Sdk Client |
14
|
|
|
*/ |
15
|
|
|
class SdkClient |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var RestClient |
19
|
|
|
*/ |
20
|
|
|
protected $restClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Cache item pool. |
24
|
|
|
* |
25
|
|
|
* @var CacheItemPoolInterface |
26
|
|
|
*/ |
27
|
|
|
protected $cacheItemPool; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cache prefix. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $cachePrefix; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Mapping |
38
|
|
|
*/ |
39
|
|
|
private $mapping; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Serializer |
43
|
|
|
*/ |
44
|
|
|
private $serializer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ModelHydrator |
48
|
|
|
*/ |
49
|
|
|
private $modelHydrator; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $repositoryList = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* proxyManagerConfig |
58
|
|
|
* |
59
|
|
|
* @var Configuration |
60
|
|
|
*/ |
61
|
|
|
private $proxyManagerConfig; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* unitOfWork |
65
|
|
|
* |
66
|
|
|
* @var UnitOfWork |
67
|
|
|
*/ |
68
|
|
|
private $unitOfWork; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Constructor. |
72
|
|
|
* |
73
|
|
|
* @param RestClient $restClient |
74
|
|
|
* @param Mapping $mapping |
75
|
|
|
* @param Serializer|null $serializer |
76
|
|
|
*/ |
77
|
|
|
public function __construct(RestClient $restClient, Mapping $mapping, UnitOfWork $unitOfWork, Serializer $serializer = null) |
78
|
|
|
{ |
79
|
1 |
|
$this->restClient = $restClient; |
80
|
1 |
|
$this->mapping = $mapping; |
81
|
1 |
|
$this->unitOfWork = $unitOfWork; |
82
|
1 |
|
if (!$serializer) { |
83
|
|
|
$serializer = new Serializer($this->mapping, $this->unitOfWork); |
84
|
|
|
} |
85
|
1 |
|
$this->serializer = $serializer; |
86
|
1 |
|
$this->serializer->setSdk($this); |
87
|
|
|
|
88
|
1 |
|
$this->modelHydrator = new ModelHydrator($this); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* setCacheItemPool |
93
|
|
|
* |
94
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
95
|
|
|
* |
96
|
|
|
* @return SdkClient |
97
|
|
|
*/ |
98
|
|
|
public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool, $cachePrefix = '') |
99
|
|
|
{ |
100
|
|
|
$this->cacheItemPool = $cacheItemPool; |
101
|
|
|
$this->cachePrefix = $cachePrefix; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* getCacheItemPool |
108
|
|
|
* |
109
|
|
|
* @return ?CacheItemPoolInterface |
110
|
|
|
*/ |
111
|
|
|
public function getCacheItemPool() |
112
|
|
|
{ |
113
|
|
|
return $this->cacheItemPool; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* getCachePrefix |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getCachePrefix() |
122
|
|
|
{ |
123
|
|
|
return $this->cachePrefix; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* getRepository |
128
|
|
|
* |
129
|
|
|
* @param string $modelName |
130
|
|
|
* |
131
|
|
|
* @return EntityRepository |
132
|
|
|
*/ |
133
|
|
|
public function getRepository($modelName) |
134
|
|
|
{ |
135
|
|
|
// get repository by key |
136
|
1 |
|
$metadata = $this->mapping->getClassMetadataByKey($modelName); |
137
|
1 |
|
if (!$metadata) { |
138
|
|
|
// get by classname |
139
|
1 |
|
$metadata = $this->mapping->getClassMetadata($modelName); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
$modelName = $metadata->getModelName(); |
143
|
|
|
|
144
|
1 |
|
if (!isset($this->repositoryList[$modelName])) { |
145
|
1 |
|
$repositoryName = $metadata->getRepositoryName() ?: '\Mapado\RestClientSdk\EntityRepository'; |
146
|
1 |
|
$this->repositoryList[$modelName] = new $repositoryName($this, $this->restClient, $this->unitOfWork, $modelName); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $this->repositoryList[$modelName]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* getRestClient |
154
|
|
|
* |
155
|
|
|
* @return RestClient |
156
|
|
|
*/ |
157
|
|
|
public function getRestClient() |
158
|
|
|
{ |
159
|
1 |
|
return $this->restClient; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* getMapping |
164
|
|
|
* |
165
|
|
|
* @return Mapping |
166
|
|
|
*/ |
167
|
|
|
public function getMapping() |
168
|
|
|
{ |
169
|
1 |
|
return $this->mapping; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* getSerializer |
174
|
|
|
* |
175
|
|
|
* @return Serializer |
176
|
|
|
*/ |
177
|
|
|
public function getSerializer() |
178
|
|
|
{ |
179
|
1 |
|
return $this->serializer; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* getModelHydrator |
184
|
|
|
* |
185
|
|
|
* @return ModelHydrator |
186
|
|
|
*/ |
187
|
|
|
public function getModelHydrator() |
188
|
|
|
{ |
189
|
|
|
return $this->modelHydrator; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* createProxy |
194
|
|
|
* |
195
|
|
|
* @param string $id |
196
|
|
|
* |
197
|
|
|
* @return \ProxyManager\Proxy\GhostObjectInterface |
198
|
|
|
*/ |
199
|
|
|
public function createProxy($id) |
200
|
|
|
{ |
201
|
|
|
$key = $this->mapping->getKeyFromId($id); |
202
|
|
|
$classMetadata = $this->mapping->getClassMetadataByKey($key); |
203
|
|
|
|
204
|
|
|
$modelName = $classMetadata->getModelName(); |
205
|
|
|
|
206
|
|
|
$sdk = $this; |
207
|
|
|
|
208
|
|
|
if ($this->proxyManagerConfig) { |
209
|
|
|
$factory = new LazyLoadingGhostFactory($this->proxyManagerConfig); |
210
|
|
|
} else { |
211
|
|
|
$factory = new LazyLoadingGhostFactory(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$proxyModelName = preg_replace('/^\\\\*/', '', $modelName); |
215
|
|
|
|
216
|
|
|
$initializer = function ( |
217
|
|
|
GhostObjectInterface $proxy, |
218
|
|
|
string $method, |
219
|
|
|
array $parameters, |
220
|
|
|
&$initializer, |
221
|
|
|
array $properties |
222
|
|
|
) use ( |
223
|
|
|
$sdk, |
224
|
|
|
$classMetadata, |
225
|
|
|
$id, |
226
|
|
|
$proxyModelName |
227
|
|
|
) { |
228
|
|
|
$isAllowedMethod = 'jsonSerialize' === $method |
229
|
|
|
|| '__set' === $method; |
230
|
|
|
|
231
|
|
|
if (!$isAllowedMethod) { |
232
|
|
|
$initializer = null; // disable initialization |
233
|
|
|
// load data and modify the object here |
234
|
|
|
if ($id) { |
235
|
|
|
$repository = $sdk->getRepository($classMetadata->getModelName()); |
236
|
|
|
$model = $repository->find($id); |
237
|
|
|
|
238
|
|
|
$attributeList = $classMetadata->getAttributeList(); |
239
|
|
|
|
240
|
|
|
foreach ($attributeList as $attribute) { |
241
|
|
|
$getter = 'get' . ucfirst($attribute->getAttributeName()); |
242
|
|
|
$value = $model->$getter(); |
243
|
|
|
$properties['\0' . $proxyModelName . '\0' . $attribute->getAttributeName()] = $value; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return true; // confirm that initialization occurred correctly |
248
|
|
|
} |
249
|
|
|
}; |
250
|
|
|
|
251
|
|
|
// initialize the proxy instance |
252
|
|
|
$instance = $factory->createProxy( |
253
|
|
|
$modelName, |
254
|
|
|
$initializer, |
255
|
|
|
[ |
256
|
|
|
'skippedProperties' => [$proxyModelName . '\0id'], |
257
|
|
|
] |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
// set the id of the object |
261
|
|
|
$idReflexion = new \ReflectionProperty( |
262
|
|
|
$modelName, |
263
|
|
|
$classMetadata->getIdentifierAttribute()->getAttributeName() |
264
|
|
|
); |
265
|
|
|
$idReflexion->setAccessible(true); |
266
|
|
|
$idReflexion->setValue($instance, $id); |
267
|
|
|
|
268
|
|
|
return $instance; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Setter for fileCachePath |
273
|
|
|
* |
274
|
|
|
* @param string $fileCachePath |
275
|
|
|
* |
276
|
|
|
* @return SdkClient |
277
|
|
|
*/ |
278
|
|
|
public function setFileCachePath($fileCachePath) |
279
|
|
|
{ |
280
|
|
|
$this->proxyManagerConfig = new Configuration(); |
281
|
|
|
$this->proxyManagerConfig->setProxiesTargetDir($fileCachePath); |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|