Browse Source

added priority support for normalizer order

Lukas Kahwe Smith 14 năm trước cách đây
mục cha
commit
fc08ac7c34

+ 7 - 2
DependencyInjection/Compiler/RegisterNormalizersPass.php

@@ -32,9 +32,14 @@ class RegisterNormalizersPass implements CompilerPassInterface
         foreach ($container->findTaggedServiceIds('jms_serializer.normalizer') as $id => $attributes) {
         foreach ($container->findTaggedServiceIds('jms_serializer.normalizer') as $id => $attributes) {
             $def = $container->findDefinition($id);
             $def = $container->findDefinition($id);
             $strict = ContainerInterface::SCOPE_PROTOTYPE !== $def->getScope();
             $strict = ContainerInterface::SCOPE_PROTOTYPE !== $def->getScope();
-            $normalizers[] = new Reference($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $strict);
+            $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
+            $normalizers[$priority][] = new Reference($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $strict);
         }
         }
 
 
+        // sort by priority and flatten
+        krsort($normalizers);
+        $normalizers = call_user_func_array('array_merge', $normalizers);
+
         foreach (array_keys($container->findTaggedServiceIds('jms_serializer.serializer')) as $id) {
         foreach (array_keys($container->findTaggedServiceIds('jms_serializer.serializer')) as $id) {
             $container
             $container
                 ->getDefinition($id)
                 ->getDefinition($id)
@@ -42,4 +47,4 @@ class RegisterNormalizersPass implements CompilerPassInterface
             ;
             ;
         }
         }
     }
     }
-}
+}