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