diff --git a/core/core.services.yml b/core/core.services.yml index a502711..91deb95 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -416,7 +416,7 @@ services: lazy: true module_listing: class: Drupal\Core\Extension\ModuleExtensionList - arguments: ['@app.root', 'module', '@state', '@info_parser', '@module_handler'] + arguments: ['@app.root', 'module', '@cache.default', '@info_parser', '@module_handler'] content_uninstall_validator: class: Drupal\Core\Entity\ContentUninstallValidator tags: diff --git a/core/lib/Drupal/Core/Extension/ExtensionList.php b/core/lib/Drupal/Core/Extension/ExtensionList.php index 2e5aeae..d36f384 100644 --- a/core/lib/Drupal/Core/Extension/ExtensionList.php +++ b/core/lib/Drupal/Core/Extension/ExtensionList.php @@ -6,12 +6,14 @@ */ namespace Drupal\Core\Extension; -use Drupal\Core\State\StateInterface; + +use Drupal\Component\Utility\SafeMarkup; +use Drupal\Core\Cache\CacheBackendInterface; /** * Provides available extensions. */ -class ExtensionList { +abstract class ExtensionList { /** * The type of the extension, either module or theme. @@ -28,11 +30,11 @@ class ExtensionList { protected $root; /** - * The state. + * The cache. * - * @var \Drupal\Core\State\StateInterface + * @var \Drupal\Core\Cache\CacheBackendInterface */ - protected $state; + protected $cache; /** * Default information added to every extension. @@ -69,32 +71,35 @@ class ExtensionList { * The app root. * @param string $type * The extension type. - * @param \Drupal\Core\State\StateInterface $state - * The state. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache + * The cache. * @param \Drupal\Core\Extension\InfoParserInterface $info_parser - * The info parser + * The info parser. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. */ - public function __construct($root, $type, StateInterface $state, InfoParserInterface $info_parser, ModuleHandlerInterface $module_handler) { + public function __construct($root, $type, CacheBackendInterface $cache, InfoParserInterface $info_parser, ModuleHandlerInterface $module_handler) { $this->root = $root; $this->type = $type; - $this->state = $state; + $this->cache = $cache; $this->infoParser = $info_parser; $this->moduleHandler = $module_handler; } + /** + * Resets the stored extension list. + */ public function reset() { $this->extensions = NULL; - $this->state->delete($this->getStateKey()); + $this->cache->delete($this->getCacheId()); } /** - * Returns the used state key. + * Returns the used cache ID. * * @return string */ - protected function getStateKey() { + protected function getCacheId() { return 'core.extension_listing.' . $this->type; } @@ -112,7 +117,7 @@ public function getName($name) { if (isset($extensions[$name])) { return $extensions[$name]->info['name']; } - return 'n/a'; + throw new \InvalidArgumentException(SafeMarkup::format('The @type %name does not exist.', ['@type' => $this->type, '%name' => $name])); } /** @@ -125,7 +130,11 @@ public function getName($name) { */ public function getExtension($name) { $extensions = $this->listExtensions(); - return $extensions[$name]; + if (!isset($extensions[$name])) { + return $extensions[$name]; + } + + throw new \InvalidArgumentException(SafeMarkup::format('The @type %name does not exist.', ['@type' => $this->type, '%name' => $name])); } /** @@ -137,11 +146,12 @@ public function listExtensions() { if (isset($this->extensions)) { return $this->extensions; } - if ($extensions = $this->state->get($this->getStateKey())) { + if ($extensions = $this->cache->get($this->getCacheId())) { + $this->extensions = $extensions; return $extensions; } $extensions = $this->doListExtensions(); - $this->state->get($this->getStateKey()); + $this->cache->get($this->getCacheId()); $this->extensions = $extensions; return $this->extensions; } diff --git a/core/lib/Drupal/Core/Extension/ModuleExtensionList.php b/core/lib/Drupal/Core/Extension/ModuleExtensionList.php index cdc504f..f46c7ff 100644 --- a/core/lib/Drupal/Core/Extension/ModuleExtensionList.php +++ b/core/lib/Drupal/Core/Extension/ModuleExtensionList.php @@ -7,12 +7,20 @@ namespace Drupal\Core\Extension; +use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +/** + * Provides a list of available modules. + */ class ModuleExtensionList extends ExtensionList { use StringTranslationTrait; + /** + * {@inheritdoc} + */ protected $defaults = [ 'dependencies' => [], 'description' => '', @@ -22,6 +30,35 @@ class ModuleExtensionList extends ExtensionList { ]; /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * Constructs a new ModuleExtensionList instance. + * + * @param string $root + * The app root. + * @param string $type + * The extension type. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache + * The cache. + * @param \Drupal\Core\Extension\InfoParserInterface $info_parser + * The info parser. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + */ + public function __construct($root, $type, CacheBackendInterface $cache, InfoParserInterface $info_parser, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) { + parent::__construct($root, $type, $cache, $cache, $info_parser, $module_handler); + + $this->configFactory = $config_factory; + } + + /** * {@inheritdoc} */ protected function doScanExtensions(ExtensionDiscovery $discovery) { @@ -71,6 +108,16 @@ protected function doListExtensions() { } } + // Add status, weight, and schema version. + $installed_modules = $this->configFactory->get('core.extension')->get('module') ?: []; + foreach ($extensions as $name => $module) { + $module->weight = isset($installed_modules[$name]) ? $installed_modules[$name] : 0; + $module->status = (int) isset($installed_modules[$name]); + $module->schema_version = SCHEMA_UNINSTALLED; + $files[$name] = $module->getPathname(); + } + $extensions = $this->moduleHandler->buildModuleDependencies($extensions); + return $extensions; } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 15491c0..00a8e10 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -902,17 +902,9 @@ function system_rebuild_module_data() { $modules = \Drupal::service('module_listing')->listExtensions(); $files = array(); ksort($modules); - // Add status, weight, and schema version. - $installed_modules = \Drupal::config('core.extension')->get('module') ?: array(); foreach ($modules as $name => $module) { - $module->weight = isset($installed_modules[$name]) ? $installed_modules[$name] : 0; - $module->status = (int) isset($installed_modules[$name]); - $module->schema_version = SCHEMA_UNINSTALLED; $files[$name] = $module->getPathname(); } - $modules = \Drupal::moduleHandler()->buildModuleDependencies($modules); - $modules_cache = $modules; - // Store filenames to allow system_list() and drupal_get_filename() to // retrieve them without having to rebuild or scan the filesystem. \Drupal::state()->set('system.module.files', $files);