From c1ca622460a914a0a8b4d9aed57d669d8f10c76a Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 8 Jun 2014 18:47:49 -0500 Subject: [PATCH] Issue #2245449 by lostkangaroo, chx, Xano: Added a ModuleHandler::getModule(). --- .../Drupal/Core/Extension/ModuleHandler.php | 10 ++++++++++ .../Core/Extension/ModuleHandlerInterface.php | 14 +++++++++++++ .../Core/Extension/ModuleHandlerTest.php | 20 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index 89b47b3ee5f..0ef41c3bcca 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -148,6 +148,16 @@ class ModuleHandler implements ModuleHandlerInterface { return $this->moduleList; } + /** + * {@inheritdoc} + */ + public function getModule($name) { + if (isset($this->moduleList[$name])) { + return $this->moduleList[$name]; + } + throw new \InvalidArgumentException(sprintf('The module %s does not exist.', $name)); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php index 64066986221..97250aa255e 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php @@ -57,6 +57,20 @@ interface ModuleHandlerInterface { */ public function getModuleList(); + /** + * Returns a module extension object from the currently active modules list. + * + * @param string $name + * The name of the module to return. + * + * @return \Drupal\Core\Extension\Extension + * An extension object. + * + * @throws \InvalidArgumentException + * Thrown when the requested module does not exist. + */ + public function getModule($name); + /** * Sets an explicit list of currently active modules. * diff --git a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php index 1709293aed4..cfb86f2a239 100644 --- a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php @@ -49,6 +49,8 @@ class ModuleHandlerTest extends UnitTestCase { /** * {@inheritdoc} + * + * @covers ::__construct */ protected function setUp() { $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); @@ -151,6 +153,24 @@ class ModuleHandlerTest extends UnitTestCase { )); } + /** + * Confirm we get back a module from the module list + * + * @covers ::getModule + */ + public function testGetModuleWithExistingModule() { + $this->assertEquals($this->moduleHandler->getModule('module_handler_test'), new Extension('module', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test/module_handler_test.info.yml', 'module_handler_test.module')); + } + + /** + * @covers ::getModule + * + * @expectedException \InvalidArgumentException + */ + public function testGetModuleWithNonExistingModule() { + $this->moduleHandler->getModule('claire_alice_watch_my_little_pony_module_that_does_not_exist'); + } + /** * Ensure setting the module list replaces the module list and resets internal structures. *