1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\Serializer; |
7
|
|
|
|
8
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
9
|
|
|
use Zend\Serializer\Adapter\AdapterInterface; |
10
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
11
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
use Nnx\JmsSerializerModule\Options\ModuleOptions; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class JmsSerializerAbstractFactory |
17
|
|
|
* |
18
|
|
|
* @package Nnx\JmsSerializerModule\Serializer |
19
|
|
|
*/ |
20
|
|
|
class JmsSerializerAbstractFactory implements AbstractFactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
* |
25
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
26
|
|
|
* @param $name |
27
|
|
|
* @param $requestedName |
28
|
|
|
* |
29
|
|
|
* @return bool|void |
30
|
|
|
*/ |
31
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
32
|
|
|
{ |
33
|
|
|
return 0 === strpos($requestedName, 'nnxJmsSerializer.serializers.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
* |
39
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
40
|
|
|
* @param $name |
41
|
|
|
* @param $requestedName |
42
|
|
|
* |
43
|
|
|
* @return AdapterInterface |
44
|
|
|
* @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException |
45
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
46
|
|
|
*/ |
47
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
48
|
|
|
{ |
49
|
|
|
$appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator; |
50
|
|
|
|
51
|
|
|
$jmsSerializerName = substr($requestedName, 29); |
52
|
|
|
|
53
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsManager */ |
54
|
|
|
$moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
55
|
|
|
|
56
|
|
|
/** @var ModuleOptions $moduleOptions */ |
57
|
|
|
$moduleOptions = $moduleOptionsManager->get(ModuleOptions::class); |
58
|
|
|
|
59
|
|
|
$serializerConfig = $moduleOptions->getSerializer($jmsSerializerName); |
60
|
|
|
|
61
|
|
|
$options = $serializerConfig->getOptions(); |
62
|
|
|
$serializer = $serviceLocator->get( |
|
|
|
|
63
|
|
|
$serializerConfig->getName(), |
64
|
|
|
[ |
|
|
|
|
65
|
|
|
'metadataFactory' => $options->getMetadataFactory(), |
66
|
|
|
'handlerRegistry' => $options->getHandlerRegistry(), |
67
|
|
|
'objectConstructor' => $options->getObjectConstructor(), |
68
|
|
|
'serializationVisitors' => $options->getSerializationVisitors(), |
69
|
|
|
'deserializationVisitors' => $options->getDeserializationVisitors(), |
70
|
|
|
'eventDispatcher' => $options->getEventDispatcher(), |
71
|
|
|
'typeParser' => $options->getTypeParser() |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
return $serializer; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|