1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMS\SerializerBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
|
11
|
|
|
class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface |
12
|
|
|
{ |
13
|
32 |
|
public function process(ContainerBuilder $container) |
14
|
|
|
{ |
15
|
32 |
|
$listeners = array(); |
16
|
32 |
|
$listenerServices = array(); |
17
|
32 |
|
foreach ($container->findTaggedServiceIds('jms_serializer.event_listener') as $id => $tags) { |
18
|
|
|
|
19
|
4 |
|
foreach ($tags as $attributes) { |
20
|
5 |
|
if (!isset($attributes['event'])) { |
21
|
1 |
|
throw new \RuntimeException(sprintf('The tag "jms_serializer.event_listener" of service "%s" requires an attribute named "event".', $id)); |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
$class = isset($attributes['class']) |
25
|
3 |
|
? strtolower($container->getParameterBag()->resolveValue($attributes['class'])) |
26
|
3 |
|
: null; |
27
|
|
|
|
28
|
3 |
|
$format = isset($attributes['format']) ? $attributes['format'] : null; |
29
|
3 |
|
$method = isset($attributes['method']) ? $attributes['method'] : EventDispatcher::getDefaultMethodName($attributes['event']); |
30
|
3 |
|
$priority = isset($attributes['priority']) ? (integer)$attributes['priority'] : 0; |
31
|
|
|
|
32
|
3 |
View Code Duplication |
if (class_exists(ServiceLocatorTagPass::class) || $container->getDefinition($id)->isPublic()) { |
|
|
|
|
33
|
3 |
|
$listenerServices[$id] = new Reference($id); |
34
|
3 |
|
$listeners[$attributes['event']][$priority][] = array(array($id, $method), $class, $format); |
35
|
3 |
|
} else { |
36
|
|
|
$listeners[$attributes['event']][$priority][] = array(array(new Reference($id), $method), $class, $format); |
37
|
|
|
} |
38
|
3 |
|
} |
39
|
31 |
|
} |
40
|
|
|
|
41
|
31 |
|
foreach ($container->findTaggedServiceIds('jms_serializer.event_subscriber') as $id => $tags) { |
42
|
28 |
|
$subscriberClass = $container->getDefinition($id)->getClass(); |
43
|
|
|
|
44
|
28 |
|
$subscriberClassReflectionObj = new \ReflectionClass($subscriberClass); |
45
|
|
|
|
46
|
28 |
|
if (!$subscriberClassReflectionObj->implementsInterface('JMS\Serializer\EventDispatcher\EventSubscriberInterface')) { |
47
|
1 |
|
throw new \RuntimeException(sprintf('The service "%s" (class: %s) does not implement the EventSubscriberInterface.', $id, $subscriberClass)); |
48
|
|
|
} |
49
|
|
|
|
50
|
27 |
|
foreach (call_user_func(array($subscriberClass, 'getSubscribedEvents')) as $eventData) { |
51
|
27 |
|
if (!isset($eventData['event'])) { |
52
|
|
|
throw new \RuntimeException(sprintf('The service "%s" (class: %s) must return an event for each subscribed event.', $id, $subscriberClass)); |
53
|
|
|
} |
54
|
|
|
|
55
|
27 |
|
$class = isset($eventData['class']) ? strtolower($eventData['class']) : null; |
56
|
27 |
|
$format = isset($eventData['format']) ? $eventData['format'] : null; |
57
|
27 |
|
$method = isset($eventData['method']) ? $eventData['method'] : EventDispatcher::getDefaultMethodName($eventData['event']); |
58
|
27 |
|
$priority = isset($eventData['priority']) ? (integer)$eventData['priority'] : 0; |
59
|
|
|
|
60
|
27 |
View Code Duplication |
if (class_exists(ServiceLocatorTagPass::class) || $container->getDefinition($id)->isPublic()) { |
|
|
|
|
61
|
27 |
|
$listenerServices[$id] = new Reference($id); |
62
|
27 |
|
$listeners[$eventData['event']][$priority][] = array(array($id, $method), $class, $format); |
63
|
27 |
|
} else { |
64
|
|
|
$listeners[$eventData['event']][$priority][] = array(array(new Reference($id), $method), $class, $format); |
65
|
|
|
} |
66
|
27 |
|
} |
67
|
30 |
|
} |
68
|
|
|
|
69
|
30 |
|
if ($listeners) { |
|
|
|
|
70
|
30 |
|
array_walk($listeners, function (&$value, $key) { |
|
|
|
|
71
|
30 |
|
ksort($value); |
72
|
30 |
|
}); |
73
|
|
|
|
74
|
30 |
|
foreach ($listeners as &$events) { |
75
|
30 |
|
$events = call_user_func_array('array_merge', $events); |
76
|
30 |
|
} |
77
|
|
|
|
78
|
30 |
|
$container->findDefinition('jms_serializer.event_dispatcher') |
79
|
30 |
|
->addMethodCall('setListeners', array($listeners)); |
80
|
30 |
|
} |
81
|
|
|
|
82
|
30 |
|
if (class_exists(ServiceLocatorTagPass::class)) { |
83
|
30 |
|
$serviceLocator = ServiceLocatorTagPass::register($container, $listenerServices); |
84
|
30 |
|
$container->getDefinition('jms_serializer.event_dispatcher')->replaceArgument(0, $serviceLocator); |
85
|
30 |
|
} |
86
|
30 |
|
} |
87
|
|
|
} |
88
|
|
|
|
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.