| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version $Id ; modules.inc.php 16:08:23 07:02:2012 Ahmed Said ; |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Cover all modules functionality. |
| 8 |
* |
| 9 |
* @author Ahmed Said. |
| 10 |
*/ |
| 11 |
class CJTModulesEngine { |
| 12 |
/** |
| 13 |
* Modules directory. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $directory = ''; |
| 18 |
|
| 19 |
/** |
| 20 |
* All created/processed modules instances. |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
public $modules = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* To process modules list. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private $modulesList = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Wordpress option name for the modules list |
| 35 |
* for the modules directory. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $modulesListOptionName = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* Create new CJTModulesEngine object. |
| 43 |
* |
| 44 |
* @param string Directory to search for modules. |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function __construct($modulesDirectory = 'modules') { |
| 48 |
$this->directory = $modulesDirectory; |
| 49 |
$this->modulesListOptionName = MODULES_LIST_CACHE_VAR_PREFIX . "-{$this->directory}";; |
| 50 |
$this->readModulesList(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Delete module by ID. |
| 55 |
* |
| 56 |
* The deleted module won't never run again |
| 57 |
* until added again by the add method. |
| 58 |
* |
| 59 |
* The module will stay running after deleted however |
| 60 |
* it won't running the next time. |
| 61 |
* |
| 62 |
* @return boolean |
| 63 |
*/ |
| 64 |
public function delete($moduleId) { |
| 65 |
if ($moduleId && is_scalar($moduleId)) { |
| 66 |
if (array_key_exists($moduleId, $this->modulesList)) { |
| 67 |
unset($this->modulesList[$moduleId]); |
| 68 |
update_option($this->modulesListOptionName, $this->modulesList); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Create new CJTModulesEngine object. |
| 75 |
* @see __construct for parameters list. |
| 76 |
* @return CJTModulesEngine |
| 77 |
*/ |
| 78 |
public static function getInstance($modulesDirectory = 'modules') { |
| 79 |
$instance = new CJTModulesEngine($modulesDirectory); |
| 80 |
return $instance; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Process all available modules. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function processAll() { |
| 89 |
// We're that this variable will hold array @see readModulesList called from __construct. |
| 90 |
foreach ($this->modulesList as $moduleId => $module) { |
| 91 |
if (file_exists($module['file'])) { |
| 92 |
// If this method called twice don't desttoy the module |
| 93 |
// object. |
| 94 |
$currentModuleObject = isset($this->modules[$moduleId]) ? $this->modules[$moduleId] : null; |
| 95 |
$mayBeObject = require_once $module['file']; |
| 96 |
$this->modules[$moduleId] = !is_bool($mayBeObject) ? $mayBeObject : $currentModuleObject; |
| 97 |
cssJSToolbox::printDebugMessage("Processing Module: {$moduleId}"); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Read modules list from cache or collect |
| 104 |
* from modules directory is not caches yet. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function readModulesList() { |
| 109 |
$cachedModulesList = get_option($this->modulesListOptionName); |
| 110 |
// Cache modules list only if not cached before. |
| 111 |
// Empty array is another case and it mean |
| 112 |
// that this method is never called before. |
| 113 |
if (!is_array($cachedModulesList)) { |
| 114 |
cssJSToolbox::printDebugMessage("Caching Modules List"); |
| 115 |
$cachedModulesList = array(); |
| 116 |
$modulesDirPath = CJTOOLBOX_PATH . "/{$this->directory}"; |
| 117 |
$modulesIterator = opendir($modulesDirPath); |
| 118 |
// Get all modules. |
| 119 |
while ($moduleDir = readdir($modulesIterator)) { |
| 120 |
// Build module main file path. |
| 121 |
$moduleFileName = "{$moduleDir}.php"; |
| 122 |
$modulePath = "{$modulesDirPath}/{$moduleDir}"; |
| 123 |
if ($moduleDir != '.' && $moduleDir != '..' & is_dir($modulePath)) { |
| 124 |
$moduleFilePath = "{$modulePath}/{$moduleFileName}"; |
| 125 |
$module = array( |
| 126 |
'id' => $moduleDir, |
| 127 |
'path' => $modulePath, |
| 128 |
'url' => CJTOOLBOX_URL . "/modules/{$moduleDir}", |
| 129 |
'file' => $moduleFilePath, |
| 130 |
'status' => 'active', |
| 131 |
); |
| 132 |
$cachedModulesList[$moduleDir] = $module; |
| 133 |
} |
| 134 |
} |
| 135 |
closedir($modulesIterator); |
| 136 |
// Update database modules list. |
| 137 |
update_option($this->modulesListOptionName, $cachedModulesList); |
| 138 |
} |
| 139 |
$this->modulesList = $cachedModulesList; |
| 140 |
} |
| 141 |
|
| 142 |
} // End class. |