You are here

class Module in Plug 7

Hierarchy

  • class \Drupal\plug\Util\Module

Expanded class hierarchy of Module

3 files declare their use of Module
FruitPluginManager.php in modules/plug_example/src/FruitPluginManager.php
Contains \Drupal\plug_example\FruitPluginManager.
NamePluginManager.php in modules/plug_example/src/NamePluginManager.php
Contains \Drupal\plug_example\NamePluginManager.
PlugBasicTest.php in lib/Drupal/plug/Tests/PlugBasicTest.php
Basic tests cases for plug module.

File

src/Util/Module.php, line 10
Contains Drupal\plug\Util\Module.

Namespace

Drupal\plug\Util
View source
class Module {

  /**
   * Generates the cached array of available namespaces for plugins.
   *
   * @param bool $all
   *   Include values for disabled modules.
   *
   * @return \ArrayObject
   *   The generated array of namespaces.
   */
  public static function getNamespaces($all = FALSE) {
    if ($all) {

      // Do not use caches or cache the results.
      return new \ArrayObject(static::namespaces($all));
    }
    return new \ArrayObject(static::memoize('namespaces'));
  }

  /**
   * Generates the cached array of enabled module directories.
   *
   * @param bool $all
   *   Include values for disabled modules.
   *
   * @return array
   *   A list of module directories.
   */
  public static function getDirectories($all = FALSE) {
    if ($all) {

      // Do not use caches or cache the results.
      return static::directories($all);
    }
    return static::memoize('directories');
  }

  /**
   * Memoize function to cache method results.
   *
   * @param string $method_name
   *   The method to execute name.
   *
   * @return array
   *   The function result
   */
  protected static function memoize($method_name) {
    $cache_name = 'module_' . drupal_strtolower($method_name);
    $data =& drupal_static($cache_name);
    if (isset($data)) {
      return $data;
    }
    if ($cache = cache_get($cache_name)) {
      $data = $cache->data;
      return $data;
    }
    $data = call_user_func_array(array(
      get_called_class(),
      $method_name,
    ), array());
    cache_set($cache_name, $data);
    return $data;
  }

  /**
   * Gets all the module directories.
   *
   * @param bool $all
   *   Include values for disabled modules.
   *
   * @return array
   *   A list of module directories.
   */
  protected static function directories($all = FALSE) {
    $directories = array();
    if ($all) {

      // We cannot use module_list to get the disabled modules. Query the system
      // table instead.
      $sql = "SELECT name FROM {system} WHERE type = :type";
      $modules = db_query($sql, array(
        ':type' => 'module',
      ))
        ->fetchCol();
      $modules = drupal_map_assoc($modules);
    }
    else {
      $modules = module_list();
    }
    foreach ($modules as $module) {
      $directories[$module] = drupal_get_path('module', $module);
    }
    return $directories;
  }

  /**
   * Gets the array of available namespaces for plugins.
   *
   * @param bool $all
   *   Include values for disabled modules.
   *
   * @return array
   *   The generated array of namespaces.
   */
  protected static function namespaces($all = FALSE) {
    $namespaces = array();
    foreach (static::getDirectories($all) as $module => $path) {
      $namespaces['Drupal\\' . $module] = $path . '/src';
    }
    return $namespaces;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Module::directories protected static function Gets all the module directories.
Module::getDirectories public static function Generates the cached array of enabled module directories.
Module::getNamespaces public static function Generates the cached array of available namespaces for plugins.
Module::memoize protected static function Memoize function to cache method results.
Module::namespaces protected static function Gets the array of available namespaces for plugins.