PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/application/classes/OntoWiki/Module/Registry.php

https://code.google.com/p/ontowiki/
PHP | 326 lines | 158 code | 43 blank | 125 comment | 26 complexity | cf744a8f341b5c12bf6d2f682ef645b6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the {@link http://ontowiki.net OntoWiki} project.
  4. *
  5. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  6. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  7. */
  8. /**
  9. * OntoWiki module registry class.
  10. *
  11. * Serves as a central registry for modules.
  12. *
  13. * @category OntoWiki
  14. * @package Module
  15. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  16. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  17. * @author Norman Heino <norman.heino@gmail.com>
  18. */
  19. class OntoWiki_Module_Registry
  20. {
  21. /**
  22. * Default module context
  23. */
  24. const DEFAULT_CONTEXT = 'Default';
  25. const MODULE_CLASS_POSTFIX = 'Module';
  26. const MODULE_FILE_POSTFIX = 'Module.php';
  27. /**
  28. * Module is open
  29. */
  30. const MODULE_STATE_OPEN = 1;
  31. /**
  32. * Module is minimized
  33. */
  34. const MODULE_STATE_MINIMIZED = 2;
  35. /**
  36. * Module is hidden
  37. */
  38. const MODULE_STATE_HIDDEN = 3;
  39. /**
  40. * Array of modules
  41. * @var array
  42. */
  43. protected $_modules = array();
  44. /**
  45. * Module path
  46. * @var string
  47. */
  48. protected $_extensionDir = '';
  49. /**
  50. * Array of module states
  51. * @var array
  52. */
  53. protected $_moduleStates = array();
  54. /**
  55. * Array of module contexts (keys) and modules therein
  56. * @var array */
  57. protected $_moduleOrder = array();
  58. /**
  59. * Singleton instance
  60. * @var OntoWiki_Module_Registry
  61. */
  62. private static $_instance = null;
  63. /**
  64. * Singleton instance
  65. *
  66. * @return OntoWiki_Module_Registry
  67. */
  68. public static function getInstance()
  69. {
  70. if (null === self::$_instance) {
  71. self::$_instance = new self();
  72. }
  73. return self::$_instance;
  74. }
  75. /**
  76. * Resets the instance to its initial state. Mainly used for
  77. * testing purposes.
  78. */
  79. public function resetInstance()
  80. {
  81. $this->_modules = array();
  82. $this->_moduleStates = array();
  83. $this->_moduleOrder = array();
  84. }
  85. public static function reset()
  86. {
  87. if (null !== self::$_instance) {
  88. self::$_instance->resetInstance();
  89. }
  90. self::$_instance = null;
  91. }
  92. /**
  93. * Sets the path where modules are to be found
  94. *
  95. * @since 0.9.5
  96. * @return OntoWiki_Module_Registry
  97. */
  98. public function setExtensionPath($extensionDir)
  99. {
  100. $extensionDir = (string)$extensionDir;
  101. if (is_readable($extensionDir)) {
  102. $this->_extensionDir = $extensionDir;
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Registers modulewith name $moduleName in namespace $namspace.
  108. *
  109. * @param string $extensionName the name of the extension that brings this module
  110. * @param string $moduleName the name of the module
  111. * @param string $context in which context the module should be shown
  112. * @param array $options config array of the module
  113. * @return OntoWiki_Module_Registry
  114. */
  115. public function register($extensionName, $moduleFileName, $context = self::DEFAULT_CONTEXT, $options = null)
  116. {
  117. $moduleName = strtolower(substr($moduleFileName, 0, strlen($moduleFileName)-strlen(self::MODULE_FILE_POSTFIX)));
  118. // create module context if necessary
  119. if (!array_key_exists($context, $this->_moduleOrder)) {
  120. $this->_moduleOrder[$context] = array();
  121. }
  122. if($options == null){
  123. $options = new Zend_Config(array());
  124. }
  125. if (!array_key_exists($moduleName, $this->_modules)) {
  126. // merge defaults
  127. $options->merge(new Zend_Config(array(
  128. 'id' => $moduleName,
  129. 'classes' => '',
  130. 'name' => isset($options->name) ? $options->name : $moduleName,
  131. 'enabled' => true ,
  132. 'extensionName' => $extensionName,
  133. '_privateConfig' => $options->private
  134. )));
  135. ;
  136. // set css classes according to module state
  137. switch ($this->_moduleStates->{$options->id}) {
  138. case self::MODULE_STATE_OPEN:
  139. break;
  140. case self::MODULE_STATE_MINIMIZED:
  141. $options->classes .= ' is-minimized';
  142. break;
  143. case self::MODULE_STATE_HIDDEN:
  144. $options->classes .= ' is-disabled';
  145. break;
  146. }
  147. // register module
  148. $this->_modules[$moduleName] = $options;
  149. } else if (in_array($moduleName, $this->_moduleOrder[$context])) {
  150. throw new Exception("Module '$moduleName' is already registered for context '$context'.");
  151. }
  152. // set module order and context
  153. if (isset($options->priority)) {
  154. $position = $this->_getModulePosition($context, $options->priority);
  155. $this->_moduleOrder[$context][$position] = $moduleName;
  156. } else {
  157. $this->_moduleOrder[$context][] = $moduleName;
  158. }
  159. return $this;
  160. }
  161. /**
  162. * Returns whether the module with $moduleName has been registered
  163. * under namespace $namespace.
  164. *
  165. * @param string $moduleName
  166. * @param string $namespace
  167. * @return boolean
  168. */
  169. public function isModuleEnabled($moduleName)
  170. {
  171. return array_key_exists($moduleName, $this->_modules) && $this->_modules[$moduleName]->enabled == true;
  172. }
  173. /**
  174. * Sets the module's state to disabled. If the module doesn't exists, it is
  175. * registered as disabled.
  176. *
  177. * @param string $moduleName
  178. * @param string $namespace
  179. * @return OntoWiki_Module_Registry
  180. */
  181. public function disableModule($moduleName, $context = self::DEFAULT_CONTEXT)
  182. {
  183. if ($this->isModuleEnabled($moduleName)) {
  184. $this->_modules[$moduleName]->enabled = false;
  185. } else {
  186. $this->register($moduleName, $moduleName, $context, new Zend_Config(array('enabled' => false), true));
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Returns an instance of the module denoted by $moduleName, if registered.
  192. *
  193. * @param string $moduleName
  194. * @return OntoWiki_Module
  195. * @throws OntoWiki_Module_Exception if a module with the has not been registered.
  196. */
  197. public function getModule($moduleName, $context = null)
  198. {
  199. if(!$this->isModuleEnabled($moduleName)){
  200. return null;
  201. }
  202. $moduleFile = $this->_extensionDir
  203. . $this->_modules[$moduleName]->extensionName
  204. . DIRECTORY_SEPARATOR
  205. . ucfirst($moduleName)
  206. . self::MODULE_FILE_POSTFIX;
  207. if (!is_readable($moduleFile)) {
  208. throw new OntoWiki_Module_Exception("Module '$moduleName' could not be loaded from path '$moduleFile'.");
  209. }
  210. // instantiate module
  211. require_once $moduleFile;
  212. $moduleClass = ucfirst($moduleName)
  213. . self::MODULE_CLASS_POSTFIX;
  214. $module = null;
  215. if (class_exists($moduleClass)) {
  216. $module = new $moduleClass($moduleName, $context, $this->_modules[$moduleName]);
  217. }
  218. return $module;
  219. }
  220. public function getModules(){
  221. return $this->_modules;
  222. }
  223. /**
  224. * Returns the config for the module denoted by $moduleName.
  225. *
  226. * @param string $moduleName The module's name
  227. * @return array
  228. */
  229. public function getModuleConfig($moduleName)
  230. {
  231. $moduleName = (string)$moduleName;
  232. if (array_key_exists($moduleName, $this->_modules)) {
  233. return $this->_modules[$moduleName];
  234. }
  235. }
  236. /**
  237. * Returns all module names that are registered and enabled under
  238. * namespace $namespace.
  239. *
  240. * @param string $namespace
  241. * @return array|null
  242. */
  243. public function getModulesForContext($context = self::DEFAULT_CONTEXT)
  244. {
  245. $modules = array();
  246. if (array_key_exists($context, $this->_moduleOrder)) {
  247. ksort($this->_moduleOrder[$context]);
  248. foreach ($this->_moduleOrder[$context] as $moduleName) {
  249. if (array_key_exists($moduleName, $this->_modules)) {
  250. if ((boolean)$this->_modules[$moduleName]->enabled === true) {
  251. $modules[$moduleName] = $this->_modules[$moduleName];
  252. }
  253. }
  254. }
  255. }
  256. return $modules;
  257. }
  258. /**
  259. * Returns the first empty position greater than $priority
  260. * in the internal module array.
  261. *
  262. * @param string $context The module context
  263. * @param int $priority The module's priority request
  264. */
  265. protected function _getModulePosition($context, $priority)
  266. {
  267. while (array_key_exists($priority, $this->_moduleOrder[$context])) {
  268. $priority++;
  269. }
  270. return $priority;
  271. }
  272. /**
  273. * Constructor
  274. */
  275. private function __construct()
  276. {
  277. $this->_moduleStates = new Zend_Session_Namespace('Module_Registry');
  278. // TODO: module order per namespace?
  279. if (isset($this->_moduleStates->moduleOrder)) {
  280. $this->_moduleOrder = $this->_moduleStates->moduleOrder;
  281. }
  282. }
  283. }