1111
1212namespace Symfony \Bundle \FrameworkBundle \DependencyInjection \Compiler ;
1313
14+ use Symfony \Component \DependencyInjection \Compiler \PriorityTaggedServiceTrait ;
1415use Symfony \Component \DependencyInjection \ContainerBuilder ;
1516use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
16- use Symfony \Component \DependencyInjection \Reference ;
1717
1818/**
1919 * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
2323 */
2424class SerializerPass implements CompilerPassInterface
2525{
26+ use PriorityTaggedServiceTrait;
27+
2628 public function process (ContainerBuilder $ container )
2729 {
2830 if (!$ container ->hasDefinition ('serializer ' )) {
@@ -31,42 +33,17 @@ public function process(ContainerBuilder $container)
3133
3234 // Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service
3335 $ 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+ }
3440 $ container ->getDefinition ('serializer ' )->replaceArgument (0 , $ normalizers );
3541
3642 // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service
3743 $ 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 ' );
6546 }
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 );
7148 }
7249}
0 commit comments