diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php index b20db35..e5edb51 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php @@ -28,31 +28,37 @@ public function process(ContainerBuilder $container) { return; } - $previous_id = 'http_kernel'; - $first = $http_kernel = $container->getDefinition('http_kernel'); - - $kernel_map = array(); + $middlewares = array(); + $priorities = array(); foreach ($container->findTaggedServiceIds('http_middleware') as $id => $attributes) { - // @todo Priority sorting. - $kernel_map[$id] = $container->getDefinition($id); + $priorities[$id] = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $middlewares[$id] = $container->getDefinition($id); } - foreach ($kernel_map as $id => $decorator) { - // Prepend the previous kernel as first constructor argument. - $arguments = $decorator->getArguments(); - array_unshift($arguments, new Reference($previous_id)); - $decorator->setArguments($arguments); - - $previous_id = $id; - $http_kernel = $decorator; + if (empty($middlewares)) { + $container->addAliases(array('http_kernel_stack' => 'http_kernel')); } + else { + array_multisort($priorities, SORT_DESC, $middlewares, SORT_DESC); + + $previous_id = 'http_kernel'; + $first = $http_kernel = $container->getDefinition('http_kernel'); - // Register the 'stack' service as being the very first one http stacked - // kernel that will decorate all others. - $container->addAliases(array( - 'http_kernel_stack' => $id, - )); + foreach ($middlewares as $id => $decorator) { + // Prepend the previous kernel as first constructor argument. + $arguments = $decorator->getArguments(); + array_unshift($arguments, new Reference($previous_id)); + $decorator->setArguments($arguments); + + $previous_id = $id; + $http_kernel = $decorator; + } + + // Register the 'stack' service as being the very first one http stacked + // kernel that will decorate all others. + $container->addAliases(array('http_kernel_stack' => $id)); + } } }