diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 4d28eaa0361500586b947003eaaaf8fe507488f6..c2aeb55e0c52a6e67f3e80c9dc553a5c20488b3f 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -10,6 +10,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Unicode; use Drupal\Core\Config\BootstrapConfigStorageFactory; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Test\TestDatabase; use Drupal\Core\Session\AccountInterface; @@ -246,7 +247,7 @@ function drupal_get_filename($type, $name, $filename = NULL) { try { return $extension_list->getPathname($name); } - catch (\InvalidArgumentException $e) { + catch (UnknownExtensionException $e) { // Catch the exception. This will result in triggering an error. } } diff --git a/core/lib/Drupal/Core/Extension/Exception/UninstalledExtensionException.php b/core/lib/Drupal/Core/Extension/Exception/UninstalledExtensionException.php new file mode 100644 index 0000000000000000000000000000000000000000..f0503f1980b571ef1ae3fd2b39d513925809decb --- /dev/null +++ b/core/lib/Drupal/Core/Extension/Exception/UninstalledExtensionException.php @@ -0,0 +1,8 @@ +<?php + +namespace Drupal\Core\Extension\Exception; + +/** + * Exception class thrown when a specified extension has not been installed. + */ +class UninstalledExtensionException extends \InvalidArgumentException {} diff --git a/core/lib/Drupal/Core/Extension/Exception/UnknownExtensionException.php b/core/lib/Drupal/Core/Extension/Exception/UnknownExtensionException.php new file mode 100644 index 0000000000000000000000000000000000000000..790285f8087cb43058c5fb30b58508a4ef7c7715 --- /dev/null +++ b/core/lib/Drupal/Core/Extension/Exception/UnknownExtensionException.php @@ -0,0 +1,8 @@ +<?php + +namespace Drupal\Core\Extension\Exception; + +/** + * Exception class thrown when a specified extension is not on the filesystem. + */ +class UnknownExtensionException extends \InvalidArgumentException {} diff --git a/core/lib/Drupal/Core/Extension/ExtensionList.php b/core/lib/Drupal/Core/Extension/ExtensionList.php index 6201a08a1150cc72bcbb302b4361bef27ef8ae00..6b5dfec45b08b1a49128bd7c8cb1ced437fcf66b 100644 --- a/core/lib/Drupal/Core/Extension/ExtensionList.php +++ b/core/lib/Drupal/Core/Extension/ExtensionList.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\DatabaseExceptionWrapper; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Core\State\StateInterface; /** @@ -227,7 +228,7 @@ public function exists($extension_name) { * @return string * The human-readable name of the extension. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException * If there is no extension with the supplied machine name. */ public function getName($extension_name) { @@ -244,7 +245,7 @@ public function getName($extension_name) { * A processed extension object for the extension with the specified machine * name. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException * If there is no extension with the supplied name. */ public function get($extension_name) { @@ -253,7 +254,7 @@ public function get($extension_name) { return $extensions[$extension_name]; } - throw new \InvalidArgumentException("The {$this->type} $extension_name does not exist."); + throw new UnknownExtensionException("The {$this->type} $extension_name does not exist."); } /** @@ -334,7 +335,7 @@ protected function doList() { * @return mixed[] * An associative array of extension information. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException * If there is no extension with the supplied name. */ public function getExtensionInfo($extension_name) { @@ -342,7 +343,7 @@ public function getExtensionInfo($extension_name) { if (isset($all_info[$extension_name])) { return $all_info[$extension_name]; } - throw new \InvalidArgumentException("The {$this->type} $extension_name does not exist or is not installed."); + throw new UnknownExtensionException("The {$this->type} $extension_name does not exist or is not installed."); } /** @@ -505,7 +506,7 @@ public function setPathname($extension_name, $pathname) { * The drupal-root relative filename and path of the requested extension's * .info.yml file. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException * If there is no extension with the supplied machine name. */ public function getPathname($extension_name) { @@ -518,7 +519,7 @@ public function getPathname($extension_name) { elseif (($path_names = $this->getPathnames()) && isset($path_names[$extension_name])) { return $path_names[$extension_name]; } - throw new \InvalidArgumentException("The {$this->type} $extension_name does not exist."); + throw new UnknownExtensionException("The {$this->type} $extension_name does not exist."); } /** @@ -533,7 +534,7 @@ public function getPathname($extension_name) { * @return string * The Drupal-root-relative path to the specified extension. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException * If there is no extension with the supplied name. */ public function getPath($extension_name) { diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index a3555cd98dc6cea18f7313bb18552b834237d1ef..8d43a857dd4c7a678ad190456b2ed14dcfe0b74e 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -5,6 +5,7 @@ use Drupal\Component\Graph\Graph; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Extension\Exception\UnknownExtensionException; /** * Class that manages modules in a Drupal installation. @@ -172,7 +173,7 @@ public function getModule($name) { if (isset($this->moduleList[$name])) { return $this->moduleList[$name]; } - throw new \InvalidArgumentException(sprintf('The module %s does not exist.', $name)); + throw new UnknownExtensionException(sprintf('The module %s does not exist.', $name)); } /** diff --git a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php index f1097e38810436e450202982d63f64ef153e104d..abb4edde32b177ba4c2603455bf4ec895042c45c 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php @@ -61,7 +61,7 @@ public function getModuleList(); * @return \Drupal\Core\Extension\Extension * An extension object. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException * Thrown when the requested module does not exist. */ public function getModule($name); diff --git a/core/lib/Drupal/Core/Extension/ThemeHandler.php b/core/lib/Drupal/Core/Extension/ThemeHandler.php index d54ff1fa1a1453cf4dc554c7cb33b9d43322d53e..4258fda9fa4f137e13f42de2bf93c3b01fe51d62 100644 --- a/core/lib/Drupal/Core/Extension/ThemeHandler.php +++ b/core/lib/Drupal/Core/Extension/ThemeHandler.php @@ -3,6 +3,8 @@ namespace Drupal\Core\Extension; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Extension\Exception\UninstalledExtensionException; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Core\State\StateInterface; /** @@ -147,7 +149,7 @@ public function getDefault() { public function setDefault($name) { $list = $this->listInfo(); if (!isset($list[$name])) { - throw new \InvalidArgumentException("$name theme is not installed."); + throw new UninstalledExtensionException("$name theme is not installed."); } $this->configFactory->getEditable('system.theme') ->set('default', $name) @@ -437,7 +439,7 @@ protected function getExtensionDiscovery() { public function getName($theme) { $themes = $this->listInfo(); if (!isset($themes[$theme])) { - throw new \InvalidArgumentException("Requested the name of a non-existing theme $theme"); + throw new UnknownExtensionException("Requested the name of a non-existing theme $theme"); } return $themes[$theme]->info['name']; } @@ -486,7 +488,7 @@ public function getTheme($name) { if (isset($themes[$name])) { return $themes[$name]; } - throw new \InvalidArgumentException(sprintf('The theme %s does not exist.', $name)); + throw new UnknownExtensionException(sprintf('The theme %s does not exist.', $name)); } /** diff --git a/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php index 00433f0b96640d286209c635a8fd3f73257858a4..56d9e9a8a86c4577d30900f343ac477d8b17d8c4 100644 --- a/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php @@ -39,8 +39,8 @@ public function install(array $theme_list, $install_dependencies = TRUE); * @param array $theme_list * The themes to uninstall. * - * @throws \InvalidArgumentException - * Thrown when you uninstall an not installed theme. + * @throws \Drupal\Core\Extension\Exception\UninstalledExtensionException + * Thrown when you try to uninstall a theme that wasn't installed. * * @see hook_themes_uninstalled() * @@ -146,6 +146,9 @@ public function getBaseThemes(array $themes, $theme); * * @return string * Returns the human readable name of the theme. + * + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException + * When the specified theme does not exist. */ public function getName($theme); @@ -206,7 +209,7 @@ public function themeExists($theme); * @return \Drupal\Core\Extension\Extension * An extension object. * - * @throws \InvalidArgumentException + * @throws \Drupal\Core\Extension\Extension\UnknownExtensionException * Thrown when the requested theme does not exist. */ public function getTheme($name); diff --git a/core/lib/Drupal/Core/Extension/ThemeInstaller.php b/core/lib/Drupal/Core/Extension/ThemeInstaller.php index 43a5469e3f4f3ce700efc8a544c8a14e51b63fd8..2d6567fa188665cf411c92c73979aa9777a78d77 100644 --- a/core/lib/Drupal/Core/Extension/ThemeInstaller.php +++ b/core/lib/Drupal/Core/Extension/ThemeInstaller.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigInstallerInterface; use Drupal\Core\Config\ConfigManagerInterface; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Core\Routing\RouteBuilderInterface; use Drupal\Core\State\StateInterface; use Psr\Log\LoggerInterface; @@ -111,7 +112,7 @@ public function install(array $theme_list, $install_dependencies = TRUE) { if ($missing = array_diff_key($theme_list, $theme_data)) { // One or more of the given themes doesn't exist. - throw new \InvalidArgumentException('Unknown themes: ' . implode(', ', $missing) . '.'); + throw new UnknownExtensionException('Unknown themes: ' . implode(', ', $missing) . '.'); } // Only process themes that are not installed currently. @@ -221,7 +222,7 @@ public function uninstall(array $theme_list) { $list = $this->themeHandler->listInfo(); foreach ($theme_list as $key) { if (!isset($list[$key])) { - throw new \InvalidArgumentException("Unknown theme: $key."); + throw new UnknownExtensionException("Unknown theme: $key."); } if ($key === $theme_config->get('default')) { throw new \InvalidArgumentException("The current default theme $key cannot be uninstalled."); diff --git a/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php b/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php index ad80762ed1cbf6342b6b8d1fd921cb7ecdf532a7..ae79b505ea1800000caebc207f237a983385d737 100644 --- a/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php +++ b/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php @@ -22,6 +22,9 @@ interface ThemeInstallerInterface { * * @throws \Drupal\Core\Extension\ExtensionNameLengthException * Thrown when the theme name is to long. + * + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException + * Thrown when the theme does not exist. */ public function install(array $theme_list, $install_dependencies = TRUE); @@ -34,8 +37,11 @@ public function install(array $theme_list, $install_dependencies = TRUE); * @param array $theme_list * The themes to uninstall. * + * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException + * Thrown when trying to uninstall a theme that was not installed. + * * @throws \InvalidArgumentException - * Thrown when you uninstall an not installed theme. + * Thrown when trying to uninstall the default theme or the admin theme. * * @see hook_themes_uninstalled() */ diff --git a/core/modules/system/system.module b/core/modules/system/system.module index d6bc508dab644d77ffb71aeb722d177c5eac4882..e82e1ffd4824d08353e431c2ba7164da72dd614b 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -9,6 +9,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Asset\AttachedAssetsInterface; use Drupal\Core\Cache\Cache; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Core\Queue\QueueGarbageCollectionInterface; use Drupal\Core\Database\Query\AlterableInterface; use Drupal\Core\Extension\Extension; @@ -972,7 +973,7 @@ function system_get_info($type, $name = NULL) { try { return $module_list->getExtensionInfo($name); } - catch (\InvalidArgumentException $e) { + catch (UnknownExtensionException $e) { return []; } } diff --git a/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php b/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php index 61f647528d0789c543420f0dc9dcdc182337df6b..dc606def6658bee3907bf3bb1da599f8bdc0c5e6 100644 --- a/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Theme/ThemeInstallerTest.php @@ -4,6 +4,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Extension\ExtensionNameLengthException; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\KernelTests\KernelTestBase; /** @@ -110,11 +111,11 @@ public function testInstallNonExisting() { $this->assertFalse(array_keys($themes)); try { - $message = 'ThemeHandler::install() throws InvalidArgumentException upon installing a non-existing theme.'; + $message = 'ThemeHandler::install() throws UnknownExtensionException upon installing a non-existing theme.'; $this->themeInstaller()->install([$name]); $this->fail($message); } - catch (\InvalidArgumentException $e) { + catch (UnknownExtensionException $e) { $this->pass(get_class($e) . ': ' . $e->getMessage()); } @@ -247,11 +248,11 @@ public function testUninstallNonExisting() { $this->assertFalse(array_keys($themes)); try { - $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon uninstalling a non-existing theme.'; + $message = 'ThemeHandler::uninstall() throws UnknownExtensionException upon uninstalling a non-existing theme.'; $this->themeInstaller()->uninstall([$name]); $this->fail($message); } - catch (\InvalidArgumentException $e) { + catch (UnknownExtensionException $e) { $this->pass(get_class($e) . ': ' . $e->getMessage()); } @@ -291,11 +292,11 @@ public function testUninstallNotInstalled() { $name = 'test_basetheme'; try { - $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon uninstalling a theme that is not installed.'; + $message = 'ThemeHandler::uninstall() throws UnknownExtensionException upon uninstalling a theme that is not installed.'; $this->themeInstaller()->uninstall([$name]); $this->fail($message); } - catch (\InvalidArgumentException $e) { + catch (UnknownExtensionException $e) { $this->pass(get_class($e) . ': ' . $e->getMessage()); } } diff --git a/core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php b/core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php index 993bf759ffba5e0aa56e480e45275d0388812829..7f73fe27ea7fc03a448225a5e7c835e7ce677151 100644 --- a/core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php @@ -9,6 +9,7 @@ use Drupal\Core\Extension\ExtensionList; use Drupal\Core\Extension\InfoParserInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Core\State\StateInterface; use Drupal\Tests\UnitTestCase; use org\bovigo\vfs\vfsStream; @@ -31,7 +32,7 @@ public function testGetNameWithNonExistingExtension() { $extension_discovery->scan('test_extension')->willReturn([]); $test_extension_list->setExtensionDiscovery($extension_discovery->reveal()); - $this->setExpectedException(\InvalidArgumentException::class); + $this->setExpectedException(UnknownExtensionException::class); $test_extension_list->getName('test_name'); } @@ -55,7 +56,7 @@ public function testGetWithNonExistingExtension() { $extension_discovery->scan('test_extension')->willReturn([]); $test_extension_list->setExtensionDiscovery($extension_discovery->reveal()); - $this->setExpectedException(\InvalidArgumentException::class); + $this->setExpectedException(UnknownExtensionException::class); $test_extension_list->get('test_name'); } diff --git a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php index 820c6bd47cccd7dd0a7846578a80063131be2feb..3adaf13507a3c625f1bde0ae82fd1988a7abcc44 100644 --- a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\Extension; use Drupal\Core\Extension\ModuleHandler; +use Drupal\Core\Extension\Exception\UnknownExtensionException; use Drupal\Tests\UnitTestCase; /** @@ -164,7 +165,7 @@ public function testGetModuleWithExistingModule() { * @covers ::getModule */ public function testGetModuleWithNonExistingModule() { - $this->setExpectedException(\InvalidArgumentException::class); + $this->setExpectedException(UnknownExtensionException::class); $this->getModuleHandler()->getModule('claire_alice_watch_my_little_pony_module_that_does_not_exist'); }