1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\MetadataDriver; |
7
|
|
|
|
8
|
|
|
use Interop\Container\ContainerInterface; |
9
|
|
|
use Metadata\Driver\DriverInterface; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LazyLoadingDriver |
14
|
|
|
* |
15
|
|
|
* @package Nnx\JmsSerializerModule\MetadataDriver |
16
|
|
|
*/ |
17
|
|
|
class LazyLoadingDriver implements DriverInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Контейнер в катором распологаются драйвера |
21
|
|
|
* |
22
|
|
|
* @var ContainerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $serviceLocator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Идендификатор драйвера |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $realDriverId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* LazyLoadingDriver constructor. |
35
|
|
|
* |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
* @param $realDriverId |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ContainerInterface $container, $realDriverId) |
40
|
|
|
{ |
41
|
|
|
$this->setServiceLocator($container); |
42
|
|
|
$this->setRealDriverId($realDriverId); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Возвращает контейнер в катором распологаются драйвера |
47
|
|
|
* |
48
|
|
|
* @return ContainerInterface |
49
|
|
|
*/ |
50
|
|
|
public function getServiceLocator() |
51
|
|
|
{ |
52
|
|
|
return $this->serviceLocator; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Устанавливает контейнер в катором распологаются драйвера |
57
|
|
|
* |
58
|
|
|
* @param ContainerInterface $serviceLocator |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setServiceLocator(ContainerInterface $serviceLocator) |
63
|
|
|
{ |
64
|
|
|
$this->serviceLocator = $serviceLocator; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Возвращает идендификатор драйвера |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getRealDriverId() |
75
|
|
|
{ |
76
|
|
|
return $this->realDriverId; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Устанавливает идендификатор драйвера |
81
|
|
|
* |
82
|
|
|
* @param string $realDriverId |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setRealDriverId($realDriverId) |
87
|
|
|
{ |
88
|
|
|
$this->realDriverId = $realDriverId; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@ineheritdoc} |
95
|
|
|
* @param ReflectionClass $class |
96
|
|
|
* |
97
|
|
|
* @return \Metadata\ClassMetadata |
98
|
|
|
* @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException |
99
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
100
|
|
|
* @throws \Interop\Container\Exception\ContainerException |
101
|
|
|
*/ |
102
|
|
|
public function loadMetadataForClass(ReflectionClass $class) |
103
|
|
|
{ |
104
|
|
|
$container = $this->getServiceLocator(); |
105
|
|
|
$id = $this->getRealDriverId(); |
106
|
|
|
$metadataDriver = $container->get($id); |
107
|
|
|
|
108
|
|
View Code Duplication |
if (!$metadataDriver instanceof DriverInterface) { |
|
|
|
|
109
|
|
|
$errMsg = sprintf('Metadata driver not implement %s', DriverInterface::class); |
110
|
|
|
throw new Exception\RuntimeException($errMsg); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $metadataDriver->loadMetadataForClass($class); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.