source: trunk/Packages/Common/System.php

Last change on this file was 9, checked in by chronos, 19 months ago
  • Fixed: Modules initialization.
File size: 2.5 KB
Line 
1<?php
2
3class Command
4{
5 public string $Name;
6 public string $Description;
7 public $Callback;
8
9 function __construct(string $Name, string $Description, callable $Callback)
10 {
11 $this->Name = $Name;
12 $this->Description = $Description;
13 $this->Callback = $Callback;
14 }
15}
16
17class System
18{
19 public Database $Database;
20 public string $Name;
21 public ModuleManager $ModuleManager;
22 public array $Models;
23 public array $CommandLine;
24 public array $Pages;
25
26 function __construct()
27 {
28 $this->Database = new Database();
29 $this->Name = 'System';
30 $this->ModuleManager = new ModuleManager($this);
31 $this->Models = array();
32 $this->CommandLine = array();
33 $this->Pages = array();
34
35 $this->RegisterCommandLine('list', 'Prints available commands', array($this, 'ListCommands'));
36 }
37
38 function GetModule(string $Name): Module
39 {
40 if (array_key_exists($Name, $this->ModuleManager->Modules))
41 {
42 return $this->ModuleManager->Modules[$Name];
43 }
44 else
45 {
46 echo('Module '.$Name.' not registered.');
47 }
48 }
49
50 function RegisterModel(string $SysName, array $Model): void
51 {
52 $NewModelDef = new ModelDef();
53 $NewModelDef->Title = $Model['Title'];
54 $this->Models[$SysName] = $NewModelDef;
55 }
56
57 function UnregisterModel(string $SysName): void
58 {
59 unset($this->Models[$SysName]);
60 }
61
62 function RegisterCommandLine(string $Name, string $Description, callable $Callback): void
63 {
64 $this->CommandLine[$Name] = new Command($Name, $Description, $Callback);
65 }
66
67 function UnregisterCommandLine(string $Name): void
68 {
69 unset($this->CommandLine[$Name]);
70 }
71
72 function ListCommands(array $Parameters)
73 {
74 $Output = 'Available commands:'."\n";
75 foreach ($this->CommandLine as $Command)
76 {
77 $Output .= ' '.$Command->Name.' - '.$Command->Description."\n";
78 }
79 echo($Output."\n");
80 }
81
82 function RegisterPage(array $Path, string $Handler): void
83 {
84 $Page = &$this->Pages;
85 $LastKey = array_pop($Path);
86 foreach ($Path as $PathItem)
87 {
88 $Page = &$Page[$PathItem];
89 }
90 if (!is_array($Page)) $Page = array('' => $Page);
91 $Page[$LastKey] = $Handler;
92 }
93
94 function UnregisterPage(array $Path): void
95 {
96 $Page = &$this->Pages;
97 $LastKey = array_pop($Path);
98 foreach ($Path as $PathItem)
99 {
100 $Page = &$Page[$PathItem];
101 }
102 unset($Page[$LastKey]);
103 }
104
105 function Run(): void
106 {
107 }
108
109 function Link(string $Target): string
110 {
111 return $Target;
112 }
113
114 function AbsoluteLink(string $Target): string
115 {
116 return $Target;
117 }
118}
Note: See TracBrowser for help on using the repository browser.