Commit 6f32687e authored by catch's avatar catch

Issue #1831350 by chx, sun: Break the circular dependency between bootstrap container and kernel.

parent 5516b946
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
use Drupal\Core\SystemListingInfo;
use Drupal\Core\Template\Attribute; use Drupal\Core\Template\Attribute;
/** /**
...@@ -5099,140 +5100,16 @@ function drupal_cron_cleanup() { ...@@ -5099,140 +5100,16 @@ function drupal_cron_cleanup() {
} }
/** /**
* Returns information about system object files (modules, themes, etc.). * This function is kept only for backward compatibility.
* *
* This function is used to find all or some system object files (module files, * @see \Drupal\Core\SystemListing::scan().
* theme files, etc.) that exist on the site. It searches in several locations,
* depending on what type of object you are looking for. For instance, if you
* are looking for modules and call:
* @code
* drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.module$/', 'modules');
* @endcode
* this function will search:
* - the core modules directory; i.e., /core/modules
* - the testing profile directory; e.g., /profiles/testing/modules
* - your installation profile's directory; e.g., /profiles/standard/modules
* - the site-wide modules directory; i.e., /modules
* - the all-sites directory; i.e., /sites/all/modules
* - the site-specific directory; i.e., /sites/example.com/modules
* in that order, and return information about all of the files ending in
* .module in those directories.
*
* The information is returned in an associative array, which can be keyed on
* the file name ($key = 'filename'), the file name without the extension ($key
* = 'name'), or the full file stream URI ($key = 'uri'). If you use a key of
* 'filename' or 'name', files found later in the search will take precedence
* over files found earlier (unless they belong to a module or theme not
* compatible with Drupal core); if you choose a key of 'uri', you will get all
* files found.
*
* @param string $mask
* The preg_match() regular expression for the files to find. The expression
* must be anchored and use DRUPAL_PHP_FUNCTION_PATTERN for the file name part
* before the extension, since the results could contain matches that do not
* present valid Drupal extensions otherwise.
* @param string $directory
* The subdirectory name in which the files are found. For example,
* 'modules' will search all 'modules' directories and their sub-directories
* as explained above.
* @param string $key
* (optional) The key to be used for the associative array returned. Possible
* values are:
* - 'uri' for the file's URI.
* - 'filename' for the basename of the file.
* - 'name' for the name of the file without the extension.
* For 'name' and 'filename' only the highest-precedence file is returned.
* Defaults to 'name'.
* @param int $min_depth
* (optional) Minimum depth of directories to return files from, relative to
* each directory searched. For instance, a directory of 'modules' and a
* minimum depth of 2 would find modules inside /modules/node/tests, but not
* modules directly in /modules/node. Defaults to 1. The default is sufficient
* and suitable for all extension types known by Drupal core.
*
* @return array
* An associative array of file objects, keyed on the chosen key. Each element
* in the array is an object containing file information, with properties:
* - 'uri': Full URI of the file.
* - 'filename': File name.
* - 'name': Name of file without the extension.
*/ */
function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1) { function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
$config = conf_path(); // As SystemListing is required to build a dependency injection container
$files = array(); // from scratch and SystemListingInfo only extends SystemLising, this
// class needs to be hardwired.
// Search for the directory in core. $listing = new SystemListingInfo();
$searchdir = array('core/' . $directory); return $listing->scan($mask, $directory, $key, $min_depth);
// The 'core/profiles' directory contains pristine collections of modules and
// themes as provided by a distribution. It is pristine in the same way that
// the 'core/modules' directory is pristine for core; users should avoid
// any modification by using the top-level or sites/<domain> directories.
$profile = drupal_get_profile();
// For SimpleTest to be able to test modules packaged together with a
// distribution we need to include the profile of the parent site (in which
// test runs are triggered).
if (drupal_valid_test_ua()) {
$testing_profile = config('simpletest.settings')->get('parent_profile');
if ($testing_profile && $testing_profile != $profile) {
$searchdir[] = drupal_get_path('profile', $testing_profile) . '/' . $directory;
}
}
// In case both profile directories contain the same extension, the actual
// profile always has precedence.
$searchdir[] = drupal_get_path('profile', $profile) . '/' . $directory;
// Always search for contributed and custom extensions in top-level
// directories as well as sites/all/* directories. If the same extension is
// located in both directories, then the latter wins for legacy/historical
// reasons.
$searchdir[] = $directory;
$searchdir[] = 'sites/all/' . $directory;
if (file_exists("$config/$directory")) {
$searchdir[] = "$config/$directory";
}
// Get current list of items.
if (!function_exists('file_scan_directory')) {
require_once DRUPAL_ROOT . '/core/includes/file.inc';
}
foreach ($searchdir as $dir) {
$files_to_add = file_scan_directory($dir, $mask, array(
'key' => $key,
'min_depth' => $min_depth,
// Do not recurse into ./lib directories; they cannot contain extensions.
// We also skip templates, css, and js directories.
// @todo Find a way to skip ./config directories (but not modules/config).
'nomask' => '/^(CVS|lib|templates|css|js)$/',
));
// Duplicate files found in later search directories take precedence over
// earlier ones, so we want them to overwrite keys in our resulting
// $files array.
// The exception to this is if the later file is from a module or theme not
// compatible with Drupal core. This may occur during upgrades of Drupal
// core when new modules exist in core while older contrib modules with the
// same name exist in a directory such as /modules.
foreach (array_intersect_key($files_to_add, $files) as $file_key => $file) {
// If it has no info file, then we just behave liberally and accept the
// new resource on the list for merging.
if (file_exists($info_file = DRUPAL_ROOT . '/' . dirname($file->uri) . '/' . $file->name . '.info')) {
// Get the .info file for the module or theme this file belongs to.
$info = drupal_parse_info_file($info_file);
// If the module or theme is incompatible with Drupal core, remove it
// from the array for the current search directory, so it is not
// overwritten when merged with the $files array.
if (isset($info['core']) && $info['core'] != DRUPAL_CORE_COMPATIBILITY) {
unset($files_to_add[$file_key]);
}
}
}
$files = array_merge($files, $files_to_add);
}
return $files;
} }
/** /**
......
...@@ -1502,7 +1502,7 @@ function install_bootstrap_full(&$install_state) { ...@@ -1502,7 +1502,7 @@ function install_bootstrap_full(&$install_state) {
module_list_reset(); module_list_reset();
// Instantiate the kernel. // Instantiate the kernel.
$kernel = new DrupalKernel('prod', FALSE, NULL); $kernel = new DrupalKernel('prod', FALSE, drupal_classloader());
$kernel->boot(); $kernel->boot();
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
} }
......
...@@ -507,7 +507,7 @@ function module_enable($module_list, $enable_dependencies = TRUE) { ...@@ -507,7 +507,7 @@ function module_enable($module_list, $enable_dependencies = TRUE) {
// @todo The if statement is here because install_begin_request() creates // @todo The if statement is here because install_begin_request() creates
// a container without a kernel. It probably shouldn't. // a container without a kernel. It probably shouldn't.
if ($kernel = drupal_container()->get('kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE)) { if ($kernel = drupal_container()->get('kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
$kernel->updateModules(module_list()); $kernel->updateModules(module_list(), array($module => drupal_get_path('module', $module)));
} }
// Refresh the schema to include it. // Refresh the schema to include it.
drupal_get_schema(NULL, TRUE); drupal_get_schema(NULL, TRUE);
......
This diff is collapsed.
...@@ -25,6 +25,8 @@ interface DrupalKernelInterface extends KernelInterface { ...@@ -25,6 +25,8 @@ interface DrupalKernelInterface extends KernelInterface {
* *
* @param array $module_list * @param array $module_list
* The new list of modules. * The new list of modules.
* @param array $module_path
* List of module paths, keyed by module name.
*/ */
public function updateModules($module_list); public function updateModules(array $module_list, array $module_path = array());
} }
<?php
/**
* @file
* Definition of Drupal\Core\SystemListing.
*/
namespace Drupal\Core;
/**
* Returns information about system object files (modules, themes, etc.).
*
* This class requires the list of profiles to be scanned (see
* \Drupal\Core\SystemListing::scan) to be passed into the constructor. Also,
* info files are not parsed.
*/
class SystemListing {
/**
* Construct this listing object.
*
* @param array $profiles
* A list of profiles to search their directories for in addition to the
* default directories.
*/
function __construct($profiles = array()) {
$this->profiles = $profiles;
}
/**
* Returns information about system object files (modules, themes, etc.).
*
* This function is used to find all or some system object files (module
* files, theme files, etc.) that exist on the site. It searches in several
* locations, depending on what type of object you are looking for. For
* instance, if you are looking for modules and call:
* @code
* $scanner = new SystemListing();
* $all_profiles = $profiles_scanner->scan('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.profile$/', 'profiles');
* @endcode
* this function will search:
* - the core modules directory; i.e., /core/modules
* - the profiles directories as defined by the profiles() method.
* - the site-wide modules directory; i.e., /modules
* - the all-sites directory; i.e., /sites/all/modules
* - the site-specific directory; i.e., /sites/example.com/modules
* in that order, and return information about all of the files ending in
* .module in those directories.
*
* The information is returned in an associative array, which can be keyed
* on the file name ($key = 'filename'), the file name without the extension
* ($key = 'name'), or the full file stream URI ($key = 'uri'). If you use a
* key of 'filename' or 'name', files found later in the search will take
* precedence over files found earlier (unless they belong to a module or
* theme not compatible with Drupal core); if you choose a key of 'uri',
* you will get all files found.
*
* @param string $mask
* The preg_match() regular expression for the files to find. The
* expression must be anchored and use DRUPAL_PHP_FUNCTION_PATTERN for the
* file name part before the extension, since the results could contain
* matches that do not present valid Drupal extensions otherwise.
* @param string $directory
* The subdirectory name in which the files are found. For example,
* 'modules' will search all 'modules' directories and their
* sub-directories as explained above.
* @param string $key
* (optional) The key to be used for the associative array returned.
* Possible values are:
* - 'uri' for the file's URI.
* - 'filename' for the basename of the file.
* - 'name' for the name of the file without the extension.
* For 'name' and 'filename' only the highest-precedence file is returned.
* Defaults to 'name'.
*
* @return array
* An associative array of file objects, keyed on the chosen key. Each
* element in the array is an object containing file information, with
* properties:
* - 'uri': Full URI of the file.
* - 'filename': File name.
* - 'name': Name of file without the extension.
*/
function scan($mask, $directory, $key = 'name') {
if (!in_array($key, array('uri', 'filename', 'name'))) {
$key = 'uri';
}
$config = conf_path();
$files = array();
// Search for the directory in core.
$searchdir = array('core/' . $directory);
foreach ($this->profiles($directory) as $profile) {
$searchdir[] = $profile;
}
// Always search for contributed and custom extensions in top-level
// directories as well as sites/all/* directories. If the same extension is
// located in both directories, then the latter wins for legacy/historical
// reasons.
$searchdir[] = $directory;
$searchdir[] = 'sites/all/' . $directory;
if (file_exists("$config/$directory")) {
$searchdir[] = "$config/$directory";
}
// @todo Find a way to skip ./config directories (but not modules/config).
$nomask = '/^(CVS|lib|templates|css|js)$/';
$files = array();
// Get current list of items.
foreach ($searchdir as $dir) {
$files = array_merge($files, $this->process($files, $this->scanDirectory($dir, $key, $mask, $nomask)));
}
return $files;
}
/**
* List the profiles for this directory.
*
* This version only returns those passed to the constructor.
*
* @param string $directory
* The current search directory like 'modules' or 'themes'.
*
* @return array
* A list of profiles.
*/
protected function profiles($directory) {
return $this->profiles;
}
/**
* Process the files to add before adding them.
*
* @param array $files
* Every file found so far.
* @param array $files_to_add
* The files found in a single directory.
*
* @return array
* The processed list of file objects. For example, the SystemListingInfo
* class removes files not compatible with the current core version.
*/
protected function process(array $files, array $files_to_add) {
return $files_to_add;
}
/**
* Abbreviated version of file_scan_directory().
*
* @param $dir
* The base directory or URI to scan, without trailing slash.
* @param $key
* The key to be used for the returned associative array of files.
* Possible values are 'uri', for the file's URI; 'filename', for the
* basename of the file; and 'name' for the name of the file without the
* extension.
* @param $mask
* The preg_match() regular expression of the files to find.
* @param $nomask
* The preg_match() regular expression of the files to ignore.
*
* @return array
* An associative array (keyed on the chosen key) of objects with 'uri',
* 'filename', and 'name' members corresponding to the matching files.
*/
protected function scanDirectory($dir, $key, $mask, $nomask) {
$files = array();
if (is_dir($dir)) {
// Avoid warnings when opendir does not have the permissions to open a
// directory.
if ($handle = @opendir($dir)) {
while (FALSE !== ($filename = readdir($handle))) {
// Skip this file if it matches the nomask or starts with a dot.
if ($filename[0] != '.' && !preg_match($nomask, $filename)) {
$uri = "$dir/$filename";
if (is_dir($uri)) {
// Give priority to files in this folder by merging them in after
// any subdirectory files.
$files = array_merge($this->scanDirectory($uri, $key, $mask, $nomask), $files);
}
elseif (preg_match($mask, $filename)) {
// Always use this match over anything already set in $files with
// the same $options['key'].
$file = new \stdClass();
$file->uri = $uri;
$file->filename = $filename;
$file->name = pathinfo($filename, PATHINFO_FILENAME);
$files[$file->$key] = $file;
}
}
}
closedir($handle);
}
}
return $files;
}
}
<?php
/**
* @file
* Definition of Drupal\Core\SystemListingInfo.
*/
namespace Drupal\Core;
/**
* Returns information about system object files (modules, themes, etc.).
*
* This class finds the profile directories itself and also parses info files.
*/
class SystemListingInfo extends SystemListing {
/**
* Overrides Drupal\Core\SystemListing::profiles().
*/
protected function profiles($directory) {
$searchdir = array();
// The 'core/profiles' directory contains pristine collections of modules
// and themes as provided by a distribution. It is pristine in the same
// way that the 'core/modules' directory is pristine for core; users
// should avoid any modification by using the top-level or sites/<domain>
// directories.
$profile = drupal_get_profile();
// For SimpleTest to be able to test modules packaged together with a
// distribution we need to include the profile of the parent site (in
// which test runs are triggered).
if (drupal_valid_test_ua()) {
$testing_profile = config('simpletest.settings')->get('parent_profile');
if ($testing_profile && $testing_profile != $profile) {
$searchdir[] = drupal_get_path('profile', $testing_profile) . '/' . $directory;
}
}
// In case both profile directories contain the same extension, the actual
// profile always has precedence.
$searchdir[] = drupal_get_path('profile', $profile) . '/' . $directory;
return $searchdir;
}
/**
* Overrides Drupal\Core\SystemListing::process().
*/
protected function process(array $files, array $files_to_add) {
// Duplicate files found in later search directories take precedence over
// earlier ones, so we want them to overwrite keys in our resulting
// $files array.
// The exception to this is if the later file is from a module or theme not
// compatible with Drupal core. This may occur during upgrades of Drupal
// core when new modules exist in core while older contrib modules with the
// same name exist in a directory such as /modules.
foreach (array_intersect_key($files_to_add, $files) as $file_key => $file) {
// If it has no info file, then we just behave liberally and accept the
// new resource on the list for merging.
if (file_exists($info_file = dirname($file->uri) . '/' . $file->name . '.info')) {
// Get the .info file for the module or theme this file belongs to.
$info = drupal_parse_info_file($info_file);
// If the module or theme is incompatible with Drupal core, remove it
// from the array for the current search directory, so it is not
// overwritten when merged with the $files array.
if (isset($info['core']) && $info['core'] != DRUPAL_CORE_COMPATIBILITY) {
unset($files_to_add[$file_key]);
}
}
}
return $files_to_add;
}
}
...@@ -912,7 +912,7 @@ protected function rebuildContainer() { ...@@ -912,7 +912,7 @@ protected function rebuildContainer() {
// container in drupal_container(). Drupal\simpletest\TestBase::tearDown() // container in drupal_container(). Drupal\simpletest\TestBase::tearDown()
// restores the original container. // restores the original container.
// @see Drupal\Core\DrupalKernel::initializeContainer() // @see Drupal\Core\DrupalKernel::initializeContainer()
$this->kernel = new DrupalKernel('testing', FALSE, NULL); $this->kernel = new DrupalKernel('testing', FALSE, drupal_classloader());
// Booting the kernel is necessary to initialize the new DIC. While // Booting the kernel is necessary to initialize the new DIC. While
// normally the kernel gets booted on demand in // normally the kernel gets booted on demand in
// Symfony\Component\HttpKernel\handle(), this kernel needs manual booting // Symfony\Component\HttpKernel\handle(), this kernel needs manual booting
......
...@@ -7,8 +7,9 @@ ...@@ -7,8 +7,9 @@
namespace Drupal\system\Tests\DrupalKernel; namespace Drupal\system\Tests\DrupalKernel;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\DrupalKernel; use Drupal\Core\DrupalKernel;
use Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage;
use Drupal\Component\PhpStorage\FileReadOnlyStorage;
use Drupal\simpletest\UnitTestBase; use Drupal\simpletest\UnitTestBase;
use ReflectionClass; use ReflectionClass;
...@@ -34,21 +35,25 @@ function testCompileDIC() { ...@@ -34,21 +35,25 @@ function testCompileDIC() {
// We need to be able to restore it to the correct one at the end of this // We need to be able to restore it to the correct one at the end of this
// test. // test.
$original_container = drupal_container(); $original_container = drupal_container();
global $conf; $classloader = drupal_classloader();
$conf['php_storage']['service_container'] = array( $configuration = array(
'class' => 'Drupal\Component\PhpStorage\MTimeProtectedFileStorage', 'bin' => 'service_container',
'directory' => DRUPAL_ROOT . '/' . variable_get('file_public_path', conf_path() . '/files') . '/php',
'secret' => $GLOBALS['drupal_hash_salt'], 'secret' => $GLOBALS['drupal_hash_salt'],
); );
$cache = new MemoryBackend('test'); // @todo: write a memory based storage backend for testing.
$php_storage = new MTimeProtectedFastFileStorage($configuration);
$module_enabled = array( $module_enabled = array(
'system' => 'system', 'system' => 'system',
'user' => 'user', 'user' => 'user',
); );
$kernel = new DrupalKernel('testing', FALSE, $module_enabled, $cache); $kernel = new DrupalKernel('testing', FALSE, $classloader, $php_storage);
$kernel->updateModules($module_enabled);
$kernel->boot(); $kernel->boot();
// Instantiate it a second time and we should get the compiled Container // Instantiate it a second time and we should get the compiled Container
// class. // class.
$kernel = new DrupalKernel('testing', FALSE, $module_enabled, $cache); $kernel = new DrupalKernel('testing', FALSE, $classloader, $php_storage);
$kernel->updateModules($module_enabled);
$kernel->boot(); $kernel->boot();
$container = $kernel->getContainer(); $container = $kernel->getContainer();
$refClass = new ReflectionClass($container); $refClass = new ReflectionClass($container);
...@@ -62,11 +67,9 @@ function testCompileDIC() { ...@@ -62,11 +67,9 @@ function testCompileDIC() {
// Now use the read-only storage implementation, simulating a "production" // Now use the read-only storage implementation, simulating a "production"
// environment. // environment.
drupal_static_reset('drupal_php_storage'); $php_storage = new FileReadOnlyStorage($configuration);
$conf['php_storage']['service_container'] = array( $kernel = new DrupalKernel('testing', FALSE, $classloader, $php_storage);
'class' => 'Drupal\Component\PhpStorage\FileReadOnlyStorage', $kernel->updateModules($module_enabled);
);
$kernel = new DrupalKernel('testing', FALSE, $module_enabled, $cache);
$kernel->boot(); $kernel->boot();
$container = $kernel->getContainer(); $container = $kernel->getContainer();
$refClass = new ReflectionClass($container); $refClass = new ReflectionClass($container);
...@@ -86,17 +89,14 @@ function testCompileDIC() { ...@@ -86,17 +89,14 @@ function testCompileDIC() {
// Add another module so that we can test that the new module's bundle is // Add another module so that we can test that the new module's bundle is
// registered to the new container. // registered to the new container.
$module_enabled = array( $module_enabled['bundle_test'] = 'bundle_test';
'system' => 'system', $kernel = new DrupalKernel('testing', FALSE, $classloader, $php_storage);
'user' => 'user', $kernel->updateModules($module_enabled);
'bundle_test' => 'bundle_test',
);
$cache->flush();
$kernel = new DrupalKernel('testing', FALSE, $module_enabled, $cache);
$kernel->boot(); $kernel->boot();
// Instantiate it a second time and we should still get a ContainerBuilder // Instantiate it a second time and we should still get a ContainerBuilder
// class because we are using the read-only PHP storage. // class because we are using the read-only PHP storage.
$kernel = new DrupalKernel('testing', FALSE, $module_enabled, $cache); $kernel = new DrupalKernel('testing', FALSE, $classloader, $php_storage);
$kernel->updateModules($module_enabled);
$kernel->boot(); $kernel->boot();
$container = $kernel->getContainer(); $container = $kernel->getContainer();
$refClass = new ReflectionClass($container); $refClass = new ReflectionClass($container);
......
...@@ -36,6 +36,6 @@ ...@@ -36,6 +36,6 @@
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
$kernel = new DrupalKernel('prod', FALSE); $kernel = new DrupalKernel('prod', FALSE, drupal_classloader());
$response = $kernel->handle($request)->prepare($request)->send(); $response = $kernel->handle($request)->prepare($request)->send();
$kernel->terminate($request, $response); $kernel->terminate($request, $response);
...@@ -35,6 +35,6 @@ ...@@ -35,6 +35,6 @@
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
$kernel = new DrupalKernel('prod', FALSE); $kernel = new DrupalKernel('prod', FALSE, drupal_classloader());
$response = $kernel->handle($request)->prepare($request)->send(); $response = $kernel->handle($request)->prepare($request)->send();
$kernel->terminate($request, $response); $kernel->terminate($request, $response);
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE); drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
// @todo Figure out how best to handle the Kernel constructor parameters. // @todo Figure out how best to handle the Kernel constructor parameters.
$kernel = new DrupalKernel('prod', FALSE, NULL, cache('bootstrap')); $kernel = new DrupalKernel('prod', FALSE, drupal_classloader(), drupal_php_storage('service_container'));
// Create a request object from the HTTPFoundation. // Create a request object from the HTTPFoundation.
$request = Request::createFromGlobals(); $request = Request::createFromGlobals();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment