source: trunk/Packages/Common/System.php

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