11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \DependencyInjection \Compiler ;
13
13
14
+ use Symfony \Component \DependencyInjection \Compiler \PriorityTaggedServiceTrait ;
14
15
use Symfony \Component \DependencyInjection \ContainerBuilder ;
15
16
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
16
- use Symfony \Component \DependencyInjection \Reference ;
17
17
18
18
/**
19
19
* Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
23
23
*/
24
24
class SerializerPass implements CompilerPassInterface
25
25
{
26
+ use PriorityTaggedServiceTrait;
27
+
26
28
public function process (ContainerBuilder $ container )
27
29
{
28
30
if (!$ container ->hasDefinition ('serializer ' )) {
@@ -31,42 +33,17 @@ public function process(ContainerBuilder $container)
31
33
32
34
// Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service
33
35
$ normalizers = $ this ->findAndSortTaggedServices ('serializer.normalizer ' , $ container );
36
+
37
+ if (empty ($ normalizers )) {
38
+ throw new \RuntimeException ('You must tag at least one service as "serializer.normalizer" to use the Serializer service ' );
39
+ }
34
40
$ container ->getDefinition ('serializer ' )->replaceArgument (0 , $ normalizers );
35
41
36
42
// Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service
37
43
$ encoders = $ this ->findAndSortTaggedServices ('serializer.encoder ' , $ container );
38
- $ container ->getDefinition ('serializer ' )->replaceArgument (1 , $ encoders );
39
- }
40
-
41
- /**
42
- * Finds all services with the given tag name and order them by their priority.
43
- *
44
- * @param string $tagName
45
- * @param ContainerBuilder $container
46
- *
47
- * @return array
48
- *
49
- * @throws \RuntimeException
50
- */
51
- private function findAndSortTaggedServices ($ tagName , ContainerBuilder $ container )
52
- {
53
- $ services = $ container ->findTaggedServiceIds ($ tagName );
54
-
55
- if (empty ($ services )) {
56
- throw new \RuntimeException (sprintf ('You must tag at least one service as "%s" to use the Serializer service ' , $ tagName ));
57
- }
58
-
59
- $ sortedServices = array ();
60
- foreach ($ services as $ serviceId => $ tags ) {
61
- foreach ($ tags as $ attributes ) {
62
- $ priority = isset ($ attributes ['priority ' ]) ? $ attributes ['priority ' ] : 0 ;
63
- $ sortedServices [$ priority ][] = new Reference ($ serviceId );
64
- }
44
+ if (empty ($ encoders )) {
45
+ throw new \RuntimeException ('You must tag at least one service as "serializer.encoder" to use the Serializer service ' );
65
46
}
66
-
67
- krsort ($ sortedServices );
68
-
69
- // Flatten the array
70
- return call_user_func_array ('array_merge ' , $ sortedServices );
47
+ $ container ->getDefinition ('serializer ' )->replaceArgument (1 , $ encoders );
71
48
}
72
49
}
0 commit comments