preHandle example


  protected function initKernel(Request $request) {
    $this->kernel = DrupalKernel::createFromRequest($request$this->classLoader, 'prod', TRUE);
    $this->kernel->boot();
    // Add our request to the stack and route context.     $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
    $this->kernel->preHandle($request);
    return $this->kernel->getContainer();
  }

  /** * Installs the default theme defined by `static::$defaultTheme` when needed. * * To install a test theme outside of the testing environment, add * @code * $settings['extension_discovery_scan_tests'] = TRUE; * @endcode * to your settings.php. * * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * The container. * * @throws \Exception * If the test case does not initialize default theme. */

  public function __construct(HttpKernelInterface $http_kernel, DrupalKernelInterface $drupal_kernel) {
    $this->httpKernel = $http_kernel;
    $this->drupalKernel = $drupal_kernel;
  }

  /** * {@inheritdoc} */
  public function handle(Request $request$type = self::MAIN_REQUEST, $catch = TRUE): Response {
    $this->drupalKernel->preHandle($request);

    return $this->httpKernel->handle($request$type$catch);
  }

}
class ServiceDestructionTest extends KernelTestBase {

  /** * Verifies that services are destructed when used. */
  public function testDestructionUsed() {
    // Enable the test module to add it to the container.     $this->enableModules(['service_provider_test']);

    $request = $this->container->get('request_stack')->getCurrentRequest();
    $kernel = $this->container->get('kernel');
    $kernel->preHandle($request);

    // The service has not been destructed yet.     $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));

    // Call the class and then terminate the kernel     $this->container->get('service_provider_test_class');

    $response = new Response();
    $kernel->terminate($request$response);
    $this->assertTrue(\Drupal::state()->get('service_provider_test.destructed'));
  }

  

  return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');
}

try {
  $request = Request::createFromGlobals();
  $kernel = DrupalKernel::createFromRequest($request$autoloader, 'prod');
  $kernel->boot();
  // A route is required for route matching.   $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
  $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
  $kernel->preHandle($request);
  // Ensure our request includes the session if appropriate.   if (PHP_SAPI !== 'cli') {
    $request->setSession($kernel->getContainer()->get('session'));
  }
}
catch (HttpExceptionInterface $e) {
  $response = new Response('', $e->getStatusCode());
  $response->prepare($request)->send();
  exit;
}

try {
      static::bootEnvironment();

      // First boot up basic things, like loading the include files.       $this->initializeSettings($request);
      ReverseProxyMiddleware::setSettingsOnRequest($request, Settings::getInstance());
      $this->boot();
      $container = $this->getContainer();
      /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
      $request_stack = $container->get('request_stack');
      $request_stack->push($request);
      $this->preHandle($request);

      // Handle the actual request. We need the session both for authentication       // as well as the DB update, like       // \Drupal\system\Controller\DbUpdateController::batchFinished.       $this->bootSession($request);
      $result = $this->handleRaw($request);
      $this->shutdownSession($request);

      return $result;
    }
    catch (\Exception $e) {
      
Settings::initialize($this->container->getParameter('app.root'), DrupalKernel::findSitePath($request)$class_loader);

      // After writing settings.php, the installer removes write permissions       // from the site directory. To allow drupal_generate_test_ua() to write       // a file containing the private key for drupal_valid_test_ua(), the site       // directory has to be writable.       // BrowserTestBase::tearDown() will delete the entire test site directory.       // Not using File API; a potential error must trigger a PHP warning.       chmod($this->container->getParameter('app.root') . '/' . $this->siteDirectory, 0777);
      $this->kernel = DrupalKernel::createFromRequest($request$class_loader, 'prod', FALSE);
      $this->kernel->boot();
      $this->kernel->preHandle($request);
      $this->container = $this->kernel->getContainer();

      // Manually configure the test mail collector implementation to prevent       // tests from sending out emails and collect them in state instead.       $this->container->get('config.factory')
        ->getEditable('system.mail')
        ->set('interface.default', 'test_mail_collector')
        ->save();

      $this->installDefaultThemeFromClassProperty($this->container);
    }
  }
    // to avoid a subsequent rebuild or setting the kernel into the     // pre-installer mode.     $extensions = $modules ? $this->getExtensionsForModules($modules) : [];
    $kernel->updateModules($extensions$extensions);

    // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),     // which is required to initialize legacy global variables.     $request = Request::create('/');
    $kernel->boot();
    $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
    $kernel->preHandle($request);

    $this->container = $kernel->getContainer();

    // Run database tasks and check for errors.     $installer_class = $namespace . "\\Install\\Tasks";
    $errors = (new $installer_class())->runTasks();
    if (!empty($errors)) {
      $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
    }

    if ($modules) {
      
chdir($root);

    $this->classLoader = require 'autoload.php';
    $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
    $kernel::bootEnvironment();
    $kernel->setSitePath($input->getOption('site-path'));
    Settings::initialize($kernel->getAppRoot()$kernel->getSitePath()$this->classLoader);

    $request = Request::createFromGlobals();

    $kernel->boot();
    $kernel->preHandle($request);

    $container = $kernel->getContainer();
    $uid = $input->getArgument('uid');
    if (!is_numeric($uid)) {
      throw new InvalidArgumentException(sprintf('The "uid" argument needs to be an integer, but it is "%s".', $uid));
    }
    $userEntity = $container->get('entity_type.manager')
      ->getStorage('user')
      ->load($uid);
    $url = user_pass_reset_url($userEntity) . '/login';
    $output->writeln($url);

    
Home | Imprint | This part of the site doesn't use cookies.