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

phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn

Return value

void

File

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

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() !== StackedHttpKernel::class) {
    return;
  }
  $middlewares = [];
  $priorities = [];
  $responders = [];
  foreach ($container
    ->findTaggedServiceIds('http_middleware') as $id => $attributes) {
    $priorities[$id] = $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) {

      // Use interface proxying to allow middleware classes declared final
      // to be set as lazy.
      $decorator
        ->setLazy(TRUE);
      foreach ([
        HttpKernelInterface::class,
        TerminableInterface::class,
      ] as $interface) {
        if (is_a($decorator
          ->getClass(), $interface, TRUE)) {
          $decorator
            ->addTag('proxy', [
            'interface' => $interface,
          ]);
        }
      }
    }
    $decorated_id = $id;
  }
  $arguments = [
    $middlewares_param[0],
    $middlewares_param,
  ];
  $stacked_kernel
    ->setArguments($arguments);
}