Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (3 years ago)
Author:
chronos
Message:
  • Added: Static types added to almost all classes, methods and function. Supported by PHP 7.4.
  • Fixed: Various found code issues.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Application.php

    r873 r887  
    33class ModelDef
    44{
    5   var $OnChange;
    6  
     5  public array $OnChange;
     6
    77  function __construct()
    88  {
    99    $this->OnChange = array();
    1010  }
    11  
    12   function DoOnChange()
     11
     12  function DoOnChange(): void
    1313  {
    1414    foreach ($this->OnChange as $Callback)
     
    1717    }
    1818  }
    19  
    20   function RegisterOnChange($SysName, $Callback)
     19
     20  function RegisterOnChange(string $SysName, callable $Callback): void
    2121  {
    2222    $this->OnChange[$SysName] = $Callback;
    2323  }
    24  
    25   function UnregisterOnChange($SysName)
     24
     25  function UnregisterOnChange(string $SysName): void
    2626  {
    2727    unset($this->OnChange[$SysName]);
     
    2929}
    3030
     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
    3145class Application extends System
    3246{
    33   var $Name;
    34   /** @var AppModuleManager */
    35   var $ModuleManager;
    36   var $Modules;
    37   var $Models;
    38  
     47  public string $Name;
     48  public AppModuleManager $ModuleManager;
     49  public array $Models;
     50  public array $CommandLine;
     51  public array $Pages;
     52
    3953  function __construct()
    4054  {
     
    4256    $this->Name = 'Application';
    4357    $this->ModuleManager = new AppModuleManager($this);
    44     $this->Modules = array();
    4558    $this->Models = array();
     59    $this->CommandLine = array();
     60    $this->Pages = array();
     61
     62    $this->RegisterCommandLine('list', 'Prints available commands', array($this, 'ListCommands'));
    4663  }
    47  
    48   function RegisterModel($SysName, $Model)
     64
     65  function GetModule(string $Name): AppModule
     66  {
     67    return $this->ModuleManager->Modules[$Name];
     68  }
     69
     70  function RegisterModel(string $SysName, array $Model): void
    4971  {
    5072    $NewModelDef = new ModelDef();
     
    5375  }
    5476
    55   function UnregisterModel($SysName)
     77  function UnregisterModel(string $SysName): void
    5678  {
    5779    unset($this->Models[$SysName]);
    5880  }
    59  
    60   function Run()
     81
     82  function RegisterCommandLine(string $Name, string $Description, callable $Callback): void
    6183  {
     84    $this->CommandLine[$Name] = new Command($Name, $Description, $Callback);
     85  }
    6286
     87  function UnrgisterCommandLine(string $Name): void
     88  {
     89    unset($this->CommandLine[$Name]);
     90  }
     91
     92  function ListCommands(array $Parameters)
     93  {
     94    $Output = 'Available commands:'."\n";
     95    foreach ($this->CommandLine as $Command)
     96    {
     97      $Output .= ' '.$Command->Name.' - '.$Command->Description."\n";
     98    }
     99    echo($Output."\n");
     100  }
     101
     102  function RegisterPage(array $Path, string $Handler): void
     103  {
     104    $Page = &$this->Pages;
     105    $LastKey = array_pop($Path);
     106    foreach ($Path as $PathItem)
     107    {
     108      $Page = &$Page[$PathItem];
     109    }
     110    if (!is_array($Page)) $Page = array('' => $Page);
     111    $Page[$LastKey] = $Handler;
     112  }
     113
     114  function UnregisterPage(array $Path): void
     115  {
     116    $Page = &$this->Pages;
     117    $LastKey = array_pop($Path);
     118    foreach ($Path as $PathItem)
     119    {
     120      $Page = &$Page[$PathItem];
     121    }
     122    unset($Page[$LastKey]);
     123  }
     124
     125  function Run(): void
     126  {
     127  }
     128
     129  function Link(string $Target): string
     130  {
     131    return $Target;
    63132  }
    64133}
Note: See TracChangeset for help on using the changeset viewer.