| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class ModelDef
|
|---|
| 4 | {
|
|---|
| 5 | public string $Title;
|
|---|
| 6 | public array $OnChange;
|
|---|
| 7 |
|
|---|
| 8 | function __construct()
|
|---|
| 9 | {
|
|---|
| 10 | $this->OnChange = array();
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function DoOnChange(): void
|
|---|
| 14 | {
|
|---|
| 15 | foreach ($this->OnChange as $Callback)
|
|---|
| 16 | {
|
|---|
| 17 | call_user_func($Callback);
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function RegisterOnChange(string $SysName, callable $Callback): void
|
|---|
| 22 | {
|
|---|
| 23 | $this->OnChange[$SysName] = $Callback;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | function UnregisterOnChange(string $SysName): void
|
|---|
| 27 | {
|
|---|
| 28 | unset($this->OnChange[$SysName]);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | class Command
|
|---|
| 33 | {
|
|---|
| 34 | public string $Name;
|
|---|
| 35 | public string $Description;
|
|---|
| 36 | public $Callback;
|
|---|
| 37 |
|
|---|
| 38 | function __construct(string $Name, string $Description, callable $Callback)
|
|---|
| 39 | {
|
|---|
| 40 | $this->Name = $Name;
|
|---|
| 41 | $this->Description = $Description;
|
|---|
| 42 | $this->Callback = $Callback;
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | class System
|
|---|
| 47 | {
|
|---|
| 48 | public Database $Database;
|
|---|
| 49 | public string $Name;
|
|---|
| 50 | public ModuleManager $ModuleManager;
|
|---|
| 51 | public array $Models;
|
|---|
| 52 | public array $CommandLine;
|
|---|
| 53 | public array $Pages;
|
|---|
| 54 |
|
|---|
| 55 | function __construct()
|
|---|
| 56 | {
|
|---|
| 57 | $this->Database = new Database();
|
|---|
| 58 | $this->Name = 'System';
|
|---|
| 59 | $this->ModuleManager = new ModuleManager($this);
|
|---|
| 60 | $this->Models = array();
|
|---|
| 61 | $this->CommandLine = array();
|
|---|
| 62 | $this->Pages = array();
|
|---|
| 63 |
|
|---|
| 64 | $this->RegisterCommandLine('list', 'Prints available commands', array($this, 'ListCommands'));
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | function GetModule(string $Name): Module
|
|---|
| 68 | {
|
|---|
| 69 | if (array_key_exists($Name, $this->ModuleManager->Modules))
|
|---|
| 70 | return $this->ModuleManager->Modules[$Name];
|
|---|
| 71 | else return null;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | function RegisterModel(string $SysName, array $Model): void
|
|---|
| 75 | {
|
|---|
| 76 | $NewModelDef = new ModelDef();
|
|---|
| 77 | $NewModelDef->Title = $Model['Title'];
|
|---|
| 78 | $this->Models[$SysName] = $NewModelDef;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | function UnregisterModel(string $SysName): void
|
|---|
| 82 | {
|
|---|
| 83 | unset($this->Models[$SysName]);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | function RegisterCommandLine(string $Name, string $Description, callable $Callback): void
|
|---|
| 87 | {
|
|---|
| 88 | $this->CommandLine[$Name] = new Command($Name, $Description, $Callback);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | function UnregisterCommandLine(string $Name): void
|
|---|
| 92 | {
|
|---|
| 93 | unset($this->CommandLine[$Name]);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | function ListCommands(array $Parameters)
|
|---|
| 97 | {
|
|---|
| 98 | $Output = 'Available commands:'."\n";
|
|---|
| 99 | foreach ($this->CommandLine as $Command)
|
|---|
| 100 | {
|
|---|
| 101 | $Output .= ' '.$Command->Name.' - '.$Command->Description."\n";
|
|---|
| 102 | }
|
|---|
| 103 | echo($Output."\n");
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | function RegisterPage(array $Path, string $Handler): void
|
|---|
| 107 | {
|
|---|
| 108 | $Page = &$this->Pages;
|
|---|
| 109 | $LastKey = array_pop($Path);
|
|---|
| 110 | foreach ($Path as $PathItem)
|
|---|
| 111 | {
|
|---|
| 112 | $Page = &$Page[$PathItem];
|
|---|
| 113 | }
|
|---|
| 114 | if (!is_array($Page)) $Page = array('' => $Page);
|
|---|
| 115 | $Page[$LastKey] = $Handler;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | function UnregisterPage(array $Path): void
|
|---|
| 119 | {
|
|---|
| 120 | $Page = &$this->Pages;
|
|---|
| 121 | $LastKey = array_pop($Path);
|
|---|
| 122 | foreach ($Path as $PathItem)
|
|---|
| 123 | {
|
|---|
| 124 | $Page = &$Page[$PathItem];
|
|---|
| 125 | }
|
|---|
| 126 | unset($Page[$LastKey]);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | function Run(): void
|
|---|
| 130 | {
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | function Link(string $Target): string
|
|---|
| 134 | {
|
|---|
| 135 | return $Target;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | function AbsoluteLink(string $Target): string
|
|---|
| 139 | {
|
|---|
| 140 | return $Target;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|