Commit aae672cc authored by catch's avatar catch

Issue #2940203 by almaudoh, dawehner: Use dedicated Exception classes for extension system

parent fb05cb4b
......@@ -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.
}
}
......
<?php
namespace Drupal\Core\Extension\Exception;
/**
* Exception class thrown when a specified extension has not been installed.
*/
class UninstalledExtensionException extends \InvalidArgumentException {}
<?php
namespace Drupal\Core\Extension\Exception;
/**
* Exception class thrown when a specified extension is not on the filesystem.
*/
class UnknownExtensionException extends \InvalidArgumentException {}
......@@ -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) {
......
......@@ -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));
}
/**
......
......@@ -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);
......
......@@ -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));
}
/**
......
......@@ -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);
......
......@@ -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.");
......
......@@ -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()
*/
......
......@@ -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 [];
}
}
......
......@@ -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());
}
}
......
......@@ -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');
}
......
......@@ -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');
}
......
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