Commit 91c38c8f authored by alexpott's avatar alexpott

Issue #2328111 by dawehner, martin107, neclimdul: Replace most instances of...

Issue #2328111 by dawehner, martin107, neclimdul: Replace most instances of the DRUPAL_ROOT constant with the app.root container parameter.
parent c18ba6aa
...@@ -42,6 +42,7 @@ services: ...@@ -42,6 +42,7 @@ services:
arguments: ['@database'] arguments: ['@database']
cache.backend.apcu: cache.backend.apcu:
class: Drupal\Core\Cache\ApcuBackendFactory class: Drupal\Core\Cache\ApcuBackendFactory
arguments: ['@app.root']
cache.backend.php: cache.backend.php:
class: Drupal\Core\Cache\PhpBackendFactory class: Drupal\Core\Cache\PhpBackendFactory
cache.bootstrap: cache.bootstrap:
...@@ -182,7 +183,7 @@ services: ...@@ -182,7 +183,7 @@ services:
arguments: ['@request_stack', '@url_generator'] arguments: ['@request_stack', '@url_generator']
form_cache: form_cache:
class: Drupal\Core\Form\FormCache class: Drupal\Core\Form\FormCache
arguments: ['@keyvalue.expirable', '@module_handler', '@current_user', '@csrf_token', '@logger.channel.form', '@config.factory', '@request_stack', '@page_cache_request_policy'] arguments: ['@app.root', '@keyvalue.expirable', '@module_handler', '@current_user', '@csrf_token', '@logger.channel.form', '@config.factory', '@request_stack', '@page_cache_request_policy']
public: false # Private to form_builder public: false # Private to form_builder
keyvalue: keyvalue:
class: Drupal\Core\KeyValueStore\KeyValueFactory class: Drupal\Core\KeyValueStore\KeyValueFactory
...@@ -287,10 +288,10 @@ services: ...@@ -287,10 +288,10 @@ services:
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler'] arguments: ['@container.namespaces', '@cache.discovery', '@module_handler']
module_handler: module_handler:
class: Drupal\Core\Extension\ModuleHandler class: Drupal\Core\Extension\ModuleHandler
arguments: ['%container.modules%', '@kernel', '@cache.bootstrap'] arguments: ['@app.root', '%container.modules%', '@kernel', '@cache.bootstrap']
theme_handler: theme_handler:
class: Drupal\Core\Extension\ThemeHandler class: Drupal\Core\Extension\ThemeHandler
arguments: ['@config.factory', '@module_handler', '@state', '@info_parser', '@logger.channel.default', '@asset.css.collection_optimizer', '@config.installer', '@config.manager', '@router.builder_indicator'] arguments: ['@app.root', '@config.factory', '@module_handler', '@state', '@info_parser', '@logger.channel.default', '@asset.css.collection_optimizer', '@config.installer', '@config.manager', '@router.builder_indicator']
entity.manager: entity.manager:
class: Drupal\Core\Entity\EntityManager class: Drupal\Core\Entity\EntityManager
arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@language_manager', '@string_translation', '@class_resolver', '@typed_data_manager', '@entity.definitions.installed', '@event_dispatcher'] arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@language_manager', '@string_translation', '@class_resolver', '@typed_data_manager', '@entity.definitions.installed', '@event_dispatcher']
...@@ -382,6 +383,13 @@ services: ...@@ -382,6 +383,13 @@ services:
event_dispatcher: event_dispatcher:
class: Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher class: Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
arguments: ['@service_container'] arguments: ['@service_container']
app.root:
class: SplString
factory_service: 'app.root.factory'
factory_method: 'get'
app.root.factory:
class: Drupal\Core\AppRootFactory
arguments: ['@kernel']
controller_resolver: controller_resolver:
class: Drupal\Core\Controller\ControllerResolver class: Drupal\Core\Controller\ControllerResolver
arguments: ['@class_resolver', '@logger.channel.default'] arguments: ['@class_resolver', '@logger.channel.default']
...@@ -989,10 +997,10 @@ services: ...@@ -989,10 +997,10 @@ services:
arguments: ['@theme.registry', '@theme.negotiator', '@theme.initialization', '@request_stack'] arguments: ['@theme.registry', '@theme.negotiator', '@theme.initialization', '@request_stack']
theme.initialization: theme.initialization:
class: Drupal\Core\Theme\ThemeInitialization class: Drupal\Core\Theme\ThemeInitialization
arguments: ['@theme_handler', '@state'] arguments: ['@app.root', '@theme_handler', '@state']
theme.registry: theme.registry:
class: Drupal\Core\Theme\Registry class: Drupal\Core\Theme\Registry
arguments: ['@cache.default', '@lock', '@module_handler'] arguments: ['@app.root', '@cache.default', '@lock', '@module_handler']
tags: tags:
- { name: needs_destruction } - { name: needs_destruction }
authentication: authentication:
...@@ -1049,12 +1057,12 @@ services: ...@@ -1049,12 +1057,12 @@ services:
- { name: needs_destruction } - { name: needs_destruction }
library.discovery.parser: library.discovery.parser:
class: Drupal\Core\Asset\LibraryDiscoveryParser class: Drupal\Core\Asset\LibraryDiscoveryParser
arguments: ['@module_handler'] arguments: ['@app.root', '@module_handler']
info_parser: info_parser:
class: Drupal\Core\Extension\InfoParser class: Drupal\Core\Extension\InfoParser
twig: twig:
class: Drupal\Core\Template\TwigEnvironment class: Drupal\Core\Template\TwigEnvironment
arguments: ['@twig.loader', '@module_handler', '@theme_handler', '%twig.config%'] arguments: ['@app.root', '@twig.loader', '@module_handler', '@theme_handler', '%twig.config%']
tags: tags:
- { name: service_collector, tag: 'twig.extension', call: addExtension } - { name: service_collector, tag: 'twig.extension', call: addExtension }
twig.extension: twig.extension:
...@@ -1074,7 +1082,7 @@ services: ...@@ -1074,7 +1082,7 @@ services:
alias: twig.loader.filesystem alias: twig.loader.filesystem
twig.loader.filesystem: twig.loader.filesystem:
class: Twig_Loader_Filesystem class: Twig_Loader_Filesystem
arguments: ['%app.root%'] arguments: ['@app.root']
element_info: element_info:
alias: plugin.manager.element_info alias: plugin.manager.element_info
file.mime_type.guesser: file.mime_type.guesser:
......
...@@ -235,7 +235,7 @@ function _batch_process() { ...@@ -235,7 +235,7 @@ function _batch_process() {
// request, we check if it requires an additional file for functions // request, we check if it requires an additional file for functions
// definitions. // definitions.
if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) { if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
include_once DRUPAL_ROOT . '/' . $current_set['file']; include_once \Drupal::root() . '/' . $current_set['file'];
} }
$task_message = $label = ''; $task_message = $label = '';
...@@ -408,7 +408,7 @@ function _batch_finished() { ...@@ -408,7 +408,7 @@ function _batch_finished() {
if (isset($batch_set['finished'])) { if (isset($batch_set['finished'])) {
// Check if the set requires an additional file for function definitions. // Check if the set requires an additional file for function definitions.
if (isset($batch_set['file']) && is_file($batch_set['file'])) { if (isset($batch_set['file']) && is_file($batch_set['file'])) {
include_once DRUPAL_ROOT . '/' . $batch_set['file']; include_once \Drupal::root() . '/' . $batch_set['file'];
} }
if (is_callable($batch_set['finished'])) { if (is_callable($batch_set['finished'])) {
$queue = _batch_queue($batch_set); $queue = _batch_queue($batch_set);
......
...@@ -277,7 +277,7 @@ function drupal_get_filename($type, $name, $filename = NULL) { ...@@ -277,7 +277,7 @@ function drupal_get_filename($type, $name, $filename = NULL) {
} }
// If still unknown, perform a filesystem scan. // If still unknown, perform a filesystem scan.
if (!isset($files[$type][$name])) { if (!isset($files[$type][$name])) {
$listing = new ExtensionDiscovery(); $listing = new ExtensionDiscovery(DRUPAL_ROOT);
// Prevent an infinite recursion by this legacy function. // Prevent an infinite recursion by this legacy function.
if ($original_type == 'profile') { if ($original_type == 'profile') {
$listing->setProfileDirectories(array()); $listing->setProfileDirectories(array());
...@@ -1190,7 +1190,7 @@ function _current_path($path = NULL) { ...@@ -1190,7 +1190,7 @@ function _current_path($path = NULL) {
*/ */
function drupal_classloader_register($name, $path) { function drupal_classloader_register($name, $path) {
$loader = \Drupal::service('class_loader'); $loader = \Drupal::service('class_loader');
$loader->addPsr4('Drupal\\' . $name . '\\', DRUPAL_ROOT . '/' . $path . '/src'); $loader->addPsr4('Drupal\\' . $name . '\\', \Drupal::root() . '/' . $path . '/src');
} }
/** /**
......
...@@ -199,8 +199,8 @@ function _drupal_log_error($error, $fatal = FALSE) { ...@@ -199,8 +199,8 @@ function _drupal_log_error($error, $fatal = FALSE) {
// Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path // Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path
// in the message. This does not happen for (false) security. // in the message. This does not happen for (false) security.
$root_length = strlen(DRUPAL_ROOT); $root_length = strlen(\Drupal::root());
if (substr($error['%file'], 0, $root_length) == DRUPAL_ROOT) { if (substr($error['%file'], 0, $root_length) == \Drupal::root()) {
$error['%file'] = substr($error['%file'], $root_length + 1); $error['%file'] = substr($error['%file'], $root_length + 1);
} }
// Should not translate the string to avoid errors producing more errors. // Should not translate the string to avoid errors producing more errors.
......
...@@ -287,7 +287,7 @@ function install_begin_request(&$install_state) { ...@@ -287,7 +287,7 @@ function install_begin_request(&$install_state) {
$site_path = DrupalKernel::findSitePath($request, FALSE); $site_path = DrupalKernel::findSitePath($request, FALSE);
$class_loader = require __DIR__ . '/../vendor/autoload.php'; $class_loader = require __DIR__ . '/../vendor/autoload.php';
Settings::initialize($site_path, $class_loader); Settings::initialize(dirname(dirname(__DIR__)), $site_path, $class_loader);
// Ensure that procedural dependencies are loaded as early as possible, // Ensure that procedural dependencies are loaded as early as possible,
// since the error/exception handlers depend on them. // since the error/exception handlers depend on them.
...@@ -389,7 +389,7 @@ function install_begin_request(&$install_state) { ...@@ -389,7 +389,7 @@ function install_begin_request(&$install_state) {
} }
// Add list of all available profiles to the installation state. // Add list of all available profiles to the installation state.
$listing = new ExtensionDiscovery(); $listing = new ExtensionDiscovery($container->get('app.root'));
$listing->setProfileDirectories(array()); $listing->setProfileDirectories(array());
$install_state['profiles'] += $listing->scan('profile'); $install_state['profiles'] += $listing->scan('profile');
...@@ -717,7 +717,7 @@ function install_tasks($install_state) { ...@@ -717,7 +717,7 @@ function install_tasks($install_state) {
$profile = $install_state['parameters']['profile']; $profile = $install_state['parameters']['profile'];
$profile_install_file = $install_state['profiles'][$profile]->getPath() . '/' . $profile . '.install'; $profile_install_file = $install_state['profiles'][$profile]->getPath() . '/' . $profile . '.install';
if (file_exists($profile_install_file)) { if (file_exists($profile_install_file)) {
include_once DRUPAL_ROOT . '/' . $profile_install_file; include_once \Drupal::root() . '/' . $profile_install_file;
} }
$function = $install_state['parameters']['profile'] . '_install_tasks'; $function = $install_state['parameters']['profile'] . '_install_tasks';
if (function_exists($function)) { if (function_exists($function)) {
......
...@@ -593,7 +593,7 @@ function drupal_verify_profile($install_state) { ...@@ -593,7 +593,7 @@ function drupal_verify_profile($install_state) {
$info = $install_state['profile_info']; $info = $install_state['profile_info'];
// Get the list of available modules for the selected installation profile. // Get the list of available modules for the selected installation profile.
$listing = new ExtensionDiscovery(); $listing = new ExtensionDiscovery(DRUPAL_ROOT);
$present_modules = array(); $present_modules = array();
foreach ($listing->scan('module') as $present_module) { foreach ($listing->scan('module') as $present_module) {
$present_modules[] = $present_module->getName(); $present_modules[] = $present_module->getName();
......
...@@ -179,7 +179,7 @@ function module_uninstall($module_list = array(), $uninstall_dependents = TRUE) ...@@ -179,7 +179,7 @@ function module_uninstall($module_list = array(), $uninstall_dependents = TRUE)
* Returns an array of modules required by core. * Returns an array of modules required by core.
*/ */
function drupal_required_modules() { function drupal_required_modules() {
$listing = new ExtensionDiscovery(); $listing = new ExtensionDiscovery(\Drupal::root());
$files = $listing->scan('module'); $files = $listing->scan('module');
$required = array(); $required = array();
......
...@@ -341,7 +341,7 @@ function _theme($hook, $variables = array()) { ...@@ -341,7 +341,7 @@ function _theme($hook, $variables = array()) {
// elsewhere. // elsewhere.
if (!empty($info['includes'])) { if (!empty($info['includes'])) {
foreach ($info['includes'] as $include_file) { foreach ($info['includes'] as $include_file) {
include_once DRUPAL_ROOT . '/' . $include_file; include_once \Drupal::root() . '/' . $include_file;
} }
} }
...@@ -353,7 +353,7 @@ function _theme($hook, $variables = array()) { ...@@ -353,7 +353,7 @@ function _theme($hook, $variables = array()) {
// might reside there. // might reside there.
if (!empty($base_hook_info['includes'])) { if (!empty($base_hook_info['includes'])) {
foreach ($base_hook_info['includes'] as $include_file) { foreach ($base_hook_info['includes'] as $include_file) {
include_once DRUPAL_ROOT . '/' . $include_file; include_once \Drupal::root() . '/' . $include_file;
} }
} }
// Replace the preprocess functions with those from the base hook. // Replace the preprocess functions with those from the base hook.
......
...@@ -152,6 +152,15 @@ public static function hasService($id) { ...@@ -152,6 +152,15 @@ public static function hasService($id) {
return static::$container && static::$container->has($id); return static::$container && static::$container->has($id);
} }
/**
* Gets the app root.
*
* @return string
*/
public static function root() {
return static::$container->get('app.root');
}
/** /**
* Indicates if there is a currently active request object. * Indicates if there is a currently active request object.
* *
......
<?php
/**
* @file
* Contains \Drupal\Core\AppRootFactory.
*/
namespace Drupal\Core;
/**
* Gets the app root from the kernel.
*/
class AppRootFactory {
/**
* The Drupal kernel.
*
* @var \Drupal\Core\DrupalKernelInterface
*/
protected $drupalKernel;
/**
* Constructs an AppRootFactory instance.
*
* @param \Drupal\Core\DrupalKernelInterface $drupal_kernel
* The Drupal kernel.
*/
public function __construct(DrupalKernelInterface $drupal_kernel) {
$this->drupalKernel = $drupal_kernel;
}
/**
* Gets the app root.
*
* @return string
*/
public function get() {
return $this->drupalKernel->getAppRoot();
}
}
...@@ -27,10 +27,22 @@ class LibraryDiscoveryParser { ...@@ -27,10 +27,22 @@ class LibraryDiscoveryParser {
protected $moduleHandler; protected $moduleHandler;
/** /**
* The app root.
*
* @var string
*/
protected $root;
/**
* Constructs a new LibraryDiscoveryParser instance.
*
* @param string $root
* The app root.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler. * The module handler.
*/ */
public function __construct(ModuleHandlerInterface $module_handler) { public function __construct($root, ModuleHandlerInterface $module_handler) {
$this->root = $root;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
} }
...@@ -67,7 +79,7 @@ public function buildByExtension($extension) { ...@@ -67,7 +79,7 @@ public function buildByExtension($extension) {
$library_file = $path . '/' . $extension . '.libraries.yml'; $library_file = $path . '/' . $extension . '.libraries.yml';
if ($library_file && file_exists(DRUPAL_ROOT . '/' . $library_file)) { if ($library_file && file_exists($this->root . '/' . $library_file)) {
$libraries = $this->parseLibraryInfo($extension, $library_file); $libraries = $this->parseLibraryInfo($extension, $library_file);
} }
...@@ -222,7 +234,7 @@ public function buildByExtension($extension) { ...@@ -222,7 +234,7 @@ public function buildByExtension($extension) {
*/ */
protected function parseLibraryInfo($extension, $library_file) { protected function parseLibraryInfo($extension, $library_file) {
try { try {
$libraries = Yaml::decode(file_get_contents(DRUPAL_ROOT . '/' . $library_file)); $libraries = Yaml::decode(file_get_contents($this->root . '/' . $library_file));
} }
catch (InvalidDataTypeException $e) { catch (InvalidDataTypeException $e) {
// Rethrow a more helpful exception to provide context. // Rethrow a more helpful exception to provide context.
......
...@@ -20,9 +20,12 @@ class ApcuBackendFactory implements CacheFactoryInterface { ...@@ -20,9 +20,12 @@ class ApcuBackendFactory implements CacheFactoryInterface {
/** /**
* Constructs an ApcuBackendFactory object. * Constructs an ApcuBackendFactory object.
*
* @param string $root
* The app root.
*/ */
public function __construct() { public function __construct($root) {
$this->sitePrefix = Crypt::hashBase64(DRUPAL_ROOT . '/' . conf_path()); $this->sitePrefix = Crypt::hashBase64($root . '/' . conf_path());
} }
/** /**
......
...@@ -157,7 +157,7 @@ protected function getAllFolders() { ...@@ -157,7 +157,7 @@ protected function getAllFolders() {
if ($profile = drupal_get_profile()) { if ($profile = drupal_get_profile()) {
$this->folders += $this->getComponentNames('profile', array($profile)); $this->folders += $this->getComponentNames('profile', array($profile));
} }
$listing = new ExtensionDiscovery(); $listing = new ExtensionDiscovery(DRUPAL_ROOT);
$this->folders += $this->getComponentNames('module', array_keys($listing->scan('module'))); $this->folders += $this->getComponentNames('module', array_keys($listing->scan('module')));
$this->folders += $this->getComponentNames('theme', array_keys($listing->scan('theme'))); $this->folders += $this->getComponentNames('theme', array_keys($listing->scan('theme')));
} }
......
...@@ -41,8 +41,6 @@ class CoreServiceProvider implements ServiceProviderInterface { ...@@ -41,8 +41,6 @@ class CoreServiceProvider implements ServiceProviderInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function register(ContainerBuilder $container) { public function register(ContainerBuilder $container) {
$container->setParameter('app.root', DRUPAL_ROOT);
$this->registerUuid($container); $this->registerUuid($container);
$this->registerTest($container); $this->registerTest($container);
......
...@@ -179,6 +179,13 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { ...@@ -179,6 +179,13 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
*/ */
protected $sitePath; protected $sitePath;
/**
* The app root.
*
* @var string
*/
protected $root;
/** /**
* Create a DrupalKernel object from a request. * Create a DrupalKernel object from a request.
* *
...@@ -198,7 +205,8 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { ...@@ -198,7 +205,8 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
*/ */
public static function createFromRequest(Request $request, $class_loader, $environment, $allow_dumping = TRUE) { public static function createFromRequest(Request $request, $class_loader, $environment, $allow_dumping = TRUE) {
// Include our bootstrap file. // Include our bootstrap file.
require_once dirname(dirname(dirname(__DIR__))) . '/includes/bootstrap.inc'; $core_root = dirname(dirname(dirname(__DIR__)));
require_once $core_root . '/includes/bootstrap.inc';
$kernel = new static($environment, $class_loader, $allow_dumping); $kernel = new static($environment, $class_loader, $allow_dumping);
...@@ -206,7 +214,9 @@ public static function createFromRequest(Request $request, $class_loader, $envir ...@@ -206,7 +214,9 @@ public static function createFromRequest(Request $request, $class_loader, $envir
static::bootEnvironment(); static::bootEnvironment();
// Get our most basic settings setup. // Get our most basic settings setup.
$kernel->initializeSettings($request); $site_path = static::findSitePath($request);
$kernel->setSitePath($site_path);
Settings::initialize(dirname($core_root), $site_path, $class_loader);
// Redirect the user to the installation script if Drupal has not been // Redirect the user to the installation script if Drupal has not been
// installed yet (i.e., if no $databases array has been defined in the // installed yet (i.e., if no $databases array has been defined in the
...@@ -219,18 +229,6 @@ public static function createFromRequest(Request $request, $class_loader, $envir ...@@ -219,18 +229,6 @@ public static function createFromRequest(Request $request, $class_loader, $envir
return $kernel; return $kernel;
} }
/**
* Initializes the kernel's site path and the Settings singleton.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request that will be used to determine the site path.
*/
protected function initializeSettings(Request $request) {
$site_path = static::findSitePath($request);
$this->setSitePath($site_path);
Settings::initialize($site_path, $this->classLoader);
}
/** /**
* Constructs a DrupalKernel object. * Constructs a DrupalKernel object.
* *
...@@ -248,6 +246,7 @@ public function __construct($environment, $class_loader, $allow_dumping = TRUE) ...@@ -248,6 +246,7 @@ public function __construct($environment, $class_loader, $allow_dumping = TRUE)
$this->environment = $environment; $this->environment = $environment;
$this->classLoader = $class_loader; $this->classLoader = $class_loader;
$this->allowDumping = $allow_dumping; $this->allowDumping = $allow_dumping;
$this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
} }
/** /**
...@@ -349,6 +348,13 @@ public function getSitePath() { ...@@ -349,6 +348,13 @@ public function getSitePath() {
return $this->sitePath; return $this->sitePath;
} }
/**
* {@inheritdoc}
*/
public function getAppRoot() {
return $this->root;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -361,21 +367,21 @@ public function boot() { ...@@ -361,21 +367,21 @@ public function boot() {
Timer::start('page'); Timer::start('page');
// Load legacy and other functional code. // Load legacy and other functional code.
require_once DRUPAL_ROOT . '/core/includes/common.inc'; require_once $this->root . '/core/includes/common.inc';
require_once DRUPAL_ROOT . '/core/includes/database.inc'; require_once $this->root . '/core/includes/database.inc';
require_once DRUPAL_ROOT . '/core/includes/path.inc'; require_once $this->root . '/core/includes/path.inc';
require_once DRUPAL_ROOT . '/core/includes/module.inc'; require_once $this->root . '/core/includes/module.inc';
require_once DRUPAL_ROOT . '/core/includes/theme.inc'; require_once $this->root . '/core/includes/theme.inc';
require_once DRUPAL_ROOT . '/core/includes/pager.inc'; require_once $this->root . '/core/includes/pager.inc';
require_once DRUPAL_ROOT . '/core/includes/menu.inc'; require_once $this->root . '/core/includes/menu.inc';
require_once DRUPAL_ROOT . '/core/includes/tablesort.inc'; require_once $this->root . '/core/includes/tablesort.inc';
require_once DRUPAL_ROOT . '/core/includes/file.inc'; require_once $this->root . '/core/includes/file.inc';
require_once DRUPAL_ROOT . '/core/includes/unicode.inc'; require_once $this->root . '/core/includes/unicode.inc';
require_once DRUPAL_ROOT . '/core/includes/form.inc';