1 | <?php
|
---|
2 |
|
---|
3 | /* This implementation will not support installation from remote source. Just
|
---|
4 | * installation of already presented modules mainly to persistence preparation.
|
---|
5 | */
|
---|
6 |
|
---|
7 | class ModuleType
|
---|
8 | {
|
---|
9 | const System = 0;
|
---|
10 | const Normal = 1;
|
---|
11 | const Application = 2;
|
---|
12 | }
|
---|
13 |
|
---|
14 | class ModuleAction
|
---|
15 | {
|
---|
16 | const Start = 0;
|
---|
17 | const Stop = 1;
|
---|
18 | const Install = 2;
|
---|
19 | const Uninstall = 3;
|
---|
20 | const Upgrade = 4;
|
---|
21 | const Enable = 5;
|
---|
22 | const Disable = 6;
|
---|
23 | }
|
---|
24 |
|
---|
25 | class ModuleCondition
|
---|
26 | {
|
---|
27 | const All = 0;
|
---|
28 | const Enabled = 1;
|
---|
29 | const NotEnabled = 2;
|
---|
30 | const Installed = 3;
|
---|
31 | const NotInstalled = 4;
|
---|
32 | const Running = 5;
|
---|
33 | const NotRunning = 6;
|
---|
34 | }
|
---|
35 |
|
---|
36 | class AppModule
|
---|
37 | {
|
---|
38 | var $Id;
|
---|
39 | var $Name;
|
---|
40 | var $Version;
|
---|
41 | var $License;
|
---|
42 | var $Creator;
|
---|
43 | var $HomePage;
|
---|
44 | var $Description;
|
---|
45 | var $Running;
|
---|
46 | var $Enabled;
|
---|
47 | var $Installed;
|
---|
48 | var $InstalledVersion;
|
---|
49 | /** @var ModuleType */
|
---|
50 | var $Type;
|
---|
51 | var $Dependencies;
|
---|
52 | /** @var Database */
|
---|
53 | var $Database;
|
---|
54 | /** @var System */
|
---|
55 | var $System;
|
---|
56 | /** @var AppModuleManager */
|
---|
57 | var $Manager;
|
---|
58 | var $OnChange;
|
---|
59 |
|
---|
60 | function __construct(System $System)
|
---|
61 | {
|
---|
62 | $this->System = &$System;
|
---|
63 | $this->Database = &$System->Database;
|
---|
64 | $this->Installed = false;
|
---|
65 | $this->Enabled = false;
|
---|
66 | $this->Running = false;
|
---|
67 | $this->Dependencies = array();
|
---|
68 | $this->Type = ModuleType::Normal;
|
---|
69 | }
|
---|
70 |
|
---|
71 | function Install()
|
---|
72 | {
|
---|
73 | if($this->Installed) return;
|
---|
74 | $List = array();
|
---|
75 | $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotInstalled));
|
---|
76 | $this->Manager->Perform($List, array(ModuleAction::Install), array(ModuleCondition::NotInstalled));
|
---|
77 | $this->DoInstall();
|
---|
78 | $this->Installed = true;
|
---|
79 | $this->InstalledVersion = $this->Version;
|
---|
80 | $this->Manager->Modules[$this->Name] = $this;
|
---|
81 | }
|
---|
82 |
|
---|
83 | function Uninstall()
|
---|
84 | {
|
---|
85 | if(!$this->Installed) return;
|
---|
86 | $this->Stop();
|
---|
87 | $this->Installed = false;
|
---|
88 | $List = array();
|
---|
89 | $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed));
|
---|
90 | $this->Manager->Perform($List, array(ModuleAction::Uninstall), array(ModuleCondition::Installed));
|
---|
91 | $this->DoUninstall();
|
---|
92 | }
|
---|
93 |
|
---|
94 | function Upgrade()
|
---|
95 | {
|
---|
96 | if(!$this->Installed) return;
|
---|
97 | $List = array();
|
---|
98 | $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed));
|
---|
99 | $this->Manager->Perform($List, array(ModuleAction::Upgrade), array(ModuleCondition::Installed));
|
---|
100 | $this->DoUpgrade();
|
---|
101 | }
|
---|
102 |
|
---|
103 | function Reinstall()
|
---|
104 | {
|
---|
105 | $this->Uninstall();
|
---|
106 | // TODO: Install also back dependecies
|
---|
107 | $this->Install();
|
---|
108 | }
|
---|
109 |
|
---|
110 | function Start()
|
---|
111 | {
|
---|
112 | if($this->Running) return;
|
---|
113 | if(!$this->Installed) return;
|
---|
114 | $List = array();
|
---|
115 | $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotRunning));
|
---|
116 | $this->Manager->Perform($List, array(ModuleAction::Start), array(ModuleCondition::NotRunning));
|
---|
117 | $this->DoStart();
|
---|
118 | $this->Running = true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | function Stop()
|
---|
122 | {
|
---|
123 | if(!$this->Running) return;
|
---|
124 | $this->Running = false;
|
---|
125 | $List = array();
|
---|
126 | $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Running));
|
---|
127 | $this->Manager->Perform($List, array(ModuleAction::Stop), array(ModuleCondition::Running));
|
---|
128 | $this->DoStop();
|
---|
129 | }
|
---|
130 |
|
---|
131 | function Restart()
|
---|
132 | {
|
---|
133 | $this->Stop();
|
---|
134 | $this->Start();
|
---|
135 | }
|
---|
136 |
|
---|
137 | function Enable()
|
---|
138 | {
|
---|
139 | if($this->Enabled) return;
|
---|
140 | if(!$this->Installed) return;
|
---|
141 | $List = array();
|
---|
142 | $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotEnabled));
|
---|
143 | $this->Manager->Perform($List, array(ModuleAction::Enable), array(ModuleCondition::NotEnabled));
|
---|
144 | $this->Enabled = true;
|
---|
145 | }
|
---|
146 |
|
---|
147 | function Disable()
|
---|
148 | {
|
---|
149 | if(!$this->Enabled) return;
|
---|
150 | $this->Stop();
|
---|
151 | $this->Enabled = false;
|
---|
152 | $List = array();
|
---|
153 | $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Enabled));
|
---|
154 | $this->Manager->Perform($List, array(ModuleAction::Disable), array(ModuleCondition::Enabled));
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected function DoStart()
|
---|
158 | {
|
---|
159 | }
|
---|
160 |
|
---|
161 | protected function DoStop()
|
---|
162 | {
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected function DoInstall()
|
---|
166 | {
|
---|
167 | }
|
---|
168 |
|
---|
169 | protected function DoUninstall()
|
---|
170 | {
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* Manage installed modules */
|
---|
175 | class AppModuleManager
|
---|
176 | {
|
---|
177 | var $Modules;
|
---|
178 | var $System;
|
---|
179 | var $FileName;
|
---|
180 | var $OnLoadModules;
|
---|
181 |
|
---|
182 | function __construct(System $System)
|
---|
183 | {
|
---|
184 | $this->Modules = array();
|
---|
185 | $this->System = &$System;
|
---|
186 | $this->FileName = 'Config/Modules.php';
|
---|
187 | }
|
---|
188 |
|
---|
189 | function Perform($List, $Actions, $Conditions = array(ModuleCondition::All))
|
---|
190 | {
|
---|
191 | foreach($List as $Index => $Module)
|
---|
192 | {
|
---|
193 | if(in_array(ModuleCondition::All, $Conditions) or
|
---|
194 | ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or
|
---|
195 | (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or
|
---|
196 | ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
|
---|
197 | (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or
|
---|
198 | ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
|
---|
199 | (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)))
|
---|
200 | {
|
---|
201 | foreach($Actions as $Action)
|
---|
202 | {
|
---|
203 | if($Action == ModuleAction::Start) $Module->Start();
|
---|
204 | if($Action == ModuleAction::Stop) $Module->Stop();
|
---|
205 | if($Action == ModuleAction::Install) $Module->Install();
|
---|
206 | if($Action == ModuleAction::Uninstall) $Module->Uninstall();
|
---|
207 | if($Action == ModuleAction::Enable) $Module->Enable();
|
---|
208 | if($Action == ModuleAction::Disable) $Module->Disable();
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | function EnumDependenciesCascade($Module, &$List, $Conditions = array(ModuleCondition::All))
|
---|
215 | {
|
---|
216 | foreach($Module->Dependencies as $Dependency)
|
---|
217 | {
|
---|
218 | $DepModule = $this->Modules[$Dependency];
|
---|
219 | if(in_array(ModuleCondition::All, $Conditions) or
|
---|
220 | ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or
|
---|
221 | (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or
|
---|
222 | ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
|
---|
223 | (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
|
---|
224 | ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
|
---|
225 | (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)))
|
---|
226 | {
|
---|
227 | array_push($List, $DepModule);
|
---|
228 | $this->EnumDependenciesCascade($DepModule, $List, $Conditions);
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | function EnumSuperiorDependenciesCascade($Module, &$List, $Conditions = array(ModuleCondition::All))
|
---|
234 | {
|
---|
235 | foreach($this->Modules as $RefModule)
|
---|
236 | {
|
---|
237 | if(in_array($Module->Name, $RefModule->Dependencies) and
|
---|
238 | (in_array(ModuleCondition::All, $Conditions) or
|
---|
239 | ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or
|
---|
240 | (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or
|
---|
241 | ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
|
---|
242 | (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
|
---|
243 | ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
|
---|
244 | (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions))))
|
---|
245 | {
|
---|
246 | array_push($List, $RefModule);
|
---|
247 | $this->EnumSuperiorDependenciesCascade($RefModule, $List, $Conditions);
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | function Start()
|
---|
253 | {
|
---|
254 | $this->LoadModules();
|
---|
255 | if(file_exists($this->FileName)) $this->LoadState();
|
---|
256 | $this->StartEnabled();
|
---|
257 | }
|
---|
258 |
|
---|
259 | function StartAll()
|
---|
260 | {
|
---|
261 | $this->Perform($this->Modules, array(ModuleAction::Start));
|
---|
262 | }
|
---|
263 |
|
---|
264 | function StartEnabled()
|
---|
265 | {
|
---|
266 | $this->Perform($this->Modules, array(ModuleAction::Start), array(ModuleCondition::Enabled));
|
---|
267 | }
|
---|
268 |
|
---|
269 | function StopAll()
|
---|
270 | {
|
---|
271 | $this->Perform($this->Modules, array(ModuleAction::Stop));
|
---|
272 | }
|
---|
273 |
|
---|
274 | function UninstallAll()
|
---|
275 | {
|
---|
276 | $this->Perform($this->Modules, array(ModuleAction::Uninstall));
|
---|
277 | $this->SaveState();
|
---|
278 | }
|
---|
279 |
|
---|
280 | function ModulePresent($Name)
|
---|
281 | {
|
---|
282 | return(array_key_exists($Name, $this->Modules));
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* @return Module */
|
---|
286 | function SearchModuleById($Id)
|
---|
287 | {
|
---|
288 | foreach($this->Modules as $Module)
|
---|
289 | {
|
---|
290 | //DebugLog($Module->Name.' '.$Module->Id);
|
---|
291 | if($Module->Id == $Id) return($Module->Name);
|
---|
292 | }
|
---|
293 | return('');
|
---|
294 | }
|
---|
295 |
|
---|
296 | function LoadState()
|
---|
297 | {
|
---|
298 | $ConfigModules = array();
|
---|
299 | include($this->FileName);
|
---|
300 | foreach($ConfigModules as $Mod)
|
---|
301 | {
|
---|
302 | if(array_key_exists($Mod['Name'], $this->Modules))
|
---|
303 | {
|
---|
304 | $this->Modules[$Mod['Name']] = $this->Modules[$Mod['Name']];
|
---|
305 | $this->Modules[$Mod['Name']]->Enabled = $Mod['Enabled'];
|
---|
306 | $this->Modules[$Mod['Name']]->Installed = $Mod['Installed'];
|
---|
307 | $this->Modules[$Mod['Name']]->InstalledVersion = $Mod['Version'];
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | function SaveState()
|
---|
313 | {
|
---|
314 | $Data = array();
|
---|
315 | foreach($this->Modules as $Module)
|
---|
316 | {
|
---|
317 | $Data[] = array('Name' => $Module->Name, 'Enabled' => $Module->Enabled,
|
---|
318 | 'Version' => $Module->Version, 'Installed' => $Module->Installed);
|
---|
319 | }
|
---|
320 | file_put_contents($this->FileName, "<?php \n\n\$ConfigModules = ".var_export($Data, true).";\n");
|
---|
321 | }
|
---|
322 |
|
---|
323 | function RegisterModule(AppModule $Module)
|
---|
324 | {
|
---|
325 | $this->Modules[$Module->Name] = &$Module;
|
---|
326 | $Module->Manager = &$this;
|
---|
327 | $Module->OnChange = &$this->OnModuleChange;
|
---|
328 | }
|
---|
329 |
|
---|
330 | function UnregisterModule(AppModule $Module)
|
---|
331 | {
|
---|
332 | unset($this->Modules[array_search($Module, $this->Modules)]);
|
---|
333 | }
|
---|
334 |
|
---|
335 | function LoadModulesFromDir($Directory)
|
---|
336 | {
|
---|
337 | $List = scandir($Directory);
|
---|
338 | foreach($List as $Item)
|
---|
339 | {
|
---|
340 | if(is_dir($Directory.'/'.$Item) and ($Item != '.') and ($Item != '..') and ($Item != '.svn'))
|
---|
341 | {
|
---|
342 | include_once($Directory.'/'.$Item.'/'.$Item.'.php');
|
---|
343 | $ModuleName = 'Module'.$Item;
|
---|
344 | $this->RegisterModule(new $ModuleName($this->System));
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | function LoadModules()
|
---|
350 | {
|
---|
351 | if(method_exists($this->OnLoadModules[0], $this->OnLoadModules[1]))
|
---|
352 | $this->OnLoadModules();
|
---|
353 | else $this->LoadModulesFromDir(dirname(__FILE__).'/../Modules');
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|