You are here

class ModuleHandler in Service Container 7.2

Same name and namespace in other branches
  1. 7 src/Extension/ModuleHandler.php \Drupal\service_container\Extension\ModuleHandler

Class that manages modules in a Drupal installation.

@codeCoverageIgnore

@todo: Some of this might be unit-testable.

Hierarchy

Expanded class hierarchy of ModuleHandler

1 string reference to 'ModuleHandler'
ModuleHandlerTest::getInfo in lib/Drupal/service_container/Tests/ModuleHandlerTest.php

File

src/Extension/ModuleHandler.php, line 21
Contains Drupal\service_container\Extension\ModuleHandler.

Namespace

Drupal\service_container\Extension
View source
class ModuleHandler implements ModuleHandlerInterface {

  /**
   * The Drupal7 service.
   *
   * @var \Drupal\service_container\Legacy\Drupal7
   */
  protected $drupal7;

  /**
   * The app root.
   *
   * @var string
   */
  protected $root;

  /**
   * Constructs a ModuleHandler object.
   *
   * @param string $root
   *   The app root.
   * @param \Drupal\service_container\Legacy\Drupal7 $drupal7
   *   The Drupal 7 legacy service.
   *
   * @see \Drupal\Core\DrupalKernel
   * @see \Drupal\Core\CoreServiceProvider
   */
  public function __construct($root, Drupal7 $drupal7) {
    $this->root = $root;
    $this->drupal7 = $drupal7;
  }

  /**
   * {@inheritdoc}
   */
  public function load($name) {
    return $this->drupal7
      ->drupal_load('module', $name);
  }

  /**
   * {@inheritdoc}
   */
  public function loadAll() {
    $this->drupal7
      ->module_load_all();
  }

  /**
   * {@inheritdoc}
   */
  public function reload() {
    $this->drupal7
      ->module_load_all();
  }

  /**
   * {@inheritdoc}
   */
  public function isLoaded() {
    return $this->drupal7
      ->module_load_all();
  }

  /**
   * {@inheritdoc}
   */
  public function getModuleList() {
    $module_list = array();
    foreach ($this->drupal7
      ->module_list() as $module) {
      $module_list[$module] = $this
        ->getModule($module);
    }
    return $module_list;
  }

  /**
   * {@inheritdoc}
   */
  public function getModule($name) {
    if (!$this->drupal7
      ->module_exists($name)) {
      throw new \InvalidArgumentException(sprintf('The module %s does not exist.', $name));
    }
    $filename = $this->drupal7
      ->drupal_get_filename('module', $name);
    return new Extension($this->root, 'module', $filename, $name . '.info');
  }

  /**
   * {@inheritdoc}
   */
  public function setModuleList(array $module_list = array()) {

    // Convert an array of module filenames to an array of module info, keyed by
    // module name.
    foreach ($module_list as $module_name => $filename) {
      $module_list[$module_name] = array(
        'filename' => $filename,
      );
    }
    $this->drupal7
      ->module_list(FALSE, FALSE, FALSE, $module_list);
  }

  /**
   * {@inheritdoc}
   */
  public function addModule($name, $path) {
    throw new \BadMethodCallException('ModuleHandler::addModule is not implemented.');
  }

  /**
   * {@inheritdoc}
   */
  public function addProfile($name, $path) {
    throw new \BadMethodCallException('ModuleHandler::addProfile is not implemented.');
  }

  /**
   * {@inheritdoc}
   */
  public function buildModuleDependencies(array $modules) {

    // @TODO
  }

  /**
   * {@inheritdoc}
   */
  public function moduleExists($module) {
    return $this->drupal7
      ->module_exists($module);
  }

  /**
   * {@inheritdoc}
   */
  public function loadAllIncludes($type, $name = NULL) {
    $this->drupal7
      ->module_load_all_includes($type, $name);
  }

  /**
   * {@inheritdoc}
   */
  public function loadInclude($module, $type, $name = NULL) {
    $this->drupal7
      ->module_load_include($type, $module, $name);
  }

  /**
   * {@inheritdoc}
   */
  public function getHookInfo() {
    return $this->drupal7
      ->module_hook_info();
  }

  /**
   * {@inheritdoc}
   */
  public function getImplementations($hook) {
    return $this->drupal7
      ->module_implements($hook);
  }

  /**
   * {@inheritdoc}
   */
  public function writeCache() {
    $this->drupal7
      ->module_implements_write_cache();
  }

