bootEnvironment example

/** * {@inheritdoc} * * @throws \Symfony\Component\Console\Exception\InvalidArgumentException */
  protected function execute(InputInterface $input, OutputInterface $output): int {
    $root = dirname(__DIR__, 5);
    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)) {
      
/** * Boots up a Drupal environment. * * @return \Drupal\Core\DrupalKernelInterface * The Drupal kernel. * * @throws \Exception * Exception thrown if kernel does not boot. */
  protected function boot() {
    $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
    $kernel::bootEnvironment();
    $kernel->setSitePath($this->getSitePath());
    Settings::initialize($kernel->getAppRoot()$kernel->getSitePath()$this->classLoader);
    $kernel->boot();
    // Some services require a request to work. For example, CommentManager.     // This is needed as generating the URL fires up entity load hooks.     $kernel->getContainer()
      ->get('request_stack')
      ->push(Request::createFromGlobals());

    return $kernel;
  }

  

  public static function createFromRequest(Request $request$class_loader$environment$allow_dumping = TRUE, $app_root = NULL) {
    $kernel = new static($environment$class_loader$allow_dumping$app_root);
    static::bootEnvironment($app_root);
    $kernel->initializeSettings($request);
    return $kernel;
  }

  /** * Constructs a DrupalKernel object. * * @param string $environment * String indicating the environment, e.g. 'prod' or 'dev'. * @param $class_loader * The class loader. Normally \Composer\Autoload\ClassLoader, as included by * the front controller, but may also be decorated. * @param bool $allow_dumping * (optional) FALSE to stop the container from being written to or read * from disk. Defaults to TRUE. * @param string $app_root * (optional) The path to the application root as a string. If not supplied, * the application root will be computed. */
protected function cacheDrupalContainer(array $container_definition) {
    // Don't save this particular container to cache, so it does not leak into     // the main site at all.     return FALSE;
  }

  /** * {@inheritdoc} */
  public function handle(Request $request$type = self::MAIN_REQUEST, $catch = TRUE): Response {
    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);

      
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// Change the directory to the Drupal root. chdir('..');

$autoloader = require_once __DIR__ . '/../autoload.php';
require_once __DIR__ . '/includes/utility.inc';

$request = Request::createFromGlobals();
// Manually resemble early bootstrap of DrupalKernel::boot(). DrupalKernel::bootEnvironment();

try {
  Settings::initialize(dirname(__DIR__), DrupalKernel::findSitePath($request)$autoloader);
}
catch (HttpExceptionInterface $e) {
  $response = new Response('', $e->getStatusCode());
  $response->prepare($request)->send();
  exit;
}

if (Settings::get('rebuild_access', FALSE) ||
  (
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Allow tests to compare MarkupInterface objects via assertEquals().     $this->registerComparator(new MarkupInterfaceComparator());

    $this->root = static::getDrupalRoot();
    $this->initFileCache();
    $this->bootEnvironment();
    $this->bootKernel();
  }

  /** * {@inheritdoc} */
  public function __get(string $name) {
    if ($name === 'randomGenerator') {
      return Random::getGenerator();
    }
  }

  
return $this->install($this->classLoader, $io$install_profile$input->getOption('langcode')$this->getSitePath()$input->getOption('site-name'));
  }

  /** * Returns whether there is already an existing Drupal installation. * * @return bool */
  protected function isDrupalInstalled() {
    try {
      $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
      $kernel::bootEnvironment();
      $kernel->setSitePath($this->getSitePath());
      Settings::initialize($kernel->getAppRoot()$kernel->getSitePath()$this->classLoader);
      $kernel->boot();
    }
    catch (ConnectionNotDefinedException $e) {
      return FALSE;
    }
    return !empty(Database::getConnectionInfo());
  }

  /** * Installs Drupal with specified installation profile. * * @param object $class_loader * The class loader. * @param \Symfony\Component\Console\Style\SymfonyStyle $io * The Symfony output decorator. * @param string $profile * The installation profile to use. * @param string $langcode * The language to install the site in. * @param string $site_path * The path to install the site to, like 'sites/default'. * @param string $site_name * The site name. * * @throws \Exception * Thrown when failing to create the $site_path directory or settings.php. * * @return int * The command exit status. */

class DrupalKernelTest extends KernelTestBase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    // Do not invoke KernelTestBase::setUp(), since that would set up further     // environment aspects, which would distort this test, because it tests the     // DrupalKernel (re-)building itself.     $this->root = static::getDrupalRoot();
    $this->bootEnvironment();
  }

  /** * Build a kernel for testings. * * Because the bootstrap is in DrupalKernel::boot and that involved loading * settings from the filesystem we need to go to extra lengths to build a kernel * for testing. * * @param \Symfony\Component\HttpFoundation\Request $request * A request object to use in booting the kernel. * @param array $modules_enabled * A list of modules to enable on the kernel. * * @return \Drupal\Core\DrupalKernel * New kernel for testing. */
Home | Imprint | This part of the site doesn't use cookies.