You are here

public function StackedKernelPass::process in Zircon Profile 8

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

You can modify the container here before it is dumped to PHP code.

Parameters

ContainerBuilder $container:

Overrides CompilerPassInterface::process

File

core/lib/Drupal/Core/DependencyInjection/Compiler/StackedKernelPass.php, line 58
Contains \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass.

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);
}