  /**
   * {@inheritdoc}
   */
  public function resetImplementations() {
    $this->drupal7
      ->drupal_static_reset('module_implements');
  }

  /**
   * {@inheritdoc}
   */
  public function implementsHook($module, $hook) {
    $implementations = $this->drupal7
      ->module_implements($hook);
    return in_array($module, $implementations);
  }

  /**
   * {@inheritdoc}
   */
  public function invoke($module, $hook, array $args = array()) {
    return $this->drupal7
      ->module_invoke($module, $hook, $args);
  }

  /**
   * {@inheritdoc}
   */
  public function invokeAll($hook, array $args = array()) {
    return $this->drupal7
      ->module_invoke_all($hook, $args);
  }

  /**
   * {@inheritdoc}
   */
  public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL) {

    // @todo Sadly ::alter() does not allow three $context values.
    $this->drupal7
      ->drupal_alter($type, $data, $context1, $context2);
  }

  /**
   * {@inheritdoc}
   */
  public function getModuleDirectories() {
    $dirs = array();
    foreach ($this
      ->getModuleList() as $name => $module) {
      $dirs[$name] = $this->root . '/' . $module
        ->getPath();
    }
    return $dirs;
  }

  /**
   * {@inheritdoc}
   */
  public function getName($module) {
    $module_data = $this->drupal7
      ->system_rebuild_module_data();
    return $module_data[$module]->info['name'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModuleHandler::$drupal7 protected property The Drupal7 service.
ModuleHandler::$root protected property The app root.
ModuleHandler::addModule public function Adds a module to the list of currently active modules. Overrides ModuleHandlerInterface::addModule
ModuleHandler::addProfile public function Adds an installation profile to the list of currently active modules. Overrides ModuleHandlerInterface::addProfile
ModuleHandler::alter public function Passes alterable variables to specific hook_TYPE_alter() implementations. Overrides ModuleHandlerInterface::alter
ModuleHandler::buildModuleDependencies public function Determines which modules require and are required by each module. Overrides ModuleHandlerInterface::buildModuleDependencies
ModuleHandler::getHookInfo public function Retrieves a list of hooks that are declared through hook_hook_info(). Overrides ModuleHandlerInterface::getHookInfo
ModuleHandler::getImplementations public function Determines which modules are implementing a hook. Overrides ModuleHandlerInterface::getImplementations
ModuleHandler::getModule public function Returns a module extension object from the currently active modules list. Overrides ModuleHandlerInterface::getModule
ModuleHandler::getModuleDirectories public function Returns an array of directories for all enabled modules. Useful for tasks such as finding a file that exists in all module directories. Overrides ModuleHandlerInterface::getModuleDirectories
ModuleHandler::getModuleList public function Returns the list of currently active modules. Overrides ModuleHandlerInterface::getModuleList
ModuleHandler::getName public function Gets the human readable name of a given module. Overrides ModuleHandlerInterface::getName
ModuleHandler::implementsHook public function Returns whether a given module implements a given hook. Overrides ModuleHandlerInterface::implementsHook
ModuleHandler::invoke public function Invokes a hook in a particular module. Overrides ModuleHandlerInterface::invoke
ModuleHandler::invokeAll public function Invokes a hook in all enabled modules that implement it. Overrides ModuleHandlerInterface::invokeAll
ModuleHandler::isLoaded public function Returns whether all modules have been loaded. Overrides ModuleHandlerInterface::isLoaded
ModuleHandler::load public function Includes a module's .module file. Overrides ModuleHandlerInterface::load
ModuleHandler::loadAll public function Loads all enabled modules. Overrides ModuleHandlerInterface::loadAll
ModuleHandler::loadAllIncludes public function Loads an include file for each enabled module. Overrides ModuleHandlerInterface::loadAllIncludes
ModuleHandler::loadInclude public function Loads a module include file. Overrides ModuleHandlerInterface::loadInclude
ModuleHandler::moduleExists public function Determines whether a given module is enabled. Overrides ModuleHandlerInterface::moduleExists
ModuleHandler::reload public function Reloads all enabled modules. Overrides ModuleHandlerInterface::reload
ModuleHandler::resetImplementations public function Resets the cached list of hook implementations. Overrides ModuleHandlerInterface::resetImplementations
ModuleHandler::setModuleList public function Sets an explicit list of currently active modules. Overrides ModuleHandlerInterface::setModuleList
ModuleHandler::writeCache public function Write the hook implementation info to the cache. Overrides ModuleHandlerInterface::writeCache
ModuleHandler::__construct public function Constructs a ModuleHandler object.