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
|
24 |
|
public function __construct(Config $config) |
28
|
|
|
{ |
29
|
24 |
|
$this->config = $config; |
30
|
24 |
|
$this->jmsSerializerBuilder = SerializerBuilder::create(); |
31
|
24 |
|
} |
32
|
|
|
|
33
|
24 |
|
public static function createByConfig(Config $config) |
34
|
|
|
{ |
35
|
24 |
|
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->set(Config::EVENT_SUBSCRIBERS, [$eventSubscriber]); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function removeEventSubscribers(EventSubscriberInterface $eventSubscriber) |
74
|
|
|
{ |
75
|
|
|
$eventSubscribers = $this->config->get(Config::EVENT_SUBSCRIBERS, []); |
76
|
|
|
|
77
|
|
|
foreach ($eventSubscribers as $key => $storedEventSubscribers) { |
78
|
|
|
if (\get_class($storedEventSubscribers) === \get_class($eventSubscriber)) { |
79
|
|
|
unset($eventSubscribers[$key]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->config->set(Config::EVENT_SUBSCRIBERS, $eventSubscribers); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function addSerializationHandler(SubscribingHandlerInterface $subscribingHandler) |
89
|
|
|
{ |
90
|
|
|
$this->config->set( |
91
|
|
|
Config::SERIALIZATION_HANDLERS, |
92
|
|
|
[$subscribingHandler] |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function removeSerializationHandler(SubscribingHandlerInterface $subscribingHandler) |
99
|
|
|
{ |
100
|
|
|
$subscribingHandlers = $this->config->get(Config::SERIALIZATION_HANDLERS, []); |
101
|
|
|
|
102
|
|
|
foreach ($subscribingHandlers as $key => $storedSubscribingHandlers) { |
103
|
|
|
if (\get_class($storedSubscribingHandlers) === \get_class($subscribingHandler)) { |
104
|
|
|
unset($subscribingHandlers[$key]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->config->set(Config::SERIALIZATION_HANDLERS, $subscribingHandlers); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setObjectConstructor(ObjectConstructorInterface $objectConstructor) |
114
|
|
|
{ |
115
|
|
|
$this->config->set(Config::OBJECT_CONSTRUCTOR, $objectConstructor); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
24 |
|
public function configureDefaults() |
121
|
|
|
{ |
122
|
24 |
|
$this->jmsSerializerBuilder->addDefaultHandlers(); |
123
|
24 |
|
$this->jmsSerializerBuilder->addDefaultListeners(); |
124
|
|
|
|
125
|
24 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
* @throws \JMS\Serializer\Exception\InvalidArgumentException |
131
|
|
|
* @throws \JMS\Serializer\Exception\RuntimeException |
132
|
|
|
*/ |
133
|
24 |
|
public function build(): SerializerInterface |
134
|
|
|
{ |
135
|
24 |
|
$this->jmsSerializerBuilder->setDebug($this->config->get('debug', false)); |
136
|
|
|
|
137
|
|
|
$this |
138
|
24 |
|
->applyMetadataDirectory() |
139
|
24 |
|
->applyEventSubscribers() |
140
|
24 |
|
->applySerializationHandlers() |
141
|
24 |
|
->applyObjectConstructor(); |
142
|
|
|
|
143
|
24 |
|
$serializer = $this->jmsSerializerBuilder->build(); |
144
|
|
|
|
145
|
24 |
|
return new JMSSerializerAdapter($serializer); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Apply Metadata Directories |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
* @throws \JMS\Serializer\Exception\InvalidArgumentException |
153
|
|
|
*/ |
154
|
24 |
|
protected function applyMetadataDirectory(): self |
155
|
|
|
{ |
156
|
24 |
|
foreach ($this->config->get(Config::METADATA_DIRS, []) as $metadataDir) { |
157
|
24 |
|
$this->jmsSerializerBuilder->addMetadataDir($metadataDir['dir'], $metadataDir['namespacePrefix']); |
158
|
|
|
} |
159
|
|
|
|
160
|
24 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Apply event subscribers |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
* @throws \JMS\Serializer\Exception\InvalidArgumentException |
168
|
|
|
*/ |
169
|
24 |
|
protected function applyEventSubscribers(): self |
170
|
|
|
{ |
171
|
24 |
|
$eventSubscribers = $this->config->get(Config::EVENT_SUBSCRIBERS, []); |
172
|
24 |
|
$this->jmsSerializerBuilder->configureListeners(function (EventDispatcher $dispatcher) use ($eventSubscribers) { |
173
|
24 |
|
foreach ($eventSubscribers as $eventSubscriber) { |
174
|
|
|
$dispatcher->addSubscriber($eventSubscriber); |
175
|
|
|
} |
176
|
24 |
|
}); |
177
|
|
|
|
178
|
24 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Apply serialization handlers |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
* @throws \JMS\Serializer\Exception\RuntimeException |
186
|
|
|
*/ |
187
|
24 |
|
protected function applySerializationHandlers(): self |
188
|
|
|
{ |
189
|
24 |
|
$serializationHandlers = $this->config->get(Config::SERIALIZATION_HANDLERS, []); |
190
|
24 |
|
$this->jmsSerializerBuilder->configureHandlers( |
191
|
24 |
|
function (HandlerRegistry $handlerRegistry) use ($serializationHandlers) { |
192
|
24 |
|
foreach ($serializationHandlers as $handler) { |
193
|
|
|
$handlerRegistry->registerSubscribingHandler($handler); |
194
|
|
|
} |
195
|
24 |
|
} |
196
|
|
|
); |
197
|
|
|
|
198
|
24 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Apply object constructor |
203
|
|
|
* |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
24 |
|
protected function applyObjectConstructor(): self |
207
|
|
|
{ |
208
|
24 |
|
$objectConstructor = $this->config->get(Config::OBJECT_CONSTRUCTOR, null); |
209
|
24 |
|
if (null !== $objectConstructor) { |
210
|
|
|
$this->jmsSerializerBuilder->setObjectConstructor($objectConstructor); |
211
|
|
|
} |
212
|
|
|
|
213
|
24 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|