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

merge-requests/1654/head
Nathaniel Catchpole 2018-06-21 22:51:20 +01:00
parent fb05cb4bd3
commit aae672cc28
14 changed files with 65 additions and 30 deletions

View File

@ -10,6 +10,7 @@ use Drupal\Component\Utility\Html;
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.
}
}

View File

@ -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 {}

View File

@ -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 {}

View File

@ -4,6 +4,7 @@ namespace Drupal\Core\Extension;
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 @@ abstract class ExtensionList {
* @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 @@ abstract class ExtensionList {
* 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 @@ abstract class ExtensionList {
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 @@ abstract class ExtensionList {
* @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 @@ abstract class ExtensionList {
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 @@ abstract class ExtensionList {
* 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 @@ abstract class ExtensionList {
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 @@ abstract class ExtensionList {
* @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) {

View File

@ -5,6 +5,7 @@ namespace Drupal\Core\Extension;
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 @@ class ModuleHandler implements ModuleHandlerInterface {
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));
}
/**

View File

@ -61,7 +61,7 @@ interface ModuleHandlerInterface {
* @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);

View File

@ -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 @@ class ThemeHandler implements ThemeHandlerInterface {
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 @@ class ThemeHandler implements ThemeHandlerInterface {
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 @@ class ThemeHandler implements ThemeHandlerInterface {
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));
}
/**

View File

@ -39,8 +39,8 @@ interface ThemeHandlerInterface {
* @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 @@ interface ThemeHandlerInterface {
*
* @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 @@ interface ThemeHandlerInterface {
* @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);

View File

@ -7,6 +7,7 @@ use Drupal\Core\Cache\Cache;
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 @@ class ThemeInstaller implements ThemeInstallerInterface {
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 @@ class ThemeInstaller implements ThemeInstallerInterface {
$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.");

View File

@ -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 @@ interface ThemeInstallerInterface {
* @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()
*/

View File

@ -9,6 +9,7 @@ use Drupal\Component\Render\PlainTextOutput;
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 [];
}
}

View File

@ -4,6 +4,7 @@ namespace Drupal\KernelTests\Core\Theme;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ExtensionNameLengthException;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\KernelTests\KernelTestBase;
/**
@ -110,11 +111,11 @@ class ThemeInstallerTest extends KernelTestBase {
$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 @@ class ThemeInstallerTest extends KernelTestBase {
$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 @@ class ThemeInstallerTest extends KernelTestBase {
$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());
}
}

View File

@ -9,6 +9,7 @@ use Drupal\Core\Extension\ExtensionDiscovery;
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 @@ class ExtensionListTest extends UnitTestCase {
$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 @@ class ExtensionListTest extends UnitTestCase {
$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');
}

View File

@ -5,6 +5,7 @@ namespace Drupal\Tests\Core\Extension;
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 @@ class ModuleHandlerTest extends UnitTestCase {
* @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');
}