source: trunk/Packages/Common/Module.php

Last change on this file was 63, checked in by chronos, 3 years ago
  • Modified: Used explicit types where possible for better error reporting.
  • Modified: Updated Common packaged to newer version.
  • Modified: Simplified pages title.
  • Added: Simple keyword based spam filter for meet items.
File size: 6.4 KB
Line 
1<?php
2
3class ModuleType
4{
5 const System = 0;
6 const Library = 1;
7 const Application = 2;
8}
9
10class ModuleAction
11{
12 const Start = 0;
13 const Stop = 1;
14 const Install = 2;
15 const Uninstall = 3;
16 const Upgrade = 4;
17 const Enable = 5;
18 const Disable = 6;
19 const InsertSampleData = 7;
20}
21
22class ModuleCondition
23{
24 const All = 0;
25 const Enabled = 1;
26 const NotEnabled = 2;
27 const Installed = 3;
28 const NotInstalled = 4;
29 const Running = 5;
30 const NotRunning = 6;
31 const System = 7;
32 const Library = 8;
33 const Application = 9;
34}
35
36class Module extends Model
37{
38 public int $Id;
39 public string $Name;
40 public string $Title;
41 public string $Version;
42 public string $License;
43 public string $Creator;
44 public string $HomePage;
45 public string $Description;
46 public int $Type; // ModuleType
47 public array $Dependencies;
48 public array $Models; // Model
49 public bool $Running;
50 public bool $Enabled;
51 public bool $Installed;
52 public string $InstalledVersion;
53 public Database $Database;
54 public System $System;
55 public ModuleManager $Manager;
56 public $OnChange;
57
58 function __construct(System $System)
59 {
60 parent::__construct($System);
61 $this->Name = '';
62 $this->HomePage = '';
63 $this->License = '';
64 $this->Version = '';
65 $this->Creator = '';
66 $this->Title = '';
67 $this->Description = '';
68 $this->Dependencies = array();
69 $this->Models = array();
70 $this->Type = ModuleType::Library;
71 $this->Installed = false;
72 $this->InstalledVersion = '';
73 $this->Enabled = false;
74 $this->Running = false;
75 }
76
77 static function GetName()
78 {
79 $ClassName = get_called_class();
80 if (substr($ClassName, 0, 6) == 'Module') return substr($ClassName, 6);
81 else return $ClassName;
82 }
83
84 static function GetModelDesc(): ModelDesc
85 {
86 $Desc = new ModelDesc(self::GetClassName());
87 $Desc->AddString('Name');
88 $Desc->AddString('Title');
89 $Desc->AddString('Creator');
90 $Desc->AddString('Version');
91 $Desc->AddString('License');
92 $Desc->AddBoolean('Installed');
93 $Desc->AddString('InstalledVersion');
94 $Desc->AddString('Description');
95 return $Desc;
96 }
97
98 function Install(): void
99 {
100 if ($this->Installed) return;
101 $List = array();
102 $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotInstalled));
103 $this->Manager->PerformList($List, array(ModuleAction::Install), array(ModuleCondition::NotInstalled));
104 $this->Installed = true;
105 $this->Enabled = true; // Automatically enable installed module
106 $this->InstalledVersion = $this->Version;
107 $this->Manager->Modules[$this->Name] = $this;
108 $this->DoBeforeInstall();
109 if ($this->Manager->OnInstallModule != null)
110 {
111 call_user_func($this->Manager->OnInstallModule, $this);
112 }
113 $this->InstallModels($this->Models);
114 $this->DoInstall();
115 }
116
117 function Uninstall(): void
118 {
119 if (!$this->Installed) return;
120 $this->Stop();
121 $this->Installed = false;
122 $List = array();
123 $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed));
124 $this->Manager->PerformList($List, array(ModuleAction::Uninstall), array(ModuleCondition::Installed));
125 $this->DoUninstall();
126 $this->UninstallModels($this->Models);
127 if ($this->Manager->OnUninstallModule != null)
128 {
129 call_user_func($this->Manager->OnUninstallModule, $this);
130 }
131 $this->DoAfterUninstall();
132 }
133
134 function Upgrade(): void
135 {
136 if (!$this->Installed) return;
137 if ($this->InstalledVersion == $this->Version) return;
138 $List = array();
139 $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed));
140 $this->Manager->PerformList($List, array(ModuleAction::Upgrade), array(ModuleCondition::Installed));
141 $this->DoUpgrade();
142 }
143
144 function InsertSampleData(): void
145 {
146 $this->DoInsertSampleData();
147 }
148
149 function Reinstall(): void
150 {
151 $this->Uninstall();
152 // TODO: Install also back dependecies
153 $this->Install();
154 }
155
156 function Start(): void
157 {
158 if ($this->Running) return;
159 if (!$this->Installed) return;
160 $List = array();
161 $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotRunning));
162 $this->Manager->PerformList($List, array(ModuleAction::Start), array(ModuleCondition::NotRunning));
163 $this->DoStart();
164 $this->Running = true;
165 }
166
167 function Stop(): void
168 {
169 if (!$this->Running) return;
170 $this->Running = false;
171 $List = array();
172 $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Running));
173 $this->Manager->PerformList($List, array(ModuleAction::Stop), array(ModuleCondition::Running));
174 $this->DoStop();
175 }
176
177 function Restart(): void
178 {
179 $this->Stop();
180 $this->Start();
181 }
182
183 function Enable(): void
184 {
185 if ($this->Enabled) return;
186 if (!$this->Installed) return;
187 $List = array();
188 $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotEnabled));
189 $this->Manager->PerformList($List, array(ModuleAction::Enable), array(ModuleCondition::NotEnabled));
190 $this->Enabled = true;
191 }
192
193 function Disable(): void
194 {
195 if (!$this->Enabled) return;
196 $this->Stop();
197 $this->Enabled = false;
198 $List = array();
199 $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Enabled));
200 $this->Manager->PerformList($List, array(ModuleAction::Disable), array(ModuleCondition::Enabled));
201 }
202
203 protected function DoStart(): void
204 {
205 }
206
207 protected function DoStop(): void
208 {
209 }
210
211 protected function DoBeforeInstall(): void
212 {
213 }
214
215 protected function DoInstall(): void
216 {
217 }
218
219 protected function DoUninstall(): void
220 {
221 }
222
223 protected function DoAfterUninstall(): void
224 {
225 }
226
227 protected function DoUpgrade(): string
228 {
229 return '';
230 }
231
232 protected function DoInsertSampleData(): void
233 {
234 }
235
236 function AddModel(Model $Model): void
237 {
238 $this->Models[get_class($Model)] = $Model;
239 }
240
241 function InstallModels(array $Models): void
242 {
243 if ($this->Manager->OnInstallModel != null)
244 {
245 foreach ($Models as $Model)
246 {
247 call_user_func($this->Manager->OnInstallModel, $Model::GetModelDesc(), $this);
248 }
249 }
250 }
251
252 function UninstallModels(array $Models): void
253 {
254 if ($this->Manager->OnUninstallModel != null)
255 {
256 foreach (array_reverse($Models) as $Model)
257 {
258 call_user_func($this->Manager->OnUninstallModel, $Model::GetModelDesc(), $this);
259 }
260 }
261 }
262}
Note: See TracBrowser for help on using the repository browser.