DefaultModuleHelper.php

DefaultModuleHelper

Namespace

Drupal\ooe\Module

File

lib/Drupal/ooe/Module/DefaultModuleHelper.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * DefaultModuleHelper
  5. */
  6. namespace Drupal\ooe\Module;
  7. use Drupal\ooe\Factory\IFactory;
  8. use Drupal\ooe\Factory\DefaultFactory;
  9. /**
  10. * Classes that need to keep a record of the name of
  11. * the module they serve may choose to extend this.
  12. *
  13. * It manages a settable factory, and provides a default factory.
  14. *
  15. * UML:
  16. * @link http://drupal7demo.webel.com.au/node/1014 DefaultModuleHelper @endlink
  17. *
  18. * @todo Consider instead or also using an IModule delegate instead
  19. * of just passing a $module string around. Migration required.
  20. *
  21. * @author darrenkelly
  22. */
  23. class DefaultModuleHelper implements IModuleHelper {
  24. /**
  25. * The module machine name.
  26. *
  27. * @todo Consider rename to $moduleName.
  28. *
  29. * @var string $module
  30. */
  31. private $module;
  32. /**
  33. * Sets the module machine name.
  34. *
  35. * Not needed by public, should always inject via constructor.
  36. *
  37. * @todo Consider rename to setModuleName.
  38. *
  39. * @param string $module
  40. * Required. A non empty module machine name.
  41. *
  42. * @return IModuleHelper
  43. * This.
  44. */
  45. private function setModule($module) {
  46. if (empty($module) || !is_string($module)) {
  47. throw new \Exception('$module must be a non null string');
  48. }
  49. $this->module = $module;
  50. return $this;
  51. }
  52. /**
  53. * Gets the module machine name.
  54. *
  55. * @todo Consider rename to getModuleName.
  56. *
  57. * @return string
  58. * The module machine name.
  59. */
  60. public function getModule() {
  61. return $this->module;
  62. }
  63. /**
  64. * Constructor.
  65. *
  66. * @param string $module
  67. * Required. The machine name of the module.
  68. *
  69. * @param IFactory $factory
  70. * Optional factory; if none given or null
  71. * a @link DefaultFactory @endlink will be used.
  72. */
  73. public function __construct($module, IFactory $factory = NULL) {
  74. if (empty($module) || !is_string($module)) {
  75. throw new \Exception('$module must be a non-null string !');
  76. }
  77. $this->setModule($module);
  78. if (!empty($factory)) {
  79. $this->setFactory($factory);
  80. }
  81. }
  82. /**
  83. * The current factory of this.
  84. *
  85. * @var \Drupal\ooe\Factory\IFactory
  86. */
  87. private $factory;
  88. /**
  89. * The current factory.
  90. *
  91. * @return \Drupal\ooe\Factory\IFactory
  92. * The current factory. Will always be non-null.
  93. */
  94. public function getFactory() {
  95. return $this->factory();
  96. }
  97. /**
  98. * The current factory or a lazily created @link DefaultFactory @endlink.
  99. *
  100. * @return \Drupal\ooe\Factory\IFactory
  101. * The currently set factory, otherwise
  102. * lazily creates a fresh @link DefaultFactory @endlink.
  103. */
  104. protected function factory() {
  105. // @todo Consider promote and use static myFactory to choose default.
  106. if (empty($this->factory)) {
  107. $this->factory = new DefaultFactory($this->getModule());
  108. }
  109. return $this->factory;
  110. }
  111. /**
  112. * The current factory, or a lazily created @link DefaultFactory @endlink.
  113. *
  114. * Convenient shorthand version of
  115. * @link DefaultModuleHelper::factory() @endlink
  116. * since used so often.
  117. *
  118. * @return \Drupal\ooe\Factory\IFactory
  119. * The currently set factory, otherwise a
  120. * lazily created @link DefaultFactory @endlink.
  121. */
  122. protected function f() {
  123. return $this->factory();
  124. }
  125. /**
  126. * Sets the factory of this.
  127. *
  128. * The module machine name of the provided factory must
  129. * match the module machine name known to this.
  130. *
  131. * @param \Drupal\ooe\Factory\IFactory $factory
  132. * The factory.
  133. *
  134. * @return \Drupal\ooe\Module\IModuleHelper
  135. * This.
  136. * @throws \Exception
  137. * If $factory is empty or the module machine name
  138. * of the factory is inconsistent.
  139. */
  140. protected function setFactory(IFactory $factory) {
  141. if (empty($factory)) {
  142. throw new \Exception('IFactory $factory must not be empty !');
  143. }
  144. if ($factory->getModuleName() != $this->getModule()) {
  145. throw new Exception(
  146. 'The module machine name(' . $factory->getModuleName() .
  147. ') of the provided factory does match' .
  148. 'The module machine name(' . $this->getModule() . ') of this !'
  149. );
  150. // Coder: ERROR | String concat is not required here;
  151. // use a single string instead
  152. // Webel: Bug report: https://www.drupal.org/node/2305591
  153. }
  154. $this->factory = $factory;
  155. return $this;
  156. }
  157. /**
  158. * A human readable and displayable name of the module.
  159. *
  160. * @return string
  161. * The module display name (if set) or
  162. * the module machine name as upper case.
  163. */
  164. public function getModuleDisplayName() {
  165. if (!empty($this->displayName)) {
  166. return $this->displayName;
  167. }
  168. return strtoupper($this->module);
  169. }
  170. /**
  171. * A human readable and displayable name of the module.
  172. *
  173. * @var string $displayName
  174. */
  175. private $displayName;
  176. /**
  177. * Sets a human readable and displayable name of the module.
  178. *
  179. * @param string $displayName
  180. * A human readable and displayable name of the module.
  181. *
  182. * @return IModuleHelper
  183. * This.
  184. */
  185. public function setModuleDisplayName($displayName) {
  186. $this->displayName = $displayName;
  187. return $this;
  188. }
  189. }

Classes

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