DrupalKernel
class DrupalKernel implements DrupalKernelInterface, TerminableInterface (View source)
The DrupalKernel class is the core of Drupal itself.
This class is responsible for building the Dependency Injection Container and also deals with the registration of service providers. It allows registered service providers to add their services to the container. Core provides the CoreServiceProvider, which, in addition to registering any core services that cannot be registered in the core.services.yaml file, adds any compiler passes needed by core, e.g. for processing tagged services. Each module can add its own service provider, i.e. a class implementing Drupal\Core\DependencyInjection\ServiceProvider, to register services to the container, or modify existing services.
Traits
Provides methods for checking if Drupal is already installed.
Properties
| protected string | $phpArrayDumperClass | Holds the class used for dumping the container to a PHP array. |
|
| protected array | $defaultBootstrapContainerDefinition | Holds the default bootstrap container definition. |
|
| protected string | $bootstrapContainerClass | Holds the class used for instantiating the bootstrap container. |
|
| protected ContainerInterface | $bootstrapContainer | Holds the bootstrap container. |
|
| protected ContainerInterface | $container | Holds the container instance. |
|
| protected string | $environment | The environment, e.g. 'testing', 'install'. |
|
| protected bool | $booted | Whether the kernel has been booted. |
|
| protected bool | $prepared | Whether essential services have been set up properly by preHandle(). |
|
| protected array | $moduleList | Holds the list of enabled modules. |
|
| protected Extension[] | $moduleData | List of available modules and installation profiles. |
|
| protected ClassLoader | $classLoader | The class loader object. |
|
| protected StorageInterface | $configStorage | Config storage object used for reading enabled modules configuration. |
|
| protected bool | $allowDumping | Whether the container can be dumped. |
|
| protected bool | $containerNeedsRebuild | Whether the container needs to be rebuilt the next time it is initialized. |
|
| protected bool | $containerNeedsDumping | Whether the container needs to be dumped once booting is complete. |
|
| protected array | $serviceYamls | List of discovered services.yml pathnames. |
|
| protected array | $serviceProviderClasses | List of discovered service provider class names or objects. |
|
| protected array | $serviceProviders | List of instantiated service provider classes. |
|
| static protected bool | $isEnvironmentInitialized | Whether the PHP environment has been initialized. |
|
| protected string | $sitePath | The site directory. |
|
| protected string | $root | The app root. |
Methods
Determines if an exception handler should redirect to the installer.
Create a DrupalKernel object from a request.
Constructs a DrupalKernel object.
Determine the application root directory based on this file's location.
Returns the appropriate site directory for a request.
Set the current site path.
Get the site path.
Gets the app root.
Boots the current kernel.
Shuts down the kernel.
Gets the current container.
{@inheritdoc}
Returns the cached container definition - if any.
Helper method that loads legacy Drupal include files.
Helper method that does request related initialization.
Discovers available serviceProviders.
Returns all registered service providers.
{@inheritdoc}
{@inheritdoc}
Returns module data on the filesystem.
Implements Drupal\Core\DrupalKernelInterface::updateModules().
Returns the container cache key based on the environment.
Returns the kernel parameters.
Initializes the service container.
Setup a consistent PHP environment.
Locate site path and initialize settings singleton.
Bootstraps the legacy global request variables.
Returns service instances to persist from an old container to a new one.
Moves persistent service instances into a new container.
Force a container rebuild.
Invalidate the service container for the next request.
Attach synthetic values on to kernel.
Compiles a new service container.
Registers all service providers to the kernel.
Gets a new ContainerBuilder instance used to build the service container.
Stores the container definition in a cache.
Gets a http kernel from the container.
Returns the active configuration storage to use during building the container.
Returns an array of Extension class parameters for all enabled modules.
Gets the file name for each enabled module.
Gets the PSR-4 base directories for module namespaces.
Registers a list of namespaces with PSR-4 directories for class loading.
Validates a hostname length.
Validates the hostname supplied from the HTTP request.
Sets up the lists of trusted HTTP Host headers.
Add service files.
Gets the active install profile.
Details
protected bool
isCli()
Returns whether the current PHP process runs on CLI.
protected bool
shouldRedirectToInstaller(Exception $exception, Connection $connection = NULL)
Determines if an exception handler should redirect to the installer.
static DrupalKernel
createFromRequest(Request $request, $class_loader, string $environment, bool $allow_dumping = TRUE, string $app_root = NULL)
Create a DrupalKernel object from a request.
__construct(string $environment, $class_loader, bool $allow_dumping = TRUE, string $app_root = NULL)
Constructs a DrupalKernel object.
static protected string
guessApplicationRoot()
Determine the application root directory based on this file's location.
static string
findSitePath(Request $request, bool $require_settings = TRUE, string $app_root = NULL)
Returns the appropriate site directory for a request.
Once the kernel has been created DrupalKernelInterface::getSitePath() is preferred since it gets the statically cached result of this method.
Site directories contain all site specific code. This includes settings.php for bootstrap level configuration, file configuration stores, public file storage and site specific modules and themes.
A file named sites.php must be present in the sites directory for multisite. If it doesn't exist, then 'sites/default' will be used.
Finds a matching site directory file by stripping the website's hostname from left to right and pathname from right to left. By default, the directory must contain a 'settings.php' file for it to match. If the parameter $require_settings is set to FALSE, then a directory without a 'settings.php' file will match as well. The first configuration file found will be used and the remaining ones will be ignored. If no configuration file is found, returns a default value 'sites/default'. See default.settings.php for examples on how the URL is converted to a directory.
The sites.php file in the sites directory can define aliases in an
associative array named $sites. The array is written in the format
'
setSitePath(string $path)
Set the current site path.
string
getSitePath()
Get the site path.
string
getAppRoot()
Gets the app root.
$this
boot()
Boots the current kernel.
shutdown()
Shuts down the kernel.
ContainerInterface
getContainer()
Gets the current container.
setContainer(ContainerInterface $container = NULL)
{@inheritdoc}
array|null
getCachedContainerDefinition()
Returns the cached container definition - if any.
This also allows inspecting a built container for debugging purposes.
loadLegacyIncludes()
Helper method that loads legacy Drupal include files.
preHandle(Request $request)
Helper method that does request related initialization.
array
discoverServiceProviders()
Discovers available serviceProviders.
array
getServiceProviders(string $origin)
Returns all registered service providers.
terminate(Request $request, Response $response)
{@inheritdoc}
handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
{@inheritdoc}
protected Response
handleException(Exception $e, Request $request, int $type)
Converts an exception into a response.
protected Extension|bool
moduleData($module)
Returns module data on the filesystem.
updateModules(array $module_list, array $module_filenames = [])
Implements Drupal\Core\DrupalKernelInterface::updateModules().
protected string
getContainerCacheKey()
Returns the container cache key based on the environment.
The 'environment' consists of:
- The kernel environment string.
- The Drupal version constant.
- The deployment identifier from settings.php. This allows custom deployments to force a container rebuild.
- The operating system running PHP. This allows compiler passes to optimize services for different operating systems.
- The paths to any additional container YAMLs from settings.php.
protected array
getKernelParameters()
Returns the kernel parameters.
protected ContainerInterface
initializeContainer()
Initializes the service container.
static
bootEnvironment(string $app_root = NULL)
Setup a consistent PHP environment.
This method sets PHP environment options we want to be sure are set correctly for security or just saneness.
protected
initializeSettings(Request $request)
Locate site path and initialize settings singleton.
protected
initializeRequestGlobals(Request $request)
Bootstraps the legacy global request variables.
protected
getServicesToPersist(ContainerInterface $container)
Returns service instances to persist from an old container to a new one.
protected
persistServices(ContainerInterface $container, array $persist)
Moves persistent service instances into a new container.
ContainerInterface
rebuildContainer()
Force a container rebuild.
invalidateContainer()
Invalidate the service container for the next request.
protected ContainerInterface
attachSynthetic(ContainerInterface $container)
Attach synthetic values on to kernel.
protected ContainerBuilder
compileContainer()
Compiles a new service container.
protected
initializeServiceProviders()
Registers all service providers to the kernel.
protected ContainerBuilder
getContainerBuilder()
Gets a new ContainerBuilder instance used to build the service container.
protected bool
cacheDrupalContainer(array $container_definition)
Stores the container definition in a cache.
protected HttpKernelInterface
getHttpKernel()
Gets a http kernel from the container.
protected StorageInterface
getConfigStorage()
Returns the active configuration storage to use during building the container.
protected array
getModulesParameter()
Returns an array of Extension class parameters for all enabled modules.
protected array
getModuleFileNames()
Gets the file name for each enabled module.
protected string[]
getModuleNamespacesPsr4(string[] $module_file_names)
Gets the PSR-4 base directories for module namespaces.
protected
classLoaderAddMultiplePsr4(array $namespaces = [], object $class_loader = NULL)
Registers a list of namespaces with PSR-4 directories for class loading.
static protected bool
validateHostnameLength(string $host)
Validates a hostname length.
static bool
validateHostname(Request $request)
Validates the hostname supplied from the HTTP request.
static protected bool
setupTrustedHosts(Request $request, array $host_patterns)
Sets up the lists of trusted HTTP Host headers.
Since the HTTP Host header can be set by the user making the request, it is possible to create an attack vectors against a site by overriding this. Symfony provides a mechanism for creating a list of trusted Host values.
Host patterns (as regular expressions) can be configured through settings.php for multisite installations, sites using ServerAlias without canonical redirection, or configurations where the site responds to default requests. For example,
protected
addServiceFiles(array $service_yamls)
Add service files.
protected string|null
getInstallProfile()
Gets the active install profile.