Changeset 592 for trunk/Common/AppModule.php
- Timestamp:
- Nov 2, 2013, 7:56:09 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/AppModule.php
r590 r592 41 41 var $InstalledVersion; 42 42 var $Running; 43 var $Enabled; 43 44 /** @var ModuleType */ 44 45 var $Type; 45 var $Enabled;46 46 var $Dependencies; 47 47 /** @var Database */ … … 70 70 $this->DoInstall(); 71 71 $this->Installed = true; 72 $this->Manager->Modules[$this->Name] = $this; 72 73 } 73 74 … … 77 78 $this->Stop(); 78 79 $this->Installed = false; 80 unset($this->Manager->Modules[$this->Name]); 79 81 $List = array(); 80 82 $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed)); … … 86 88 { 87 89 $this->Uninstall(); 90 // TODO: Install also back dependecies 88 91 $this->Install(); 89 92 } … … 108 111 $this->Manager->Perform($List, array(ModuleAction::Stop), array(ModuleCondition::Running)); 109 112 $this->DoStop(); 110 } 113 } 111 114 112 115 function Restart() … … 116 119 } 117 120 121 function Enable() 122 { 123 if($this->Enabled) return; 124 if(!$this->Installed) return; 125 $List = array(); 126 $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotEnabled)); 127 $this->Manager->Perform($List, array(ModuleAction::Enable), array(ModuleCondition::NotEnabled)); 128 $this->Enabled = true; 129 } 130 131 function Disable() 132 { 133 if(!$this->Enabled) return; 134 $this->Stop(); 135 $this->Enabled = false; 136 $List = array(); 137 $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Enabled)); 138 $this->Manager->Perform($List, array(ModuleAction::Disable), array(ModuleCondition::Enabled)); 139 } 140 118 141 protected function DoStart() 119 142 { … … 121 144 122 145 protected function DoStop() 123 { 124 146 { 125 147 } 126 148 … … 134 156 } 135 157 158 /* Manage installed modules */ 136 159 class AppModuleManager 137 160 { 138 161 var $Modules; 139 var $ModulesAvail;140 162 var $System; 141 var $OnLoadModules; 163 var $Repository; 164 var $FileName; 142 165 143 166 function __construct($System) 144 167 { 145 168 $this->Modules = array(); 146 $this->System = &$System; 169 $this->System = &$System; 170 $this->Repository = new AppModuleRepository($System); 171 $this->Repository->Manager = $this; 172 $this->FileName = 'Config.php'; 147 173 } 148 174 … … 155 181 (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or 156 182 ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or 157 (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions))) 183 (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or 184 ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or 185 (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions))) 158 186 { 159 187 foreach($Actions as $Action) … … 163 191 if($Action == ModuleAction::Install) $Module->Install(); 164 192 if($Action == ModuleAction::Uninstall) $Module->Uninstall(); 193 if($Action == ModuleAction::Enable) $Module->Enable(); 194 if($Action == ModuleAction::Disable) $Module->Disable(); 165 195 } 166 196 } … … 203 233 } 204 234 235 function Start() 236 { 237 $this->Repository->LoadModules(); 238 $this->Load(); 239 $this->StartEnabled(); 240 } 241 205 242 function StartAll() 206 243 { 207 foreach($this->Modules as $Index => $Module) 208 { 209 $this->Modules[$Index]->Start(); 210 } 244 $this->Perform($this->Modules, array(ModuleAction::Start)); 245 } 246 247 function StartEnabled() 248 { 249 $this->Perform($this->Modules, array(ModuleAction::Start), array(ModuleCondition::Enabled)); 211 250 } 212 251 213 252 function StopAll() 214 253 { 215 foreach($this->Modules as $Index => $Module) 216 { 217 $this->Modules[$Index]->Stop(); 218 } 254 $this->Perform($this->Modules, array(ModuleAction::Stop)); 219 255 } 220 256 … … 222 258 { 223 259 return(array_key_exists($Name, $this->Modules)); 224 }225 226 function RegisterModule(AppModule $Module)227 {228 $this->Modules[$Module->Name] = &$Module;229 $Module->Manager = &$this;230 $Module->OnChange = &$this->OnModuleChange;231 }232 233 function UnregisterModule($Module)234 {235 unset($this->Modules[array_search($Module, $this->Modules)]);236 260 } 237 261 … … 246 270 return(''); 247 271 } 272 273 function Load() 274 { 275 include($this->FileName); 276 $this->Modules = array(); 277 foreach($ConfigModules as $Mod) 278 { 279 if(array_key_exists($Mod['Name'], $this->Repository->Modules)) 280 { 281 $this->Modules[$Mod['Name']] = $this->Repository->Modules[$Mod['Name']]; 282 $this->Modules[$Mod['Name']]->Enabled = $Mod['Enabled']; 283 $this->Modules[$Mod['Name']]->Version = $Mod['Version']; 284 } 285 } 286 } 287 288 function Save() 289 { 290 $Data = array(); 291 foreach($this->Modules as $Module) 292 { 293 $Data[] = array('Name' => $Module->Name, 'Enabled' => $Module->Enabled, 294 'Version' => $Module->Version); 295 } 296 file_put_contents($this->FileName, "<?php \n\n\$ConfigModules = ".var_export($Data).";\n"); 297 } 298 } 299 300 /* Manager available modules for installation */ 301 class AppModuleRepository 302 { 303 var $Modules; 304 var $System; 305 var $Manager; 306 var $OnLoadModules; 307 308 function __construct($System) 309 { 310 $this->Modules = array(); 311 $this->System = &$System; 312 } 313 314 function RegisterModule(AppModule $Module) 315 { 316 $this->Modules[$Module->Name] = &$Module; 317 $Module->Manager = &$this->Manager; 318 $Module->OnChange = &$this->OnModuleChange; 319 } 320 321 function UnregisterModule($Module) 322 { 323 unset($this->Modules[array_search($Module, $this->Modules)]); 324 } 248 325 249 326 function LoadModulesFromDir($Directory) 250 327 { 251 252 253 254 255 {256 257 258 259 260 328 $List = scandir($Directory); 329 foreach($List as $Item) 330 { 331 if(is_dir($Directory.'/'.$Item) and ($Item != '.') and ($Item != '..') and ($Item != '.svn')) 332 { 333 include_once($Directory.'/'.$Item.'/'.$Item.'.php'); 334 $ModuleName = 'Module'.$Item; 335 $this->RegisterModule(new $ModuleName($this->System)); 336 } 337 } 261 338 } 262 339 263 340 function LoadModules() 264 341 { 265 if(method_exists($this->OnLoadModules[0], $this->OnLoadModules[1])) 266 $this->OnLoadModules(); 267 else $this->LoadModulesFromDir(dirname(__FILE__).'/../Modules'); 268 } 269 } 342 if(method_exists($this->OnLoadModules[0], $this->OnLoadModules[1])) 343 $this->OnLoadModules(); 344 else $this->LoadModulesFromDir(dirname(__FILE__).'/../Modules'); 345 } 346 } 347
Note:
See TracChangeset
for help on using the changeset viewer.