1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Component\Rest\Serializer; |
6
|
|
|
|
7
|
|
|
use CCT\Component\Rest\Config; |
8
|
|
|
use JMS\Serializer\Construction\ObjectConstructorInterface; |
9
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
10
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
12
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
13
|
|
|
use JMS\Serializer\SerializerBuilder; |
14
|
|
|
|
15
|
|
|
class JMSSerializerBuilder implements SerializerBuilderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Config |
19
|
|
|
*/ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var SerializerBuilder |
24
|
|
|
*/ |
25
|
|
|
protected $jmsSerializerBuilder; |
26
|
|
|
|
27
|
2 |
|
public function __construct(Config $config) |
28
|
|
|
{ |
29
|
2 |
|
$this->config = $config; |
30
|
2 |
|
$this->jmsSerializerBuilder = SerializerBuilder::create(); |
31
|
2 |
|
} |
32
|
|
|
|
33
|
2 |
|
public static function createByConfig(Config $config) |
34
|
|
|
{ |
35
|
2 |
|
return new static($config); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function addMetadataDir($dir, $namespacePrefix) |
39
|
|
|
{ |
40
|
|
|
$metadataDirs = $this->config->get(Config::METADATA_DIRS, []); |
41
|
|
|
$metadataDirs[] = [ |
42
|
|
|
'dir' => $dir, |
43
|
|
|
'namespacePrefix' => $namespacePrefix |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$this->config->set(Config::METADATA_DIRS, $metadataDirs); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function removeMetadataDir($dir) |
52
|
|
|
{ |
53
|
|
|
$metadataDirs = $this->config->get(Config::METADATA_DIRS, []); |
54
|
|
|
|
55
|
|
|
foreach ($metadataDirs as $key => $metadataDir) { |
56
|
|
|
if ($metadataDir['dir'] == $dir) { |
57
|
|
|
unset($metadataDirs[$key]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->config->set(Config::METADATA_DIRS, $metadataDirs); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function addEventSubscribers(EventSubscriberInterface $eventSubscriber) |
67
|
|
|
{ |
68
|
|
|
$this->config->merge([ |
69
|
|
|
Config::EVENT_SUBSCRIBERS => [$eventSubscriber] |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function removeEventSubscribers(EventSubscriberInterface $eventSubscriber) |
76
|
|
|
{ |
77
|
|
|
$eventSubscribers = $this->config->get(Config::EVENT_SUBSCRIBERS, []); |
78
|
|
|
|
79
|
|
|
foreach ($eventSubscribers as $key => $storedEventSubscribers) { |
80
|
|
|
if (get_class($storedEventSubscribers) === get_class($eventSubscriber)) { |
81
|
|
|
unset($eventSubscribers[$key]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->config->set(Config::EVENT_SUBSCRIBERS, $eventSubscribers); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function addSerializationHandler(SubscribingHandlerInterface $subscribingHandler) |
91
|
|
|
{ |
92
|
|
|
$this->config->merge([ |
93
|
|
|
Config::SERIALIZATION_HANDLERS => [$subscribingHandler] |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function removeSerializationHandler(SubscribingHandlerInterface $subscribingHandler) |
100
|
|
|
{ |
101
|
|
|
$subscribingHandlers = $this->config->get(Config::SERIALIZATION_HANDLERS, []); |
102
|
|
|
|
103
|
|
|
foreach ($subscribingHandlers as $key => $storedSubscribingHandlers) { |
104
|
|
|
if (get_class($storedSubscribingHandlers) === get_class($subscribingHandler)) { |
105
|
|
|
unset($subscribingHandlers[$key]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->config->set(Config::SERIALIZATION_HANDLERS, $subscribingHandlers); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setObjectConstructor(ObjectConstructorInterface $objectConstructor) |
115
|
|
|
{ |
116
|
|
|
$this->config->set(Config::OBJECT_CONSTRUCTOR, $objectConstructor); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
public function configureDefaults() |
122
|
|
|
{ |
123
|
2 |
|
$this->jmsSerializerBuilder->addDefaultHandlers(); |
124
|
2 |
|
$this->jmsSerializerBuilder->addDefaultListeners(); |
125
|
|
|
|
126
|
2 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2 |
|
public function build(): SerializerInterface |
133
|
|
|
{ |
134
|
2 |
|
$this->jmsSerializerBuilder->setDebug($this->config->get('debug', false)); |
135
|
|
|
|
136
|
|
|
$this |
137
|
2 |
|
->applyMetadataDirectory() |
138
|
2 |
|
->applyEventSubscribers() |
139
|
2 |
|
->applySerializationHandlers() |
140
|
2 |
|
->applyObjectConstructor(); |
141
|
|
|
|
142
|
2 |
|
$serializer = $this->jmsSerializerBuilder->build(); |
143
|
|
|
|
144
|
2 |
|
return new JMSSerializerAdapter($serializer); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Apply Metadata Directories |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
2 |
|
protected function applyMetadataDirectory() |
153
|
|
|
{ |
154
|
2 |
|
foreach ($this->config->get(Config::METADATA_DIRS, []) as $metadataDir) { |
155
|
2 |
|
$this->jmsSerializerBuilder->addMetadataDir($metadataDir['dir'], $metadataDir['namespacePrefix']); |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Apply event subscribers |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
2 |
|
protected function applyEventSubscribers() |
167
|
|
|
{ |
168
|
2 |
|
$eventSubscribers = $this->config->get(Config::EVENT_SUBSCRIBERS, []); |
169
|
2 |
|
$this->jmsSerializerBuilder->configureListeners(function (EventDispatcher $dispatcher) use ($eventSubscribers) { |
170
|
2 |
|
foreach ($eventSubscribers as $eventSubscriber) { |
171
|
|
|
$dispatcher->addSubscriber($eventSubscriber); |
172
|
|
|
} |
173
|
2 |
|
}); |
174
|
|
|
|
175
|
2 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Apply serialization handlers |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
2 |
|
protected function applySerializationHandlers() |
184
|
|
|
{ |
185
|
2 |
|
$serializationHandlers = $this->config->get(Config::SERIALIZATION_HANDLERS, []); |
186
|
2 |
|
$this->jmsSerializerBuilder->configureHandlers( |
187
|
2 |
|
function (HandlerRegistry $handlerRegistry) use ($serializationHandlers) { |
188
|
2 |
|
foreach ($serializationHandlers as $handler) { |
189
|
|
|
$handlerRegistry->registerSubscribingHandler($handler); |
190
|
|
|
} |
191
|
2 |
|
} |
192
|
|
|
); |
193
|
|
|
|
194
|
2 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Apply object constructor |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
2 |
|
protected function applyObjectConstructor() |
203
|
|
|
{ |
204
|
2 |
|
$objectConstructor = $this->config->get(Config::OBJECT_CONSTRUCTOR, null); |
205
|
2 |
|
if (null !== $objectConstructor) { |
206
|
|
|
$this->jmsSerializerBuilder->setObjectConstructor($objectConstructor); |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|