trait BootableCommandTrait

Contains helper methods for console commands that boot up Drupal.

Hierarchy

2 files declare their use of BootableCommandTrait
RecipeCommand.php in core/lib/Drupal/Core/Recipe/RecipeCommand.php
RecipeInfoCommand.php in core/lib/Drupal/Core/Recipe/RecipeInfoCommand.php

File

core/lib/Drupal/Core/Command/BootableCommandTrait.php, line 15

Namespace

Drupal\Core\Command
View source
trait BootableCommandTrait {
    
    /**
     * The class loader.
     *
     * @var object
     */
    protected object $classLoader;
    
    /**
     * Boots up a Drupal environment.
     *
     * @return \Drupal\Core\DrupalKernelInterface
     *   The Drupal kernel.
     *
     * @throws \Exception
     *   Exception thrown if kernel does not boot.
     */
    protected function boot() : DrupalKernelInterface {
        $kernel = new DrupalKernel('prod', $this->classLoader);
        $kernel::bootEnvironment();
        $kernel->setSitePath($this->getSitePath());
        Settings::initialize($kernel->getAppRoot(), $kernel->getSitePath(), $this->classLoader);
        $kernel->boot();
        $kernel->preHandle(Request::createFromGlobals());
        return $kernel;
    }
    
    /**
     * Gets the site path.
     *
     * Defaults to 'sites/default'. For testing purposes this can be overridden
     * using the DRUPAL_DEV_SITE_PATH environment variable.
     *
     * @return string
     *   The site path to use.
     */
    protected function getSitePath() : string {
        return getenv('DRUPAL_DEV_SITE_PATH') ?: 'sites/default';
    }

}

Members

Title Sort descending Modifiers Object type Summary
BootableCommandTrait::$classLoader protected property The class loader.
BootableCommandTrait::boot protected function Boots up a Drupal environment.
BootableCommandTrait::getSitePath protected function Gets the site path.

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