Ignore:
Timestamp:
Jan 12, 2021, 10:29:50 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Setup is now AppModule and it is installed and stated as first module.
  • Modified: Improved modular system.
File:
1 edited

Legend:

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

    r894 r895  
    3232  const Running = 5;
    3333  const NotRunning = 6;
     34  const System = 7;
     35  const User = 8;
    3436}
    3537
     
    5557  public $OnChange;
    5658  public array $Models;
     59  public bool $SystemModule;
    5760
    5861  function __construct(Application $System)
     
    7174    $this->Type = ModuleType::Normal;
    7275    $this->Models = array();
     76    $this->SystemModule = false;
    7377  }
    7478
     
    8488    $this->Manager->EnumDependenciesCascade($this, $List, array(ModuleCondition::NotInstalled));
    8589    $this->Manager->Perform($List, array(ModuleAction::Install), array(ModuleCondition::NotInstalled));
     90    $this->DoBeforeInstall();
    8691    $this->InstallModels();
    8792    $this->DoInstall();
    8893    $this->Installed = true;
     94    $this->Enabled = true; // Automatically enable installed module
    8995    $this->InstalledVersion = $this->Version;
    9096    $this->Manager->Modules[$this->Name] = $this;
     
    101107    $this->DoUninstall();
    102108    $this->UninstallModels();
     109    $this->DoAfterUninstall();
    103110  }
    104111
     
    175182  }
    176183
     184  protected function DoBeforeInstall(): void
     185  {
     186  }
     187
    177188  protected function DoInstall(): void
    178189  {
     
    183194  }
    184195
    185   protected function DoUpgrade(): void
    186   {
     196  protected function DoAfterUninstall(): void
     197  {
     198  }
     199
     200  protected function DoUpgrade(): string
     201  {
     202    return '';
    187203  }
    188204
     
    194210  function InstallModels(): void
    195211  {
    196     foreach ($this->GetModels() as $Model)
    197     {
    198       $this->InstallModel($Model::GetDesc());
     212    if ($this->Manager->OnInstallModel != null)
     213    {
     214      foreach ($this->GetModels() as $Model)
     215      {
     216        call_user_func($this->Manager->OnInstallModel, $Model::GetDesc());
     217      }
    199218    }
    200219  }
     
    202221  function UninstallModels(): void
    203222  {
    204     foreach (array_reverse($this->GetModels()) as $Model)
    205     {
    206       $this->UninstallModel($Model::GetDesc());
    207     }
    208   }
    209 
    210   function InstallModel(ModelDesc $ModelDesc)
    211   {
    212     $Query = "CREATE TABLE IF NOT EXISTS `".$ModelDesc->Name."` (\n";
    213     $Query .= '  `'.$ModelDesc->PrimaryKey.'` int(11) NOT NULL AUTO_INCREMENT,'."\n";
    214     foreach ($ModelDesc->Columns as $Column)
    215     {
    216       $Query .= "  `".$Column->Name."` ";
    217       if ($Column->Type == ModelColumnType::Integer) $Query .= 'int(11)';
    218       else if ($Column->Type == ModelColumnType::String) $Query .= 'varchar(255)';
    219       else if ($Column->Type == ModelColumnType::Float) $Query .= 'varchar(255)';
    220       else if ($Column->Type == ModelColumnType::Text) $Query .= 'text';
    221       else if ($Column->Type == ModelColumnType::DateTime) $Query .= 'datetime';
    222       else if ($Column->Type == ModelColumnType::Reference) $Query .= 'int(11)';
    223       else if ($Column->Type == ModelColumnType::Boolean) $Query .= 'tinyint(1)';
    224       else if ($Column->Type == ModelColumnType::Date) $Query .= 'date';
    225       else if ($Column->Type == ModelColumnType::BigInt) $Query .= 'bigint(20)';
    226       else if ($Column->Type == ModelColumnType::Enum)
    227       {
    228         $Query .= 'enum("'.implode('", "', $Column->States).'")';
    229       }
    230 
    231       if ($Column->Nullable) $Query .= '';
    232         else $Query .= ' NOT NULL';
    233 
    234       $Query .= ' COLLATE utf8_general_ci';
    235 
    236       if ($Column->HasDefault)
    237       {
    238         if ($Column->Default == null)
    239           $Query .= ' DEFAULT NULL';
    240         $Query .= ' DEFAULT '.$Column->GetDefault();
    241       }
    242       $Query .= ",\n";
    243     }
    244     $Query .= '  PRIMARY KEY (`'.$ModelDesc->PrimaryKey.'`)';
    245     foreach ($ModelDesc->Columns as $Column)
    246     {
    247       if ($Column->Type == ModelColumnType::Reference)
    248         $Query .= ','."\n".'  KEY `'.$Column->Name.'` (`'.$Column->Name.'`)';
    249       else if ($Column->Unique)
    250         $Query .= ','."\n".'  UNIQUE KEY `'.$Column->Name.'` (`'.$Column->Name.'`)';
    251     }
    252     $Query .= "\n";
    253 
    254     if ($ModelDesc->Memory) $Engine = 'MEMORY';
    255       else $Engine = 'InnoDB';
    256     $Query .= ') ENGINE='.$Engine.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;';
    257     $I = 1;
    258     foreach ($ModelDesc->Columns as $Column)
    259     {
    260       if ($Column->Type == ModelColumnType::Reference)
    261         $Query .= "ALTER TABLE `".$ModelDesc->Name."` ".
    262         "ADD CONSTRAINT `".$ModelDesc->Name."_ibfk_".$I."` FOREIGN KEY (`".$Column->Name."`) REFERENCES `".$Column->RefTable."` (`Id`);";
    263       $I++;
    264     }
    265     $this->Database->query($Query);
    266   }
    267 
    268   function UninstallModel(ModelDesc $ModelDesc)
    269   {
    270     $this->Database->query('DROP TABLE IF EXISTS `'.$ModelDesc->Name.'`');
     223    if ($this->Manager->OnUninstallModel != null)
     224    {
     225      foreach (array_reverse($this->GetModels()) as $Model)
     226      {
     227        call_user_func($this->Manager->OnUninstallModel, $Model::GetDesc());
     228      }
     229    }
    271230  }
    272231}
     
    280239  public string $ModulesDir;
    281240  public $OnLoadModules;
     241  public $OnInstallModel;
     242  public $OnUninstsallModel;
    282243
    283244  function __construct(System $System)
     
    287248    $this->FileName = dirname(__FILE__).'/../../Config/ModulesConfig.php';
    288249    $this->ModulesDir = dirname(__FILE__).'/../../Modules';
     250    $this->OnInstallModel = null;
     251    $this->OnUninstallModel = null;
    289252  }
    290253
    291254  function Perform(array $List, array $Actions, array $Conditions = array(ModuleCondition::All)): void
    292255  {
    293     foreach ($List as $Index => $Module)
     256    foreach ($List as $Module)
    294257    {
    295258      if (in_array(ModuleCondition::All, $Conditions) or
     
    299262        (!$Module->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or
    300263        ($Module->Enabled and in_array(ModuleCondition::Enabled, $Conditions)) or
    301         (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)))
     264        (!$Module->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
     265        ($Module->SystemModule and in_array(ModuleCondition::System, $Conditions)) or
     266        (!$Module->SystemModule and in_array(ModuleCondition::User, $Conditions)))
    302267      {
    303268        foreach ($Actions as $Action)
     
    328293        (!$DepModule->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
    329294        ($DepModule->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
    330         (!$DepModule->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)))
     295        (!$DepModule->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or
     296        ($DepModule->SystemModule and in_array(ModuleCondition::System, $Conditions)) or
     297        (!$DepModule->SystemModule and in_array(ModuleCondition::User, $Conditions)))
    331298      {
    332299        array_push($List, $DepModule);
     
    347314          (!$RefModule->Enabled and in_array(ModuleCondition::NotEnabled, $Conditions)) or
    348315          ($RefModule->Installed and in_array(ModuleCondition::Installed, $Conditions)) or
    349           (!$RefModule->Installed and in_array(ModuleCondition::NotInstalled, $Conditions))))
    350       {
     316          (!$RefModule->Installed and in_array(ModuleCondition::NotInstalled, $Conditions)) or
     317          ($RefModule->SystemModule and in_array(ModuleCondition::System, $Conditions)) or
     318          (!$RefModule->SystemModule and in_array(ModuleCondition::User, $Conditions))))
     319        {
    351320        array_push($List, $RefModule);
    352321        $this->EnumSuperiorDependenciesCascade($RefModule, $List, $Conditions);
     
    357326  function Start(): void
    358327  {
    359     $this->LoadModules();
    360     if (file_exists($this->FileName)) $this->LoadState();
    361     $this->StartEnabled();
     328    $ModuleSetup = $this->LoadModule(dirname(__FILE__).'/Setup.php');
     329    $this->LoadState();
     330    if (!$ModuleSetup->Installed)
     331    {
     332      $ModuleSetup->Install();
     333      $this->SaveState();
     334    }
     335    $ModuleSetup->Start();
     336    if (ModuleSetup::Cast($ModuleSetup)->CheckState())
     337    {
     338      $this->LoadModules();
     339      if (file_exists($this->FileName)) $this->LoadState();
     340      $this->InstallSystem();
     341      $this->StartSystem();
     342      $this->StartEnabled();
     343    }
    362344  }
    363345
     
    370352  {
    371353    $this->Perform($this->Modules, array(ModuleAction::Start), array(ModuleCondition::Enabled));
     354  }
     355
     356  function StartSystem(): void
     357  {
     358    $this->Perform($this->Modules, array(ModuleAction::Start), array(ModuleCondition::System));
    372359  }
    373360
     
    383370  }
    384371
     372  function InstallSystem(): void
     373  {
     374    $this->Perform($this->Modules, array(ModuleAction::Install), array(ModuleCondition::System));
     375    $this->SaveState();
     376  }
     377
    385378  function UninstallAll(): void
    386379  {
     
    425418    foreach ($this->Modules as $Module)
    426419    {
    427       //DebugLog($Module->Name.' '.$Module->Id);
    428420      if ($Module->Id == $Id) return $Module->Name;
    429421    }
     
    452444    foreach ($this->Modules as $Module)
    453445    {
    454       $Data[] = array('Name' => $Module->Name, 'Enabled' => $Module->Enabled,
    455         'Version' => $Module->Version, 'Installed' => $Module->Installed);
     446      $Data[] = array(
     447        'Name' => $Module->Name,
     448        'Enabled' => $Module->Enabled,
     449        'Version' => $Module->Version,
     450        'Installed' => $Module->Installed
     451      );
    456452    }
    457453    if (file_put_contents($this->FileName, "<?php \n\n\$ConfigModules = ".var_export($Data, true).";\n") === FALSE)
     
    473469  }
    474470
     471  function LoadModule(string $FileName): AppModule
     472  {
     473    include_once($FileName);
     474    $ModuleName = 'Module'.pathinfo($FileName, PATHINFO_FILENAME);
     475    $Module = new $ModuleName($this->System);
     476    $this->RegisterModule($Module);
     477    return $Module;
     478  }
     479
    475480  function LoadModulesFromDir(string $Directory): void
    476481  {
     
    480485      if (is_dir($Directory.'/'.$Item) and ($Item != '.') and ($Item != '..') and ($Item != '.svn'))
    481486      {
    482         include_once($Directory.'/'.$Item.'/'.$Item.'.php');
    483         $ModuleName = 'Module'.$Item;
    484         $this->RegisterModule(new $ModuleName($this->System));
     487        $this->LoadModule($Directory.'/'.$Item.'/'.$Item.'.php');
    485488      }
    486489    }
Note: See TracChangeset for help on using the changeset viewer.