function StackedKernelPass::process

Same name in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass::process()
  2. 10 core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass::process()
  3. 11.x core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass::process()

File

core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php, line 53

Class

StackedKernelPass
Provides a compiler pass for stacked HTTP kernels.

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) {
    if (!$container->hasDefinition('http_kernel')) {
        return;
    }
    $stacked_kernel = $container->getDefinition('http_kernel');
    // Return now if this is not a stacked kernel.
    if ($stacked_kernel->getClass() !== 'Stack\\StackedHttpKernel') {
        return;
    }
    $middlewares = [];
    $priorities = [];
    $responders = [];
    foreach ($container->findTaggedServiceIds('http_middleware') as $id => $attributes) {
        $priorities[$id] = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
        $middlewares[$id] = $container->getDefinition($id);
        $responders[$id] = !empty($attributes[0]['responder']);
    }
    array_multisort($priorities, SORT_ASC, $middlewares, $responders);
    $decorated_id = 'http_kernel.basic';
    $middlewares_param = [
        new Reference($decorated_id),
    ];
    $first_responder = array_search(TRUE, array_reverse($responders, TRUE), TRUE);
    if ($first_responder) {
        $container->getDefinition($decorated_id)
            ->setLazy(TRUE);
    }
    foreach ($middlewares as $id => $decorator) {
        // Prepend a reference to the middlewares container parameter.
        array_unshift($middlewares_param, new Reference($id));
        // Prepend the inner kernel as first constructor argument.
        $arguments = $decorator->getArguments();
        array_unshift($arguments, new Reference($decorated_id));
        $decorator->setArguments($arguments);
        if ($first_responder === $id) {
            $first_responder = FALSE;
        }
        elseif ($first_responder) {
            $decorator->setLazy(TRUE);
        }
        $decorated_id = $id;
    }
    $arguments = [
        $middlewares_param[0],
        $middlewares_param,
    ];
    $stacked_kernel->setArguments($arguments);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.