Changeset 899 for trunk/Packages/Common
- Timestamp:
- Feb 17, 2021, 12:30:23 PM (4 years ago)
- Location:
- trunk/Packages/Common
- Files:
-
- 5 added
- 4 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Base.php
r898 r899 1 1 <?php 2 3 class System4 {5 public Database $Database;6 7 function __construct()8 {9 $this->Database = new Database();10 }11 }12 2 13 3 class Base 14 4 { 15 public Application$System;5 public System $System; 16 6 public Database $Database; 17 7 … … 27 17 } 28 18 } 29 30 class Model extends Base31 {32 }33 34 class View extends Base35 {36 }37 38 class Controller extends Base39 {40 }41 42 class ModelDesc43 {44 public string $Name;45 public array $Columns;46 public array $Indices;47 public string $PrimaryKey;48 public bool $Memory;49 50 function __construct(string $Name)51 {52 $this->Name = $Name;53 $this->Columns = array();54 $this->Indices = array();55 $this->PrimaryKey = 'Id';56 $this->Memory = false;57 }58 59 function AddString(string $Name): ModelColumnString60 {61 $Result = new ModelColumnString($Name);62 $this->Columns[] = $Result;63 return $Result;64 }65 66 function AddFloat(string $Name): ModelColumnFloat67 {68 $Result = new ModelColumnFloat($Name);69 $this->Columns[] = $Result;70 return $Result;71 }72 73 function AddText(string $Name): ModelColumnText74 {75 $Result = new ModelColumnText($Name);76 $this->Columns[] = $Result;77 return $Result;78 }79 80 function AddInteger(string $Name): ModelColumnInteger81 {82 $Result = new ModelColumnInteger($Name);83 $this->Columns[] = $Result;84 return $Result;85 }86 87 function AddBigInt(string $Name): ModelColumnBigInt88 {89 $Result = new ModelColumnBigInt($Name);90 $this->Columns[] = $Result;91 return $Result;92 }93 94 function AddDateTime(string $Name): ModelColumnDateTime95 {96 $Result = new ModelColumnDateTime($Name);97 $this->Columns[] = $Result;98 return $Result;99 }100 101 function AddDate(string $Name): ModelColumnDate102 {103 $Result = new ModelColumnDate($Name);104 $this->Columns[] = $Result;105 return $Result;106 }107 108 function AddBoolean(string $Name): ModelColumnBoolean109 {110 $Result = new ModelColumnBoolean($Name);111 $this->Columns[] = $Result;112 return $Result;113 }114 115 function AddReference(string $Name, string $RefTable, bool $Nullable = true): ModelColumnReference116 {117 $Result = new ModelColumnReference($Name, $RefTable);118 $this->Columns[] = $Result;119 $Result->Nullable = $Nullable;120 return $Result;121 }122 123 function AddEnum(string $Name, array $States): ModelColumnEnum124 {125 $Result = new ModelColumnEnum($Name, $States);126 $this->Columns[] = $Result;127 return $Result;128 }129 130 function AddChangeAction(): void131 {132 $Column = $this->AddEnum('ChangeAction', array('add', 'modify', 'remove'));133 $Column->Nullable = true;134 $Column = $this->AddDateTime('ChangeTime');135 $Column->Nullable = true;136 $this->AddInteger('ChangeReplaceId');137 }138 }139 140 class ModelColumnType141 {142 const Integer = 0;143 const String = 1;144 const Float = 2;145 const Text = 3;146 const DateTime = 4;147 const Reference = 5;148 const Boolean = 6;149 const Date = 7;150 const Enum = 8;151 const BigInt = 9;152 153 static function GetName(int $Index)154 {155 $Text = array('Integer', 'String', 'Float', 'Text', 'DateTime', 'Reference', 'Boolean', 'Date', 'Enum', 'BigInt');156 return $Text[$Index];157 }158 }159 160 class ModelColumn161 {162 public string $Name;163 public int $Type; // ModelColumnType164 public bool $Nullable;165 public bool $Unique;166 public bool $HasDefault;167 168 function __construct(string $Name, int $Type, bool $Nullable = false, bool $Unique = false)169 {170 $this->Name = $Name;171 $this->Type = $Type;172 $this->Nullable = $Nullable;173 $this->Unique = $Unique;174 $this->HasDefault = false;175 }176 177 function GetDefault(): ?string178 {179 return null;180 }181 }182 183 class ModelColumnString extends ModelColumn184 {185 public ?string $Default;186 187 function __construct(string $Name)188 {189 parent::__construct($Name, ModelColumnType::String);190 $this->HasDefault = false;191 $this->Default = null;192 }193 194 function GetDefault(): ?string195 {196 return '"'.$this->Default.'"';197 }198 }199 200 class ModelColumnFloat extends ModelColumn201 {202 public ?float $Default;203 204 function __construct(string $Name)205 {206 parent::__construct($Name, ModelColumnType::Float);207 $this->HasDefault = false;208 $this->Default = null;209 }210 211 function GetDefault(): ?string212 {213 return '"'.$this->Default.'"';214 }215 }216 217 class ModelColumnText extends ModelColumn218 {219 public ?string $Default;220 221 function __construct(string $Name)222 {223 parent::__construct($Name, ModelColumnType::Text);224 $this->HasDefault = false;225 $this->Default = null;226 }227 228 function GetDefault(): ?string229 {230 return '"'.$this->Default.'"';231 }232 }233 234 class ModelColumnInteger extends ModelColumn235 {236 public ?int $Default;237 238 function __construct(string $Name)239 {240 parent::__construct($Name, ModelColumnType::Integer);241 $this->HasDefault = false;242 $this->Default = null;243 }244 245 function GetDefault(): ?string246 {247 return $this->Default;248 }249 }250 251 class ModelColumnBigInt extends ModelColumn252 {253 public ?int $Default;254 255 function __construct(string $Name)256 {257 parent::__construct($Name, ModelColumnType::BigInt);258 $this->HasDefault = false;259 $this->Default = null;260 }261 262 function GetDefault(): ?string263 {264 return $this->Default;265 }266 }267 268 class ModelColumnDateTime extends ModelColumn269 {270 public ?DateTime $Default;271 272 function __construct(string $Name)273 {274 parent::__construct($Name, ModelColumnType::DateTime);275 $this->HasDefault = false;276 $this->Default = null;277 }278 279 function GetDefault(): ?string280 {281 return '"'.$this->Default.'"';282 }283 }284 285 class ModelColumnDate extends ModelColumn286 {287 public ?DateTime $Default;288 289 function __construct(string $Name)290 {291 parent::__construct($Name, ModelColumnType::Date);292 $this->HasDefault = false;293 $this->Default = null;294 }295 296 function GetDefault(): ?string297 {298 return '"'.$this->Default.'"';299 }300 }301 302 class ModelColumnReference extends ModelColumn303 {304 public string $RefTable;305 306 function __construct(string $Name, string $RefTable)307 {308 parent::__construct($Name, ModelColumnType::Reference);309 $this->RefTable = $RefTable;310 }311 }312 313 class ModelColumnBoolean extends ModelColumn314 {315 public ?bool $Default;316 317 function __construct(string $Name)318 {319 parent::__construct($Name, ModelColumnType::Boolean);320 $this->HasDefault = false;321 $this->Default = null;322 }323 324 function GetDefault(): ?string325 {326 return $this->Default;327 }328 }329 330 class ModelColumnEnum extends ModelColumn331 {332 public ?bool $Default;333 public array $States;334 335 function __construct(string $Name, array $States)336 {337 parent::__construct($Name, ModelColumnType::Enum);338 $this->HasDefault = false;339 $this->Default = null;340 $this->States = $States;341 }342 343 function GetDefault(): ?string344 {345 return $this->Default;346 }347 } -
trunk/Packages/Common/Common.php
r897 r899 11 11 include_once(dirname(__FILE__).'/Error.php'); 12 12 include_once(dirname(__FILE__).'/Base.php'); 13 include_once(dirname(__FILE__).'/Application.php'); 14 include_once(dirname(__FILE__).'/AppModule.php'); 13 include_once(dirname(__FILE__).'/View.php'); 14 include_once(dirname(__FILE__).'/Model.php'); 15 include_once(dirname(__FILE__).'/ModelDesc.php'); 16 include_once(dirname(__FILE__).'/Controller.php'); 17 include_once(dirname(__FILE__).'/System.php'); 18 include_once(dirname(__FILE__).'/Module.php'); 19 include_once(dirname(__FILE__).'/ModuleManager.php'); 15 20 include_once(dirname(__FILE__).'/Config.php'); 16 21 include_once(dirname(__FILE__).'/Page.php'); … … 40 45 $this->ReleaseDate = strtotime('2020-03-29'); 41 46 $this->Creator = 'Chronos'; 42 $this->License = 'GNU/GPL ';47 $this->License = 'GNU/GPLv3'; 43 48 $this->Homepage = 'https://svn.zdechov.net/PHPlib/Common/'; 44 49 } -
trunk/Packages/Common/Module.php
r898 r899 1 1 <?php 2 3 /* This implementation will not support installation from remote source. Just4 * installation of already presented modules mainly to persistence preparation.5 */6 2 7 3 class ModuleType … … 37 33 } 38 34 39 class AppModule35 class Module extends Model 40 36 { 41 37 public int $Id; … … 47 43 public string $HomePage; 48 44 public string $Description; 45 public int $Type; // ModuleType 46 public array $Dependencies; 47 public array $Models; // Model 49 48 public bool $Running; 50 49 public bool $Enabled; 51 50 public bool $Installed; 52 51 public string $InstalledVersion; 53 public int $Type;54 public array $Dependencies;55 52 public Database $Database; 56 53 public System $System; 57 public AppModuleManager $Manager;54 public ModuleManager $Manager; 58 55 public $OnChange; 59 public array $Models; 60 61 function __construct(Application $System) 62 { 63 $this->System = &$System; 64 $this->Database = &$System->Database; 65 $this->Installed = false; 66 $this->InstalledVersion = ''; 67 $this->Enabled = false; 68 $this->Running = false; 56 57 function __construct(System $System) 58 { 59 parent::__construct($System); 60 $this->Name = ''; 69 61 $this->HomePage = ''; 70 62 $this->License = ''; … … 74 66 $this->Description = ''; 75 67 $this->Dependencies = array(); 68 $this->Models = array(); 76 69 $this->Type = ModuleType::Library; 77 $this->Models = array(); 78 } 79 80 function GetModels(): array 81 { 82 return array(); 70 $this->Installed = false; 71 $this->InstalledVersion = ''; 72 $this->Enabled = false; 73 $this->Running = false; 74 } 75 76 static function GetName() 77 { 78 $ClassName = get_called_class(); 79 if (substr($ClassName, 0, 6) == 'Module') return substr($ClassName, 6); 80 else return $ClassName; 81 } 82 83 static function GetModelDesc(): ModelDesc 84 { 85 $Desc = new ModelDesc(self::GetClassName()); 86 $Desc->AddString('Name'); 87 $Desc->AddString('Title'); 88 $Desc->AddString('Creator'); 89 $Desc->AddString('Version'); 90 $Desc->AddString('License'); 91 $Desc->AddBoolean('Installed'); 92 $Desc->AddString('InstalledVersion'); 93 $Desc->AddString('Description'); 94 return $Desc; 83 95 } 84 96 … … 86 98 { 87 99 if ($this->Installed) return; 88 echo('Install mod '.$this->Name.'<br/>');89 100 $List = array(); 90 101 $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotInstalled)); … … 99 110 call_user_func($this->Manager->OnInstallModule, $this); 100 111 } 101 $this->InstallModels( );112 $this->InstallModels($this->Models); 102 113 $this->DoInstall(); 103 114 } … … 112 123 $this->Manager->PerformList($List, array(ModuleAction::Uninstall), array(ModuleCondition::Installed)); 113 124 $this->DoUninstall(); 114 $this->UninstallModels( );125 $this->UninstallModels($this->Models); 115 126 if ($this->Manager->OnUninstallModule != null) 116 127 { … … 218 229 } 219 230 220 function InstallModels( ): void231 function InstallModels(array $Models): void 221 232 { 222 233 if ($this->Manager->OnInstallModel != null) 223 234 { 224 foreach ($ this->GetModels()as $Model)235 foreach ($Models as $Model) 225 236 { 226 call_user_func($this->Manager->OnInstallModel, $Model::Get Desc(), $this);237 call_user_func($this->Manager->OnInstallModel, $Model::GetModelDesc(), $this); 227 238 } 228 239 } 229 240 } 230 241 231 function UninstallModels( ): void242 function UninstallModels(array $Models): void 232 243 { 233 244 if ($this->Manager->OnUninstallModel != null) 234 245 { 235 foreach (array_reverse($ this->GetModels()) as $Model)246 foreach (array_reverse($Models) as $Model) 236 247 { 237 call_user_func($this->Manager->OnUninstallModel, $Model::Get Desc(), $this);248 call_user_func($this->Manager->OnUninstallModel, $Model::GetModelDesc(), $this); 238 249 } 239 250 } 240 251 } 241 252 } 242 243 /* Manage installed modules */244 class AppModuleManager245 {246 public array $Modules;247 public System $System;248 public string $FileName;249 public string $ModulesDir;250 public $OnLoadModules;251 public $OnInstallModel;252 public $OnUninstsallModel;253 public $OnInstallModule;254 public $OnUninstsallModule;255 256 function __construct(System $System)257 {258 $this->Modules = array();259 $this->System = &$System;260 $this->FileName = dirname(__FILE__).'/../../Config/ModulesConfig.php';261 $this->ModulesDir = dirname(__FILE__).'/../../Modules';262 $this->OnInstallModel = null;263 $this->OnUninstallModel = null;264 $this->OnInstallModule = null;265 $this->OnUninstallModule = null;266 }267 268 function Perform(array $Actions, array $Conditions = array(ModuleCondition::All)): void269 {270 $this->PerformList($this->Modules, $Actions, $Conditions);271 }272 273 function PerformList(array $List, array $Actions, array $Conditions = array(ModuleCondition::All)): void274 {275 foreach ($List as $Module)276 {277 if (in_array(ModuleCondition::All, $Conditions) or278 ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or279 (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or280 ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or281 (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or282 ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or283 (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or284 (($Module->Type == ModuleType::System) and in_array(ModuleCondition::System, $Conditions)) or285 (($Module->Type == ModuleType::Application) and in_array(ModuleCondition::Application, $Conditions)) or286 (($Module->Type == ModuleType::Library) and in_array(ModuleCondition::Library, $Conditions)))287 {288 foreach ($Actions as $Action)289 {290 if ($Action == ModuleAction::Start) $Module->Start();291 if ($Action == ModuleAction::Stop) $Module->Stop();292 if ($Action == ModuleAction::Install) $Module->Install();293 if ($Action == ModuleAction::Uninstall) $Module->Uninstall();294 if ($Action == ModuleAction::Enable) $Module->Enable();295 if ($Action == ModuleAction::Disable) $Module->Disable();296 if ($Action == ModuleAction::Upgrade) $Module->Upgrade();297 }298 }299 }300 }301 302 function EnumDependenciesCascade(AppModule $Module, array &$List, array $Conditions = array(ModuleCondition::All))303 {304 foreach ($Module->Dependencies as $Dependency)305 {306 if (!array_key_exists($Dependency, $this->Modules))307 throw new Exception(sprintf(T('Module "%s" dependency "%s" not found'), $Module->Name, $Dependency));308 $DepModule = $this->Modules[$Dependency];309 if (in_array(ModuleCondition::All, $Conditions) or310 ($DepModule->Running and in_array(ModuleCondition::Running, $Conditions)) or311 (!$DepModule->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or312 ($DepModule->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or313 (!$DepModule->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or314 ($DepModule->Installed and in_array(ModuleCondition::Installed, $Conditions)) or315 (!$DepModule->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or316 (($Module->Type == ModuleType::System) and in_array(ModuleCondition::System, $Conditions)) or317 (($Module->Type == ModuleType::Application) and in_array(ModuleCondition::Application, $Conditions)) or318 (($Module->Type == ModuleType::Library) and in_array(ModuleCondition::Library, $Conditions)))319 {320 array_push($List, $DepModule);321 $this->EnumDependenciesCascade($DepModule, $List, $Conditions);322 }323 }324 }325 326 function EnumSuperiorDependenciesCascade(AppModule $Module, array &$List, array $Conditions = array(ModuleCondition::All))327 {328 foreach ($this->Modules as $RefModule)329 {330 if (in_array($Module->Name, $RefModule->Dependencies) and331 (in_array(ModuleCondition::All, $Conditions) or332 ($RefModule->Running and in_array(ModuleCondition::Running, $Conditions)) or333 (!$RefModule->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or334 ($RefModule->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or335 (!$RefModule->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or336 ($RefModule->Installed and in_array(ModuleCondition::Installed, $Conditions)) or337 (!$RefModule->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or338 (($Module->Type == ModuleType::System) and in_array(ModuleCondition::System, $Conditions)) or339 (($Module->Type == ModuleType::Application) and in_array(ModuleCondition::Application, $Conditions)) or340 (($Module->Type == ModuleType::Library) and in_array(ModuleCondition::Library, $Conditions))))341 {342 array_push($List, $RefModule);343 $this->EnumSuperiorDependenciesCascade($RefModule, $List, $Conditions);344 }345 }346 }347 348 function StartAll(array $Conditions = array(ModuleCondition::All)): void349 {350 $this->Perform(array(ModuleAction::Start), $Conditions);351 }352 353 function StopAll(array $Conditions = array(ModuleCondition::All)): void354 {355 $this->Perform(array(ModuleAction::Stop), $Conditions);356 }357 358 function InstallAll(array $Conditions = array(ModuleCondition::All)): void359 {360 $this->Perform(array(ModuleAction::Install), $Conditions);361 $this->SaveState();362 }363 364 function UninstallAll(array $Conditions = array(ModuleCondition::All)): void365 {366 $this->Perform(array(ModuleAction::Uninstall), $Conditions);367 $this->SaveState();368 }369 370 function EnableAll(array $Conditions = array(ModuleCondition::All)): void371 {372 $this->Perform(array(ModuleAction::Enable), $Conditions);373 $this->SaveState();374 }375 376 function DisableAll(array $Conditions = array(ModuleCondition::All)): void377 {378 $this->Perform(array(ModuleAction::Disable), $Conditions);379 $this->SaveState();380 }381 382 function UpgradeAll(array $Conditions = array(ModuleCondition::All)): void383 {384 $this->Perform(array(ModuleAction::Upgrade), $Conditions);385 $this->SaveState();386 }387 388 function ModulePresent(string $Name): bool389 {390 return array_key_exists($Name, $this->Modules);391 }392 393 function ModuleEnabled(string $Name): bool394 {395 return array_key_exists($Name, $this->Modules) and $this->Modules[$Name]->Enabled;396 }397 398 function ModuleRunning(string $Name): bool399 {400 return array_key_exists($Name, $this->Modules) and $this->Modules[$Name]->Running;401 }402 403 function GetModule(string $Name): AppModule404 {405 return $this->Modules[$Name];406 }407 408 function SearchModuleById(int $Id): string409 {410 foreach ($this->Modules as $Module)411 {412 if ($Module->Id == $Id) return $Module->Name;413 }414 return '';415 }416 417 function LoadState(): void418 {419 $ConfigModules = array();420 include $this->FileName;421 foreach ($ConfigModules as $Mod)422 {423 $ModuleName = $Mod['Name'];424 if (array_key_exists($ModuleName, $this->Modules))425 {426 if (array_key_exists('Enabled', $Mod))427 {428 $this->Modules[$ModuleName]->Enabled = $Mod['Enabled'];429 }430 if (array_key_exists('Installed', $Mod))431 {432 $this->Modules[$ModuleName]->Installed = $Mod['Installed'];433 }434 if (array_key_exists('InstalledVersion', $Mod))435 {436 $this->Modules[$ModuleName]->InstalledVersion = $Mod['InstalledVersion'];437 }438 }439 }440 }441 442 function SaveState(): void443 {444 $Data = array();445 foreach ($this->Modules as $Module)446 {447 $Data[] = array(448 'Name' => $Module->Name,449 'Enabled' => $Module->Enabled,450 'InstalledVersion' => $Module->InstalledVersion,451 'Installed' => $Module->Installed452 );453 }454 if (file_put_contents($this->FileName, "<?php \n\n\$ConfigModules = ".var_export($Data, true).";\n") === FALSE)455 {456 echo($this->FileName.' is not writeable.');457 }458 }459 460 function RegisterModule(AppModule $Module): void461 {462 $this->Modules[$Module->Name] = &$Module;463 $Module->Manager = &$this;464 $Module->OnChange = &$this->OnModuleChange;465 }466 467 function UnregisterModule(AppModule $Module): void468 {469 unset($this->Modules[array_search($Module, $this->Modules)]);470 }471 472 function GetUniqueModuleId()473 {474 $Id = 1;475 foreach ($this->Modules as $Module)476 {477 if ($Module->Id >= $Id) $Id = $Module->Id + 1;478 }479 return $Id;480 }481 482 function LoadModule(string $FileName): AppModule483 {484 include_once($FileName);485 $ModuleName = 'Module'.pathinfo($FileName, PATHINFO_FILENAME);486 $Module = new $ModuleName($this->System);487 $Module->Id = $this->GetUniqueModuleId();488 $this->RegisterModule($Module);489 return $Module;490 }491 492 function LoadModulesFromDir(string $Directory): void493 {494 $List = scandir($Directory);495 foreach ($List as $Item)496 {497 if (is_dir($Directory.'/'.$Item) and ($Item != '.') and ($Item != '..') and ($Item != '.svn'))498 {499 $this->LoadModule($Directory.'/'.$Item.'/'.$Item.'.php');500 }501 }502 }503 504 function LoadModules(): void505 {506 if (is_array($this->OnLoadModules) and (count($this->OnLoadModules) == 2) and method_exists($this->OnLoadModules[0], $this->OnLoadModules[1]))507 call_user_func($this->OnLoadModules);508 else $this->LoadModulesFromDir($this->ModulesDir);509 }510 } -
trunk/Packages/Common/Modules/ModuleManager.php
r898 r899 1 1 <?php 2 2 3 class ModuleModuleManager extends AppModule3 class ModuleModuleManager extends Module 4 4 { 5 5 public UpdateManager $UpdateManager; … … 13 13 $this->License = 'GNU/GPLv3'; 14 14 $this->Description = 'User interface for module management.'; 15 $this->Dependencies = array( 'Setup');15 $this->Dependencies = array(ModuleSetup::GetName()); 16 16 $this->Revision = 1; 17 17 $this->Type = ModuleType::System; 18 $this->Models = array(ModelField::GetClassName(), ModuleDependency::GetClassName()); 18 19 } 19 20 … … 142 143 function DoBeforeInstall(): void 143 144 { 144 $this->AddModelDatabase(Module::Get Desc());145 $this->AddModelDatabase(Module::GetModelDesc()); 145 146 $this->Manager->OnInstallModule = array($this, 'InstallModule'); 146 147 $this->Manager->OnUninstallModule = array($this, 'UninstallModule'); 147 $this->AddModelDatabase(Mod uleModel::GetDesc());148 $this->AddModelDatabase(Model::GetModelDesc()); 148 149 $this->Manager->OnInstallModel = array($this, 'InstallModel'); 149 150 $this->Manager->OnUninstallModel = array($this, 'UninstallModel'); … … 154 155 $this->Manager->OnInstallModel = null; 155 156 $this->Manager->OnUninstallModel = null; 156 $this->RemoveModelDatabase(Mod uleModel::GetDesc());157 $this->RemoveModelDatabase(Model::GetModelDesc()); 157 158 $this->Manager->OnInstallModule = null; 158 159 $this->Manager->OnUninstallModule = null; 159 $this->RemoveModelDatabase(Module::GetDesc()); 160 } 161 162 function GetModels(): array 163 { 164 return array(ModuleModelField::GetClassName(), ModuleDependency::GetClassName()); 160 $this->RemoveModelDatabase(Module::GetModelDesc()); 161 } 162 163 function DoInstall(): void 164 { 165 $this->InstallModel(Module::GetModelDesc(), $this->System->GetModule($this::GetName())); 166 $this->InstallModel(Model::GetModelDesc(), $this->System->GetModule($this::GetName())); 165 167 } 166 168 … … 238 240 } 239 241 240 function InstallModel(ModelDesc $ModelDesc, AppModule $Module)242 function InstallModel(ModelDesc $ModelDesc, Module $Module) 241 243 { 242 244 $this->AddModelDatabase($ModelDesc); 243 $this->Database->insert('Mod uleModel', array('Name' => $ModelDesc->Name, 'Module' => $Module->Id));245 $this->Database->insert('Model', array('Name' => $ModelDesc->Name, 'Module' => $Module->Id)); 244 246 $ModelId = $this->Database->insert_id; 245 247 foreach ($ModelDesc->Columns as $Field) 246 248 { 247 $this->Database->insert('Mod uleModelField', array('Name' => $Field->Name, 'Model' => $ModelId,249 $this->Database->insert('ModelField', array('Name' => $Field->Name, 'Model' => $ModelId, 248 250 'Type' => ModelColumnType::GetName($Field->Type), 'Nullable' => (int)$Field->Nullable)); 249 251 } 250 252 } 251 253 252 function UninstallModel(ModelDesc $ModelDesc, AppModule $Module) 253 { 254 $this->Database->delete('ModuleModel', '(Name='.$ModelDesc->Name.') AND (Module='.$Module->Id.')'); 254 function UninstallModel(ModelDesc $ModelDesc, Module $Module) 255 { 256 $DbResult = $this->Database->select('Model', 'Id', '(Name="'.$ModelDesc->Name.'") AND (Module='.$Module->Id.')'); 257 if ($DbResult->num_rows > 0) 258 { 259 $DbRow = $DbResult->fetch_assoc(); 260 $ModelId = $DbRow['Id']; 261 $DbResult = $this->Database->delete('ModelField', '(Model='.$ModelId.')'); 262 } 263 $this->Database->delete('Model', '(Id='.$ModelId.')'); 255 264 $this->RemoveModelDatabase($ModelDesc); 256 265 } 257 266 258 function InstallModule(AppModule $Module) 259 { 260 echo('Install module '.$Module->Name); 267 function InstallModule(Module $Module) 268 { 261 269 $this->Database->insert('Module', array( 270 'Id' => $Module->Id, 262 271 'Name' => $Module->Name, 263 272 'Title' => $Module->Title, … … 269 278 'InstalledVersion' => $Module->InstalledVersion 270 279 )); 271 $Module->Id = $this->Database->insert_id; 272 } 273 274 function UninstallModule(AppModule $Module) 275 { 276 echo('Uninstall module '.$Module->Name); 277 $this->Database->delete('Module', 'Name='.$Module->Name); 278 } 279 280 function ModuleChange(AppModule $Module): void 280 } 281 282 function UninstallModule(Module $Module) 283 { 284 $this->Database->delete('Module', 'Name="'.$Module->Name.'"'); 285 } 286 287 function ModuleChange(Module $Module): void 281 288 { 282 289 if ($Module->Installed) $Installed = 1; … … 293 300 while ($Module = $DbResult->fetch_array()) 294 301 { 295 //echo($Module['Name'].',');296 302 include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php'); 297 303 $ModuleClassName = 'Module'.$Module['Name']; … … 534 540 } 535 541 536 class Mod uleextends Model542 class ModelField extends Model 537 543 { 538 static function Get Desc(): ModelDesc544 static function GetModelDesc(): ModelDesc 539 545 { 540 546 $Desc = new ModelDesc(self::GetClassName()); 541 547 $Desc->AddString('Name'); 542 $Desc->AddString('Title'); 543 $Desc->AddString('Creator'); 544 $Desc->AddString('Version'); 545 $Desc->AddString('License'); 546 $Desc->AddBoolean('Installed'); 547 $Desc->AddString('InstalledVersion'); 548 $Desc->AddString('Description'); 549 return $Desc; 550 } 551 } 552 553 class ModuleModel extends Model 554 { 555 static function GetDesc(): ModelDesc 556 { 557 $Desc = new ModelDesc(self::GetClassName()); 558 $Desc->AddString('Name'); 559 $Desc->AddReference('Module', Module::GetClassName()); 560 return $Desc; 561 } 562 } 563 564 class ModuleModelField extends Model 565 { 566 static function GetDesc(): ModelDesc 567 { 568 $Desc = new ModelDesc(self::GetClassName()); 569 $Desc->AddString('Name'); 570 $Desc->AddReference('Model', ModuleModel::GetClassName()); 548 $Desc->AddReference('Model', Model::GetClassName()); 571 549 $Desc->AddString('Type'); 572 550 $Desc->AddBoolean('Nullable'); … … 577 555 class ModuleDependency extends Model 578 556 { 579 static function Get Desc(): ModelDesc557 static function GetModelDesc(): ModelDesc 580 558 { 581 559 $Desc = new ModelDesc(self::GetClassName()); -
trunk/Packages/Common/Modules/Setup.php
r897 r899 1 1 <?php 2 3 class ModuleSetup extends Module 4 { 5 public UpdateManager $UpdateManager; 6 7 function __construct(System $System) 8 { 9 global $DatabaseRevision; 10 11 parent::__construct($System); 12 $this->Name = 'Setup'; 13 $this->Version = '1.0'; 14 $this->Creator = 'Chronos'; 15 $this->License = 'GNU/GPLv3'; 16 $this->Description = 'Base setup module'; 17 $this->Revision = 1; 18 $this->Type = ModuleType::System; 19 20 // Check database persistence structure 21 $this->UpdateManager = new UpdateManager(); 22 $this->UpdateManager->Database = &$this->Database; 23 $this->UpdateManager->Revision = $DatabaseRevision; 24 $this->UpdateManager->InstallMethod = 'FullInstall'; 25 } 26 27 static function Cast(Module $Module): ModuleSetup 28 { 29 if ($Module instanceof ModuleSetup) 30 { 31 return $Module; 32 } 33 throw new Exception('Expected ModuleSetup type but '.gettype($Module)); 34 } 35 36 function DoStart(): void 37 { 38 Core::Cast($this->System)->RegisterPage([''], 'PageSetupRedirect'); 39 Core::Cast($this->System)->RegisterPage(['setup'], 'PageSetup'); 40 } 41 42 function DoStop(): void 43 { 44 unset($this->UpdateManager); 45 Core::Cast($this->System)->UnregisterPage(['']); 46 Core::Cast($this->System)->UnregisterPage(['setup']); 47 } 48 49 function CheckState(): bool 50 { 51 return $this->Database->Connected() and $this->UpdateManager->IsInstalled() and 52 $this->UpdateManager->IsUpToDate(); 53 } 54 55 function DoUpgrade(): string 56 { 57 $Updates = new Updates(); 58 $this->UpdateManager->Trace = $Updates->Get(); 59 $Output = $this->UpdateManager->Upgrade(); 60 return $Output; 61 } 62 63 function InsertSampleData(): void 64 { 65 } 66 } 2 67 3 68 class PageSetup extends Page … … 111 176 { 112 177 $Output .= '<h3>Instalace systém</h3>'; 178 global $DatabaseRevision; 179 180 $this->Database->query("CREATE TABLE IF NOT EXISTS `".$this->UpdateManager->VersionTable."` (". 181 '`Id` int(11) NOT NULL AUTO_INCREMENT, '. 182 '`Revision` int(11) NOT NULL, '. 183 'PRIMARY KEY (`Id`) '. 184 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'); 185 $DbResult = $this->Database->select($this->UpdateManager->VersionTable, 'Id'); 186 if ($DbResult->num_rows == 0) 187 { 188 $this->Database->query("INSERT INTO `".$this->UpdateManager->VersionTable. 189 "` (`Id`, `Revision`) VALUES (1, ".$DatabaseRevision.");"); 190 } 113 191 $this->System->ModuleManager->InstallAll(array(ModuleCondition::System)); 114 192 $this->System->ModuleManager->LoadModules(); … … 121 199 $Output .= '<h3>Odinstalace vše</h3>'; 122 200 $this->System->ModuleManager->UninstallAll(array(ModuleCondition::System)); 201 $this->Database->query('DROP TABLE IF EXISTS `'.$this->UpdateManager->VersionTable.'`'); 123 202 $Output .= $this->ControlPanel(); 124 203 } … … 296 375 } 297 376 } 298 299 class ModuleSetup extends AppModule300 {301 public UpdateManager $UpdateManager;302 303 function __construct(System $System)304 {305 global $DatabaseRevision;306 307 parent::__construct($System);308 $this->Name = 'Setup';309 $this->Version = '1.0';310 $this->Creator = 'Chronos';311 $this->License = 'GNU/GPLv3';312 $this->Description = 'Base setup module';313 $this->Dependencies = array();314 $this->Revision = 1;315 $this->Type = ModuleType::System;316 317 // Check database persistence structure318 $this->UpdateManager = new UpdateManager();319 $this->UpdateManager->Database = &$this->Database;320 $this->UpdateManager->Revision = $DatabaseRevision;321 $this->UpdateManager->InstallMethod = 'FullInstall';322 }323 324 static function Cast(AppModule $AppModule): ModuleSetup325 {326 if ($AppModule instanceof ModuleSetup)327 {328 return $AppModule;329 }330 throw new Exception('Expected ModuleSetup type but '.gettype($AppModule));331 }332 333 function DoStart(): void334 {335 Core::Cast($this->System)->RegisterPage([''], 'PageSetupRedirect');336 Core::Cast($this->System)->RegisterPage(['setup'], 'PageSetup');337 }338 339 function DoStop(): void340 {341 unset($this->UpdateManager);342 Core::Cast($this->System)->UnregisterPage(['']);343 Core::Cast($this->System)->UnregisterPage(['setup']);344 }345 346 function CheckState(): bool347 {348 return $this->Database->Connected() and $this->UpdateManager->IsInstalled() and349 $this->UpdateManager->IsUpToDate();350 }351 352 function DoUpgrade(): string353 {354 $Updates = new Updates();355 $this->UpdateManager->Trace = $Updates->Get();356 $Output = $this->UpdateManager->Upgrade();357 return $Output;358 }359 } -
trunk/Packages/Common/System.php
r898 r899 43 43 } 44 44 45 class Application extendsSystem45 class System 46 46 { 47 public Database $Database; 47 48 public string $Name; 48 public AppModuleManager $ModuleManager;49 public ModuleManager $ModuleManager; 49 50 public array $Models; 50 51 public array $CommandLine; … … 53 54 function __construct() 54 55 { 55 parent::__construct();56 $this->Name = ' Application';57 $this->ModuleManager = new AppModuleManager($this);56 $this->Database = new Database(); 57 $this->Name = 'System'; 58 $this->ModuleManager = new ModuleManager($this); 58 59 $this->Models = array(); 59 60 $this->CommandLine = array(); … … 63 64 } 64 65 65 function GetModule(string $Name): AppModule66 function GetModule(string $Name): Module 66 67 { 67 68 if (array_key_exists($Name, $this->ModuleManager->Modules)) … … 87 88 } 88 89 89 function Unr gisterCommandLine(string $Name): void90 function UnregisterCommandLine(string $Name): void 90 91 { 91 92 unset($this->CommandLine[$Name]);
Note:
See TracChangeset
for help on using the changeset viewer.