Changeset 424 for branches/Modular/Common/Module.php
- Timestamp:
- Oct 10, 2012, 9:29:20 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Modular/Common/Module.php
r405 r424 14 14 var $Models = array(); 15 15 var $SupportedModels = array(); 16 /** @var Database */ 16 17 var $Database; 17 18 var $Installed; 18 var $Initialized; 19 var $System; 19 var $Running; 20 /** @var ModularSystem */ 21 var $ModularSystem; 22 20 23 21 24 function __construct($Database, $System) … … 23 26 $this->Database = &$Database; 24 27 $this->System = &$System; 25 $this-> Initialized= false;28 $this->Running = false; 26 29 $this->Installed = false; 27 30 } … … 29 32 function Install() 30 33 { 31 if($this->I nstalled) return;34 if($this->IsInstalled()) return; 32 35 DebugLog('Installing module '.$this->Name.'...'); 33 36 $this->Installed = true; … … 35 38 // Install dependencies first 36 39 foreach($this->Dependencies as $Dependency) 37 $this->System->Modules[$Dependency]->Install(); 38 39 $this->LoadModels(); 40 foreach($this->Models as $Index => $Module) 41 { 42 $this->Models[$Index]->Install(); 43 } 44 $this->Database->query('UPDATE SystemModule SET Installed=1 WHERE Name="'.$this->Name.'"'); 40 $this->ModularSystem->Modules[$Dependency]->Install(); 41 $this->DoChange(); 45 42 } 46 43 47 44 function UnInstall() 48 45 { 46 if($this->IsInstalled() == false) return; 49 47 DebugLog('Uninstalling module '.$this->Name.'...'); 48 if($this->Running) $this->Stop(); 50 49 $this->Installed = false; 51 50 … … 55 54 if(($Dependency == $this->Name) and ($Module->Installed)) $this->System->Modules[$Module->Name]->UnInstall(); 56 55 56 $this->DoChange(); 57 } 58 59 function IsInstalled() 60 { 61 return($this->Installed); 62 } 63 64 function Start() 65 { 66 if($this->Running) return; 67 if($this->IsInstalled() == false) return; 68 $this->Running = true; 69 foreach($this->Dependencies as $Dependency) 70 $this->System->Modules[$Dependency]->Start(); 57 71 $this->LoadModels(); 58 foreach(array_reverse($this->Models, true) as $Index => $Model) 59 { 60 $this->Models[$Index]->UnInstall(); 61 } 62 $this->Database->query('UPDATE SystemModule SET Installed=0 WHERE Name="'.$this->Name.'"'); 63 } 64 65 function Init() 66 { 67 if($this->Initialized) return; 68 $this->Initialized = true; 69 foreach($this->Dependencies as $Dependency) 70 $this->System->Modules[$Dependency]->Init(); 72 $this->DoChange(); 71 73 } 72 74 75 function Stop() 76 { 77 if(!$this->Running) return; 78 $this->Running = false; 79 foreach($this->System->Modules as $Module) 80 foreach($Module->Dependencies as $Dependency) 81 if(($Dependency == $this->Name) and ($Module->Running)) 82 $this->System->Modules[$Module->Name]->Stop(); 83 $this->DoChange(); 84 } 85 86 private function DoChange() 87 { 88 if($this->ModularSystem->OnModuleChange) 89 call_user_func_array($this->ModularSystem->OnModuleChange, array($this)); 90 } 91 92 function RegisterModel($ModelName) 93 { 94 $this->SupportedModels[] = $ModelName; 95 } 96 97 function UnregisterModel($ModelName) 98 { 99 unset($this->SupportedModels[$ModelName]); 100 } 101 73 102 function LoadModels() 74 103 { … … 85 114 class ModularSystem 86 115 { 116 /** @var Database */ 87 117 var $Database; 88 118 var $Modules = array(); 89 119 var $Models = array(); 90 120 var $Menu = array(); 121 var $OnModuleChange; 91 122 92 123 function __construct($Database) … … 95 126 } 96 127 97 function ModulePresent($Name) 98 { 99 return(array_key_exists($Name, $this->Modules)); 100 } 101 102 function LoadModules($Installed = true) 103 { 104 //DebugLog('Loading modules...'); 105 $this->Modules = array(); 106 $Query = 'SELECT `Id`, `Name`,`Installed` FROM `SystemModule`'; 107 if($Installed) $Query .= ' WHERE `Installed`=1'; 108 else $Query .= ''; // WHERE `Installed`=0'; 109 $DbResult = $this->Database->query($Query); 110 while($Module = $DbResult->fetch_array()) 111 { 112 //echo($Module['Name'].','); 113 include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php'); 114 $ModuleClassName = 'Module'.$Module['Name']; 115 $NewModule = new $ModuleClassName($this->Database, $this); 116 $NewModule->Id = $Module['Id']; 117 $NewModule->Installed = $Module['Installed']; 118 $this->Modules[$Module['Name']] = $NewModule; 119 } 120 } 121 122 function Init() 128 function StartAll() 123 129 { 124 130 $this->LoadModules(); … … 126 132 { 127 133 //DebugLog('Init module '.$Module->Name); 128 $this->Modules[$Index]->Init(); 129 } 130 } 131 134 $this->Modules[$Index]->Start(); 135 } 136 } 137 138 function StopAll() 139 { 140 foreach($this->Modules as $Index => $Module) 141 { 142 //DebugLog('Init module '.$Module->Name); 143 $this->Modules[$Index]->Stop(); 144 } 145 } 146 132 147 function Install() 133 148 { 134 149 //DebugLog('Installing modular system core...'); 135 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (136 `Id` int(11) NOT NULL AUTO_INCREMENT,137 `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,138 `Description` datetime NOT NULL,139 PRIMARY KEY (`Id`)140 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');141 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModule` (142 `Id` int(11) NOT NULL AUTO_INCREMENT,143 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,144 `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,145 `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,146 `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,147 `Installed` int(11) NOT NULL,148 `Description` text COLLATE utf8_czech_ci NOT NULL,149 PRIMARY KEY (`Id`)150 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');151 152 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModuleDependency` (153 `Id` int(11) NOT NULL AUTO_INCREMENT,154 `Module` int(11) NOT NULL,155 `DependencyModule` int(11) NOT NULL,156 PRIMARY KEY (`Id`),157 KEY (`Module`),158 KEY (`DependencyModule`),159 UNIQUE (`Module` , `DependencyModule`)160 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');161 $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');162 $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_2` FOREIGN KEY ( `DependencyModule` ) REFERENCES `SystemModule` (`Id`)');163 164 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModel` (165 `Id` int(11) NOT NULL AUTO_INCREMENT,166 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,167 `Module` int(11) NOT NULL,168 KEY (`Module`),169 PRIMARY KEY (`Id`)170 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');171 $this->Database->query('ALTER TABLE `SystemModel` ADD CONSTRAINT `SystemModel_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');172 173 $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModelProperty` (174 `Id` int(11) NOT NULL AUTO_INCREMENT,175 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,176 `Type` varchar(255) COLLATE utf8_czech_ci NOT NULL,177 `Model` int(11) NOT NULL,178 KEY (`Model`),179 PRIMARY KEY (`Id`)180 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');181 $this->Database->query('ALTER TABLE `SystemModelProperty` ADD CONSTRAINT `SystemModelProperty_ibfk_1` FOREIGN KEY ( `Model` ) REFERENCES `SystemModel` (`Id`)');182 150 183 151 $this->ReloadList(); … … 190 158 } 191 159 192 function UnInstall ()160 function UnInstallAll() 193 161 { 194 162 DebugLog('Uninstalling modular system core...'); 195 foreach(array_reverse($this->Modules, true) as $Index => $Module) 196 $this->Modules[$Index]->UnInstall(); 197 198 // Delete tables with reverse order 199 $this->Database->query('ALTER TABLE `SystemModelProperty` DROP FOREIGN KEY `SystemModelProperty_ibfk_1`'); 200 $this->Database->query('DROP TABLE IF EXISTS `SystemModelProperty`'); 201 $this->Database->query('ALTER TABLE `SystemModel` DROP FOREIGN KEY `SystemModel_ibfk_1`'); 202 $this->Database->query('DROP TABLE IF EXISTS `SystemModel`'); 203 $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_1`'); 204 $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_2`'); 205 $this->Database->query('DROP TABLE IF EXISTS `SystemModuleDependency`'); 206 $this->Database->query('DROP TABLE IF EXISTS `SystemModule`'); 207 $this->Database->query('DROP TABLE IF EXISTS `SystemVersion`'); 208 } 209 210 function IsInstalled() 211 { 212 $DbResult = $this->Database->query('SELECT table_name FROM information_schema.tables 213 WHERE table_schema = "'.$this->Database->Database.'" AND table_name = "SystemVersion";'); 214 if($DbResult->num_rows > 0) return(true); 215 else return(false); 216 } 217 218 function ReloadList() 219 { 220 // Load list of modules from database 221 $Modules = array(); 222 $DbResult = $this->Database->query('SELECT * FROM `SystemModule`'); 223 while($DbRow = $DbResult->fetch_assoc()) 224 $Modules[$DbRow['Name']] = $DbRow; 225 163 foreach($this->Modules as $Index => $Module) 164 { 165 //DebugLog('Init module '.$Module->Name); 166 $this->Modules[$Index]->Uninstall(); 167 } 168 } 169 170 /* @return Module */ 171 function SearchModuleById($Id) 172 { 173 foreach($this->Modules as $Module) 174 { 175 //DebugLog($Module->Name.' '.$Module->Id); 176 if($Module->Id == $Id) return($Module->Name); 177 } 178 return(''); 179 } 180 181 function ModulePresent($Name) 182 { 183 return(array_key_exists($Name, $this->Modules)); 184 } 185 186 function RegisterModule(Module $Module) 187 { 188 $this->Modules[$Module->Name] = &$Module; 189 $Module->ModularSystem = $this; 190 $Module->OnChange = &$this->OnModuleChange; 191 } 192 193 function UnregisterModule($Module) 194 { 195 unset($this->Modules[array_search($Module, $this->Modules)]); 196 } 197 198 function ReloadFromDisk() 199 { 226 200 // Load list of modules on disk 227 201 $ModulesOnDisk = array(); … … 235 209 // Add new 236 210 foreach($ModulesOnDisk as $ModuleName) 237 if(!array_key_exists($ModuleName, $ Modules))238 { 239 DebugLog('Adding module '.$ModuleName.' to list');211 if(!array_key_exists($ModuleName, $this->Modules)) 212 { 213 //DebugLog('Adding module '.$ModuleName.' to list'); 240 214 include_once('Modules/'.$ModuleName.'/'.$ModuleName.'.php'); 241 215 $ModuleClassName = 'Module'.$ModuleName; 242 216 if(class_exists($ModuleClassName)) 243 217 { 244 $Module = new $ModuleClassName($this->Database, $this); 245 $this->Database->insert('SystemModule', array('Name' => $Module->Name, 246 'Version' => $Module->Version, 'Creator' => $Module->Creator, 247 'Description' => $Module->Description, 'License' => $Module->License, 248 'Installed' => 0)); 249 unset($Module); 218 $NewModule = new $ModuleClassName($this->Database, $this); 219 $this->RegisterModule($NewModule); 250 220 } else throw new Exception('Missing class '.$ModuleClassName.' in module '.$ModuleName); 251 } 221 } 252 222 253 // Remove missing254 foreach($Modules as $Module)255 if(($Module['Installed'] == 0) and !in_array($Module['Name'], $ModulesOnDisk))256 {257 DebugLog('Removing module '.$Module['Name'].' from list');258 $this->Database->query('DELETE FROM `SystemModule` WHERE `Id` = '.$Module['Id']);259 }260 261 // Reload dependencies262 $this->LoadModules(false);263 223 foreach($this->Modules as $Module) 264 { 265 foreach($Module->Dependencies as $Dependency) 266 { 267 $this->Database->insert('SystemModuleDependency', array('Module' => $Module->Id, 268 'DependencyModule' => $this->Modules[$Dependency]->Id)); 269 } 270 } 271 } 272 273 function SearchModuleById($Id) 274 { 275 foreach($this->Modules as $Module) 276 { 277 DebugLog($Module->Name.' '.$Module->Id); 278 if($Module->Id == $Id) return($Module->Name); 279 } 280 return(''); 281 } 224 if(array_key_exists($Module->Name, $ModulesOnDisk)) 225 { 226 $this->ModularSystem->UnregisterModule($Module); 227 } 228 } 282 229 } 283 230
Note:
See TracChangeset
for help on using the changeset viewer.