class DefaultModuleHelper

Classes that need to keep a record of the name of the module they serve may choose to extend this.

It manages a settable factory, and provides a default factory.

UML: DefaultModuleHelper

@todo Consider instead or also using an IModule delegate instead of just passing a $module string around. Migration required.

@author darrenkelly

Hierarchy

Expanded class hierarchy of DefaultModuleHelper

6 files declare their use of DefaultModuleHelper
AbstractFormController.php in lib/Drupal/ooe/Form/AbstractFormController.php
AbstractFormController
AbstractPageController.php in lib/Drupal/ooe/Page/AbstractPageController.php
AbstractPageController
AbstractProject.php in lib/Drupal/ooe/Project/AbstractProject.php
AbstractProject
DefaultMenuItem.php in lib/Drupal/ooe/Menu/DefaultMenuItem.php
DefaultMenuItem
DemoOfMenuTabs.php in lib/Drupal/ooe/Demo/Tabs/DemoOfMenuTabs.php
DemoOfMenuTabs

... See full list

File

lib/Drupal/ooe/Module/DefaultModuleHelper.php, line 27
DefaultModuleHelper

Namespace

Drupal\ooe\Module
View source
class DefaultModuleHelper implements IModuleHelper {

  /**
   * The module machine name.
   *
   * @todo Consider rename to $moduleName.
   *
   * @var string $module
   */
  private $module;

  /**
   * Sets the module machine name.
   *
   * Not needed by public, should always inject via constructor.
   *
   * @todo Consider rename to setModuleName.
   *
   * @param string $module
   *   Required. A non empty module machine name.
   *
   * @return IModuleHelper
   *   This.
   */
  private function setModule($module) {
    if (empty($module) || !is_string($module)) {
      throw new \Exception('$module must be a non null string');
    }
    $this->module = $module;
    return $this;
  }

  /**
   * Gets the module machine name.
   *
   * @todo Consider rename to getModuleName.
   *
   * @return string
   *   The module machine name.
   */
  public function getModule() {
    return $this->module;
  }

  /**
   * Constructor.
   *
   * @param string $module
   *   Required. The machine name of the module.
   *
   * @param IFactory $factory
   *   Optional factory; if none given or null
   *   a @link DefaultFactory @endlink will be used.
   */
  public function __construct($module, IFactory $factory = NULL) {
    if (empty($module) || !is_string($module)) {
      throw new \Exception('$module must be a non-null string !');
    }
    $this->setModule($module);
    if (!empty($factory)) {
      $this->setFactory($factory);
    }
  }

  /**
   * The current factory of this.
   *
   * @var \Drupal\ooe\Factory\IFactory
   */
  private $factory;

  /**
   * The current factory.
   *
   * @return \Drupal\ooe\Factory\IFactory
   *   The current factory. Will always be non-null.
   */
  public function getFactory() {
    return $this->factory();
  }

  /**
   * The current factory or a lazily created @link DefaultFactory @endlink.
   *
   * @return \Drupal\ooe\Factory\IFactory
   *   The currently set factory, otherwise
   *   lazily creates a fresh @link DefaultFactory @endlink.
   */
  protected function factory() {
    // @todo Consider promote and use static myFactory to choose default.
    if (empty($this->factory)) {
      $this->factory = new DefaultFactory($this->getModule());
    }
    return $this->factory;
  }

  /**
   * The current factory, or a lazily created @link DefaultFactory @endlink.
   *
   * Convenient shorthand version of
   * @link DefaultModuleHelper::factory() @endlink
   * since used so often.
   *
   * @return \Drupal\ooe\Factory\IFactory
   *   The currently set factory, otherwise a
   *   lazily created @link DefaultFactory @endlink.
   */
  protected function f() {
    return $this->factory();
  }

  /**
   * Sets the factory of this.
   *
   * The module machine name of the provided factory must
   * match the module machine name known to this.
   *
   * @param \Drupal\ooe\Factory\IFactory $factory
   *   The factory.
   *
   * @return \Drupal\ooe\Module\IModuleHelper
   *   This.
   * @throws \Exception
   *   If $factory is empty or the module machine name
   *   of the factory is inconsistent.
   */
  protected function setFactory(IFactory $factory) {
    if (empty($factory)) {
      throw new \Exception('IFactory $factory must not be empty !');
    }
    if ($factory->getModuleName() != $this->getModule()) {
      throw new Exception(
      'The module machine name(' . $factory->getModuleName() .
        ') of the provided factory does match' .
        'The module machine name(' . $this->getModule() . ') of this !'
        );
      // Coder: ERROR | String concat is not required here;
      // use a single string instead
      // Webel: Bug report: https://www.drupal.org/node/2305591
    }
    $this->factory = $factory;
    return $this;
  }

  /**
   * A human readable and displayable name of the module.
   *
   * @return string
   *   The module display name (if set) or
   *   the module machine name as upper case.
   */
  public function getModuleDisplayName() {
    if (!empty($this->displayName)) {
      return $this->displayName;
    }
    return strtoupper($this->module);
  }

  /**
   * A human readable and displayable name of the module.
   *
   * @var string $displayName
   */
  private $displayName;

  /**
   * Sets a human readable and displayable name of the module.
   *
   * @param string $displayName
   *   A human readable and displayable name of the module.
   *
   * @return IModuleHelper
   *   This.
   */
  public function setModuleDisplayName($displayName) {
    $this->displayName = $displayName;
    return $this;
  }

}

Members

Contains filters are case sensitive
Name Modifiers Type Descriptionsort descending
DefaultModuleHelper::getModuleDisplayName public function A human readable and displayable name of the module. Overrides IModuleHelper::getModuleDisplayName
DefaultModuleHelper::$displayName private property A human readable and displayable name of the module.
DefaultModuleHelper::__construct public function Constructor.
DefaultModuleHelper::getModule public function Gets the module machine name. Overrides IModuleHelper::getModule
DefaultModuleHelper::setModuleDisplayName public function Sets a human readable and displayable name of the module. Overrides IModuleHelper::setModuleDisplayName
DefaultModuleHelper::setFactory protected function Sets the factory of this.
DefaultModuleHelper::setModule private function Sets the module machine name.
DefaultModuleHelper::$factory private property The current factory of this.
DefaultModuleHelper::factory protected function The current factory or a lazily created DefaultFactory.
DefaultModuleHelper::f protected function The current factory, or a lazily created DefaultFactory.
DefaultModuleHelper::getFactory public function The current factory. Overrides IModuleHelper::getFactory
DefaultModuleHelper::$module private property The module machine name.