Ignore:
Timestamp:
Dec 27, 2022, 7:50:23 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File:
1 moved

Legend:

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

    r887 r888  
    11<?php
    22
    3 /* This implementation will not support installation from remote source. Just
    4  * installation of already presented modules mainly to persistence preparation.
    5  */
    6 
    73class ModuleType
    84{
    95  const System = 0;
    10   const Normal = 1;
     6  const Library = 1;
    117  const Application = 2;
    128}
     
    2117  const Enable = 5;
    2218  const Disable = 6;
     19  const InsertSampleData = 7;
    2320}
    2421
     
    3229  const Running = 5;
    3330  const NotRunning = 6;
    34 }
    35 
    36 class AppModule
    37 {
    38   var $Id;
    39   var $Name;
    40   var $Title;
    41   var $Version;
    42   var $License;
    43   var $Creator;
    44   var $HomePage;
    45   var $Description;
    46   var $Running;
    47   var $Enabled;
    48   var $Installed;
    49   var $InstalledVersion;
    50   /** @var ModuleType */
    51   var $Type;
    52   var $Dependencies;
    53   /** @var Database */
    54   var $Database;
    55   /** @var System */
    56   var $System;
    57   /** @var AppModuleManager */
    58   var $Manager;
    59   var $OnChange;
     31  const System = 7;
     32  const Library = 8;
     33  const Application = 9;
     34}
     35
     36class Module extends Model
     37{
     38  public int $Id;
     39  public string $Name;
     40  public string $Title;
     41  public string $Version;
     42  public string $License;
     43  public string $Creator;
     44  public string $HomePage;
     45  public string $Description;
     46  public int $Type; // ModuleType
     47  public array $Dependencies;
     48  public array $Models; // Model
     49  public bool $Running;
     50  public bool $Enabled;
     51  public bool $Installed;
     52  public string $InstalledVersion;
     53  public Database $Database;
     54  public System $System;
     55  public ModuleManager $Manager;
     56  public $OnChange;
    6057
    6158  function __construct(System $System)
    6259  {
    63     $this->System = &$System;
    64     $this->Database = &$System->Database;
    65     $this->Installed = false;
    66     $this->Enabled = false;
    67     $this->Running = false;
     60    parent::__construct($System);
     61    $this->Name = '';
    6862    $this->HomePage = '';
    6963    $this->License = '';
    7064    $this->Version = '';
    7165    $this->Creator = '';
     66    $this->Title = '';
    7267    $this->Description = '';
    7368    $this->Dependencies = array();
    74     $this->Type = ModuleType::Normal;
    75   }
    76 
    77   function Install()
     69    $this->Models = array();
     70    $this->Type = ModuleType::Library;
     71    $this->Installed = false;
     72    $this->InstalledVersion = '';
     73    $this->Enabled = false;
     74    $this->Running = false;
     75  }
     76
     77  static function GetName()
     78  {
     79    $ClassName = get_called_class();
     80    if (substr($ClassName, 0, 6) == 'Module') return substr($ClassName, 6);
     81      else return $ClassName;
     82  }
     83
     84  static function GetModelDesc(): ModelDesc
     85  {
     86    $Desc = new ModelDesc(self::GetClassName());
     87    $Desc->AddString('Name');
     88    $Desc->AddString('Title');
     89    $Desc->AddString('Creator');
     90    $Desc->AddString('Version');
     91    $Desc->AddString('License');
     92    $Desc->AddBoolean('Installed');
     93    $Desc->AddString('InstalledVersion');
     94    $Desc->AddString('Description');
     95    return $Desc;
     96  }
     97
     98  function Install(): void
    7899  {
    79100    if ($this->Installed) return;
    80101    $List = array();
    81102    $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotInstalled));
    82     $this->Manager->Perform($List, array(ModuleAction::Install), array(ModuleCondition::NotInstalled));
    83     $this->DoInstall();
     103    $this->Manager->PerformList($List, array(ModuleAction::Install), array(ModuleCondition::NotInstalled));
    84104    $this->Installed = true;
     105    $this->Enabled = true; // Automatically enable installed module
    85106    $this->InstalledVersion = $this->Version;
    86107    $this->Manager->Modules[$this->Name] = $this;
    87   }
    88 
    89   function Uninstall()
     108    $this->DoBeforeInstall();
     109    if ($this->Manager->OnInstallModule != null)
     110    {
     111      call_user_func($this->Manager->OnInstallModule, $this);
     112    }
     113    $this->InstallModels($this->Models);
     114    $this->DoInstall();
     115  }
     116
     117  function Uninstall(): void
    90118  {
    91119    if (!$this->Installed) return;
     
    94122    $List = array();
    95123    $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed));
    96     $this->Manager->Perform($List, array(ModuleAction::Uninstall), array(ModuleCondition::Installed));
     124    $this->Manager->PerformList($List, array(ModuleAction::Uninstall), array(ModuleCondition::Installed));
    97125    $this->DoUninstall();
    98   }
    99 
    100   function Upgrade()
     126    $this->UninstallModels($this->Models);
     127    if ($this->Manager->OnUninstallModule != null)
     128    {
     129      call_user_func($this->Manager->OnUninstallModule, $this);
     130    }
     131    $this->DoAfterUninstall();
     132  }
     133
     134  function Upgrade(): void
    101135  {
    102136    if (!$this->Installed) return;
     
    104138    $List = array();
    105139    $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Installed));
    106     $this->Manager->Perform($List, array(ModuleAction::Upgrade), array(ModuleCondition::Installed));
     140    $this->Manager->PerformList($List, array(ModuleAction::Upgrade), array(ModuleCondition::Installed));
    107141    $this->DoUpgrade();
    108142  }
    109143
    110   function Reinstall()
     144  function InsertSampleData(): void
     145  {
     146    $this->DoInsertSampleData();
     147  }
     148
     149  function Reinstall(): void
    111150  {
    112151    $this->Uninstall();
     
    115154  }
    116155
    117   function Start()
     156  function Start(): void
    118157  {
    119158    if ($this->Running) return;
     
    121160    $List = array();
    122161    $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotRunning));
    123     $this->Manager->Perform($List, array(ModuleAction::Start), array(ModuleCondition::NotRunning));
     162    $this->Manager->PerformList($List, array(ModuleAction::Start), array(ModuleCondition::NotRunning));
    124163    $this->DoStart();
    125164    $this->Running = true;
    126165  }
    127166
    128   function Stop()
     167  function Stop(): void
    129168  {
    130169    if (!$this->Running) return;
     
    132171    $List = array();
    133172    $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Running));
    134     $this->Manager->Perform($List, array(ModuleAction::Stop), array(ModuleCondition::Running));
     173    $this->Manager->PerformList($List, array(ModuleAction::Stop), array(ModuleCondition::Running));
    135174    $this->DoStop();
    136175  }
    137176
    138   function Restart()
     177  function Restart(): void
    139178  {
    140179    $this->Stop();
     
    142181  }
    143182
    144   function Enable()
     183  function Enable(): void
    145184  {
    146185    if ($this->Enabled) return;
     
    148187    $List = array();
    149188    $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotEnabled));
    150     $this->Manager->Perform($List, array(ModuleAction::Enable), array(ModuleCondition::NotEnabled));
     189    $this->Manager->PerformList($List, array(ModuleAction::Enable), array(ModuleCondition::NotEnabled));
    151190    $this->Enabled = true;
    152191  }
    153192
    154   function Disable()
     193  function Disable(): void
    155194  {
    156195    if (!$this->Enabled) return;
     
    159198    $List = array();
    160199    $this->Manager->EnumSuperiorDependenciesCascade($this, $List, array(ModuleCondition::Enabled));
    161     $this->Manager->Perform($List, array(ModuleAction::Disable), array(ModuleCondition::Enabled));
    162   }
    163 
    164   protected function DoStart()
    165   {
    166   }
    167 
    168   protected function DoStop()
    169   {
    170   }
    171 
    172   protected function DoInstall()
    173   {
    174   }
    175 
    176   protected function DoUninstall()
    177   {
    178   }
    179 
    180   protected function DoUpgrade()
    181   {
    182 
    183   }
    184 }
    185 
    186 /* Manage installed modules */
    187 class AppModuleManager
    188 {
    189   var $Modules;
    190   var $System;
    191   var $FileName;
    192   var $ModulesDir;
    193   var $OnLoadModules;
    194 
    195   function __construct(System $System)
    196   {
    197     $this->Modules = array();
    198     $this->System = &$System;
    199     $this->FileName = dirname(__FILE__).'/../../Config/ModulesConfig.php';
    200     $this->ModulesDir = dirname(__FILE__).'/../../Modules';
    201   }
    202 
    203   function Perform($List, $Actions, $Conditions = array(ModuleCondition::All))
    204   {
    205     foreach ($List as $Index => $Module)
    206     {
    207       if (in_array(ModuleCondition::All, $Conditions) or
    208         ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or
    209         (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or
    210         ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
    211         (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or
    212         ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
    213         (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)))
     200    $this->Manager->PerformList($List, array(ModuleAction::Disable), array(ModuleCondition::Enabled));
     201  }
     202
     203  protected function DoStart(): void
     204  {
     205  }
     206
     207  protected function DoStop(): void
     208  {
     209  }
     210
     211  protected function DoBeforeInstall(): void
     212  {
     213  }
     214
     215  protected function DoInstall(): void
     216  {
     217  }
     218
     219  protected function DoUninstall(): void
     220  {
     221  }
     222
     223  protected function DoAfterUninstall(): void
     224  {
     225  }
     226
     227  protected function DoUpgrade(): string
     228  {
     229    return '';
     230  }
     231
     232  protected function DoInsertSampleData(): void
     233  {
     234  }
     235
     236  function AddModel(Model $Model): void
     237  {
     238    $this->Models[get_class($Model)] = $Model;
     239  }
     240
     241  function InstallModels(array $Models): void
     242  {
     243    if ($this->Manager->OnInstallModel != null)
     244    {
     245      foreach ($Models as $Model)
    214246      {
    215         foreach ($Actions as $Action)
    216         {
    217           if ($Action == ModuleAction::Start) $Module->Start();
    218           if ($Action == ModuleAction::Stop) $Module->Stop();
    219           if ($Action == ModuleAction::Install) $Module->Install();
    220           if ($Action == ModuleAction::Uninstall) $Module->Uninstall();
    221           if ($Action == ModuleAction::Enable) $Module->Enable();
    222           if ($Action == ModuleAction::Disable) $Module->Disable();
    223           if ($Action == ModuleAction::Upgrade) $Module->Upgrade();
    224         }
     247        call_user_func($this->Manager->OnInstallModel, $Model::GetModelDesc(), $this);
    225248      }
    226249    }
    227250  }
    228251
    229   function EnumDependenciesCascade($Module, &$List, $Conditions = array(ModuleCondition::All))
    230   {
    231     foreach ($Module->Dependencies as $Dependency)
    232     {
    233       if (!array_key_exists($Dependency, $this->Modules))
    234         throw new Exception(sprintf(T('Module "%s" dependency "%s" not found'), $Module->Name, $Dependency));
    235       $DepModule = $this->Modules[$Dependency];
    236       if (in_array(ModuleCondition::All, $Conditions) or
    237         ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or
    238         (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or
    239         ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
    240         (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
    241         ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
    242         (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)))
     252  function UninstallModels(array $Models): void
     253  {
     254    if ($this->Manager->OnUninstallModel != null)
     255    {
     256      foreach (array_reverse($Models) as $Model)
    243257      {
    244         array_push($List, $DepModule);
    245         $this->EnumDependenciesCascade($DepModule, $List, $Conditions);
     258        call_user_func($this->Manager->OnUninstallModel, $Model::GetModelDesc(), $this);
    246259      }
    247260    }
    248261  }
    249 
    250   function EnumSuperiorDependenciesCascade($Module, &$List, $Conditions = array(ModuleCondition::All))
    251   {
    252     foreach ($this->Modules as $RefModule)
    253     {
    254       if (in_array($Module->Name, $RefModule->Dependencies) and
    255           (in_array(ModuleCondition::All, $Conditions) or
    256           ($Module->Running and in_array(ModuleCondition::Running, $Conditions)) or
    257           (!$Module->Running and in_array(ModuleCondition::NotRunning, $Conditions)) or
    258           ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
    259           (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
    260           ($Module->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
    261           (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions))))
    262       {
    263         array_push($List, $RefModule);
    264         $this->EnumSuperiorDependenciesCascade($RefModule, $List, $Conditions);
    265       }
    266     }
    267   }
    268 
    269   function Start()
    270   {
    271     $this->LoadModules();
    272     if (file_exists($this->FileName)) $this->LoadState();
    273     $this->StartEnabled();
    274   }
    275 
    276   function StartAll()
    277   {
    278     $this->Perform($this->Modules, array(ModuleAction::Start));
    279   }
    280 
    281   function StartEnabled()
    282   {
    283     $this->Perform($this->Modules, array(ModuleAction::Start), array(ModuleCondition::Enabled));
    284   }
    285 
    286   function StopAll()
    287   {
    288     $this->Perform($this->Modules, array(ModuleAction::Stop));
    289   }
    290 
    291   function InstallAll()
    292   {
    293     $this->Perform($this->Modules, array(ModuleAction::Install));
    294     $this->SaveState();
    295   }
    296 
    297   function UninstallAll()
    298   {
    299     $this->Perform($this->Modules, array(ModuleAction::Uninstall));
    300     $this->SaveState();
    301   }
    302 
    303   function EnableAll()
    304   {
    305     $this->Perform($this->Modules, array(ModuleAction::Enable));
    306     $this->SaveState();
    307   }
    308 
    309   function DisableAll()
    310   {
    311     $this->Perform($this->Modules, array(ModuleAction::Disable));
    312     $this->SaveState();
    313   }
    314 
    315   function ModulePresent($Name)
    316   {
    317     return array_key_exists($Name, $this->Modules);
    318   }
    319 
    320   function ModuleEnabled($Name)
    321   {
    322     return array_key_exists($Name, $this->Modules) and $this->Modules[$Name]->Enabled;
    323   }
    324 
    325   function ModuleRunning($Name)
    326   {
    327     return array_key_exists($Name, $this->Modules) and $this->Modules[$Name]->Running;
    328   }
    329 
    330   /* @return Module */
    331   function SearchModuleById($Id)
    332   {
    333     foreach ($this->Modules as $Module)
    334     {
    335       //DebugLog($Module->Name.' '.$Module->Id);
    336       if ($Module->Id == $Id) return $Module->Name;
    337     }
    338     return '';
    339   }
    340 
    341   function LoadState()
    342   {
    343     $ConfigModules = array();
    344     include($this->FileName);
    345     foreach ($ConfigModules as $Mod)
    346     {
    347       if (array_key_exists($Mod['Name'], $this->Modules))
    348       {
    349         $this->Modules[$Mod['Name']] = $this->Modules[$Mod['Name']];
    350         $this->Modules[$Mod['Name']]->Enabled = $Mod['Enabled'];
    351         $this->Modules[$Mod['Name']]->Installed = $Mod['Installed'];
    352         $this->Modules[$Mod['Name']]->InstalledVersion = $Mod['Version'];
    353       }
    354     }
    355   }
    356 
    357   function SaveState()
    358   {
    359     $Data = array();
    360     foreach ($this->Modules as $Module)
    361     {
    362       $Data[] = array('Name' => $Module->Name, 'Enabled' => $Module->Enabled,
    363         'Version' => $Module->Version, 'Installed' => $Module->Installed);
    364     }
    365     file_put_contents($this->FileName, "<?php \n\n\$ConfigModules = ".var_export($Data, true).";\n");
    366   }
    367 
    368   function RegisterModule(AppModule $Module)
    369   {
    370     $this->Modules[$Module->Name] = &$Module;
    371     $Module->Manager = &$this;
    372     $Module->OnChange = &$this->OnModuleChange;
    373   }
    374 
    375   function UnregisterModule(AppModule $Module)
    376   {
    377     unset($this->Modules[array_search($Module, $this->Modules)]);
    378   }
    379 
    380   function LoadModulesFromDir($Directory)
    381   {
    382     $List = scandir($Directory);
    383     foreach ($List as $Item)
    384     {
    385       if (is_dir($Directory.'/'.$Item) and ($Item != '.') and ($Item != '..') and ($Item != '.svn'))
    386       {
    387         include_once($Directory.'/'.$Item.'/'.$Item.'.php');
    388         $ModuleName = 'Module'.$Item;
    389         $this->RegisterModule(new $ModuleName($this->System));
    390       }
    391     }
    392   }
    393 
    394   function LoadModules(): void
    395   {
    396     if (is_array($this->OnLoadModules) and (count($this->OnLoadModules) == 2) and method_exists($this->OnLoadModules[0], $this->OnLoadModules[1]))
    397       call_user_func($this->OnLoadModules);
    398     else $this->LoadModulesFromDir($this->ModulesDir);
    399   }
    400 }
     262}
Note: See TracChangeset for help on using the changeset viewer.