1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMS\SerializerBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\GraphNavigator; |
6
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
|
11
|
|
|
class CustomHandlersPass implements CompilerPassInterface |
12
|
|
|
{ |
13
|
32 |
|
public function process(ContainerBuilder $container) |
14
|
|
|
{ |
15
|
32 |
|
$handlers = array(); |
16
|
32 |
|
foreach ($container->findTaggedServiceIds('jms_serializer.handler') as $id => $tags) { |
17
|
5 |
|
foreach ($tags as $attrs) { |
18
|
5 |
|
if (!isset($attrs['type'], $attrs['format'])) { |
19
|
1 |
|
throw new \RuntimeException(sprintf('Each tag named "jms_serializer.handler" of service "%s" must have at least two attributes: "type" and "format".', $id)); |
20
|
|
|
} |
21
|
|
|
|
22
|
4 |
|
$directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION); |
23
|
4 |
|
if (isset($attrs['direction'])) { |
24
|
2 |
|
if (!defined($directionConstant = 'JMS\Serializer\GraphNavigator::DIRECTION_' . strtoupper($attrs['direction']))) { |
25
|
1 |
|
throw new \RuntimeException(sprintf('The direction "%s" of tag "jms_serializer.handler" of service "%s" does not exist.', $attrs['direction'], $id)); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
$directions = array(constant($directionConstant)); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
3 |
View Code Duplication |
foreach ($directions as $direction) { |
|
|
|
|
32
|
3 |
|
$method = isset($attrs['method']) ? $attrs['method'] : HandlerRegistry::getDefaultMethod($direction, $attrs['type'], $attrs['format']); |
33
|
3 |
|
if ($container->getDefinition($id)->isPublic()) { |
34
|
2 |
|
$handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method); |
35
|
2 |
|
} else { |
36
|
1 |
|
$handlers[$direction][$attrs['type']][$attrs['format']] = array(new Reference($id), $method); |
37
|
|
|
} |
38
|
3 |
|
} |
39
|
3 |
|
} |
40
|
30 |
|
} |
41
|
|
|
|
42
|
30 |
|
foreach ($container->findTaggedServiceIds('jms_serializer.subscribing_handler') as $id => $tags) { |
43
|
27 |
|
$class = $container->getDefinition($id)->getClass(); |
44
|
27 |
|
$ref = new \ReflectionClass($class); |
45
|
27 |
|
if (!$ref->implementsInterface('JMS\Serializer\Handler\SubscribingHandlerInterface')) { |
46
|
1 |
|
throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.', $id)); |
47
|
|
|
} |
48
|
|
|
|
49
|
26 |
|
foreach (call_user_func(array($class, 'getSubscribingMethods')) as $methodData) { |
50
|
26 |
|
if (!isset($methodData['format'], $methodData['type'])) { |
51
|
|
|
throw new \RuntimeException(sprintf('Each method returned from getSubscribingMethods of service "%s" must have a "type", and "format" attribute.', $id)); |
52
|
|
|
} |
53
|
|
|
|
54
|
26 |
|
$directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION); |
55
|
26 |
|
if (isset($methodData['direction'])) { |
56
|
26 |
|
$directions = array($methodData['direction']); |
57
|
26 |
|
} |
58
|
|
|
|
59
|
26 |
View Code Duplication |
foreach ($directions as $direction) { |
|
|
|
|
60
|
26 |
|
$method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']); |
61
|
26 |
|
if ($container->getDefinition($id)->isPublic()) { |
62
|
25 |
|
$handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method); |
63
|
25 |
|
} else { |
64
|
1 |
|
$handlers[$direction][$methodData['type']][$methodData['format']] = array(new Reference($id), $method); |
65
|
|
|
} |
66
|
26 |
|
} |
67
|
26 |
|
} |
68
|
29 |
|
} |
69
|
|
|
|
70
|
29 |
|
$container->getDefinition('jms_serializer.handler_registry') |
71
|
29 |
|
->addArgument($handlers); |
72
|
29 |
|
} |
73
|
|
|
} |
74
|
|
|
|
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.