1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vox\Webservice\Proxy; |
4
|
|
|
|
5
|
|
|
use ProxyManager\Factory\AccessInterceptorValueHolderFactory; |
6
|
|
|
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface; |
7
|
|
|
use Vox\Metadata\PropertyMetadata; |
8
|
|
|
use Vox\Webservice\Mapping\BelongsTo; |
9
|
|
|
use Vox\Webservice\Metadata\TransferMetadata; |
10
|
|
|
use Vox\Webservice\TransferManagerInterface; |
11
|
|
|
|
12
|
|
|
class ProxyFactory implements ProxyFactoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AccessInterceptorValueHolderFactory |
16
|
|
|
*/ |
17
|
|
|
private $accessInterceptorFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \ProxyManager\Configuration |
21
|
|
|
*/ |
22
|
|
|
private $proxyConfig; |
23
|
|
|
|
24
|
6 |
|
public function __construct(\ProxyManager\Configuration $proxyConfig = null) |
25
|
|
|
{ |
26
|
6 |
|
$this->accessInterceptorFactory = new AccessInterceptorValueHolderFactory($proxyConfig); |
27
|
6 |
|
$this->proxyConfig = $proxyConfig; |
28
|
6 |
|
} |
29
|
|
|
|
30
|
5 |
|
public function createProxy($class, TransferManagerInterface $transferManager): AccessInterceptorValueHolderInterface |
31
|
|
|
{ |
32
|
5 |
|
$className = is_object($class) ? get_class($class) : $class; |
33
|
5 |
|
$metadata = $transferManager->getClassMetadata($className); |
34
|
5 |
|
$object = is_object($class) ? $class : new $class(); |
35
|
|
|
|
36
|
5 |
|
$interceptors = []; |
37
|
|
|
|
38
|
5 |
|
foreach ($metadata->propertyMetadata as $name => $config) { |
39
|
5 |
|
$getter = sprintf('get%s', ucfirst($name)); |
40
|
|
|
|
41
|
5 |
|
if (isset($metadata->methodMetadata[$getter])) { |
42
|
5 |
|
$interceptors[$getter] = $this->createGetterInterceptor($metadata, $name, $object, $transferManager); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
return $this->accessInterceptorFactory->createProxy($object, $interceptors); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function createGetterInterceptor( |
50
|
|
|
TransferMetadata $metadata, |
51
|
|
|
string $name, |
52
|
|
|
$object, |
53
|
|
|
TransferManagerInterface $transferManager |
54
|
|
|
): callable { |
55
|
5 |
|
return function () use ($metadata, $name, $object, $transferManager) { |
56
|
|
|
/* @var $propertyMetadata PropertyMetadata */ |
57
|
1 |
|
$propertyMetadata = $metadata->propertyMetadata[$name]; |
58
|
1 |
|
$type = $propertyMetadata->type; |
59
|
|
|
|
60
|
1 |
|
if (class_exists($type)) { |
61
|
1 |
|
$belongsTo = $propertyMetadata->getAnnotation(BelongsTo::class); |
62
|
|
|
|
63
|
1 |
|
if ($belongsTo instanceof BelongsTo && empty($propertyMetadata->getValue($object))) { |
64
|
|
|
$data = $transferManager |
65
|
1 |
|
->find($type, $metadata->propertyMetadata[$belongsTo->foreignField]->getValue($object)); |
66
|
|
|
|
67
|
1 |
|
$propertyMetadata->setValue($object, $data); |
68
|
|
|
} |
69
|
|
|
} |
70
|
5 |
|
}; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function registerProxyAutoloader() |
74
|
|
|
{ |
75
|
|
|
if ($this->proxyConfig) { |
76
|
|
|
spl_autoload_register($this->proxyConfig->getProxyAutoloader()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|