Ignore:
Timestamp:
Oct 10, 2012, 9:29:20 AM (12 years ago)
Author:
chronos
Message:
  • Upraveno: Modulární systém přepracován tak, že základní modul System je zodpovědný za udržování stavu instalování modulů v databázi a je instalován samostatně jako první modul. Následně lze instalovat moduly dle závislostí.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Modular/Common/Module.php

    r405 r424  
    1414  var $Models = array();
    1515  var $SupportedModels = array();
     16  /** @var Database */
    1617  var $Database;
    1718  var $Installed;
    18   var $Initialized;
    19   var $System;
     19  var $Running;
     20  /** @var ModularSystem */
     21  var $ModularSystem;
     22 
    2023 
    2124  function __construct($Database, $System)
     
    2326    $this->Database = &$Database;   
    2427    $this->System = &$System;   
    25     $this->Initialized = false;
     28    $this->Running = false;
    2629    $this->Installed = false;
    2730  }
     
    2932  function Install()
    3033  {
    31     if($this->Installed) return;
     34    if($this->IsInstalled()) return;
    3235    DebugLog('Installing module '.$this->Name.'...');
    3336    $this->Installed = true;
     
    3538    // Install dependencies first
    3639    foreach($this->Dependencies as $Dependency) 
    37       $this->System->Modules[$Dependency]->Install();
    38 
    39     $this->LoadModels();
    40     foreach($this->Models as $Index => $Module)
    41     {
    42       $this->Models[$Index]->Install();
    43     }
    44     $this->Database->query('UPDATE SystemModule SET Installed=1 WHERE Name="'.$this->Name.'"');
     40      $this->ModularSystem->Modules[$Dependency]->Install();
     41    $this->DoChange();
    4542  }
    4643 
    4744  function UnInstall()
    4845  {
     46    if($this->IsInstalled() == false) return;
    4947    DebugLog('Uninstalling module '.$this->Name.'...');
     48    if($this->Running) $this->Stop();
    5049    $this->Installed = false;
    5150   
     
    5554        if(($Dependency == $this->Name) and ($Module->Installed)) $this->System->Modules[$Module->Name]->UnInstall();
    5655       
     56    $this->DoChange();
     57  }
     58 
     59  function IsInstalled()
     60  {
     61    return($this->Installed);
     62  }
     63 
     64  function Start()
     65  {
     66    if($this->Running) return;
     67    if($this->IsInstalled() == false) return;
     68    $this->Running = true;
     69    foreach($this->Dependencies as $Dependency) 
     70      $this->System->Modules[$Dependency]->Start();
    5771    $this->LoadModels();
    58     foreach(array_reverse($this->Models, true) as $Index => $Model)
    59     {
    60       $this->Models[$Index]->UnInstall();
    61     }
    62     $this->Database->query('UPDATE SystemModule SET Installed=0 WHERE Name="'.$this->Name.'"');
    63   }
    64  
    65   function Init()
    66   {
    67     if($this->Initialized) return;
    68     $this->Initialized = true;
    69     foreach($this->Dependencies as $Dependency) 
    70       $this->System->Modules[$Dependency]->Init();
     72    $this->DoChange();
    7173  }
    7274 
     75  function Stop()
     76  {
     77    if(!$this->Running) return;
     78    $this->Running = false;
     79    foreach($this->System->Modules as $Module) 
     80      foreach($Module->Dependencies as $Dependency)
     81        if(($Dependency == $this->Name) and ($Module->Running))
     82          $this->System->Modules[$Module->Name]->Stop();
     83    $this->DoChange();
     84  }
     85 
     86  private function DoChange()
     87  {
     88    if($this->ModularSystem->OnModuleChange)
     89      call_user_func_array($this->ModularSystem->OnModuleChange, array($this));
     90  }
     91 
     92  function RegisterModel($ModelName)
     93  {
     94    $this->SupportedModels[] = $ModelName;
     95  }
     96 
     97  function UnregisterModel($ModelName)
     98  {
     99    unset($this->SupportedModels[$ModelName]); 
     100  }
     101
    73102  function LoadModels()
    74103  {
     
    85114class ModularSystem
    86115{
     116  /** @var Database */
    87117  var $Database;
    88118  var $Modules = array();
    89119  var $Models = array();
    90120  var $Menu = array();
     121  var $OnModuleChange;
    91122 
    92123  function __construct($Database)
     
    95126  }
    96127 
    97   function ModulePresent($Name)
    98   {
    99     return(array_key_exists($Name, $this->Modules));
    100   }
    101 
    102   function LoadModules($Installed = true)
    103   {
    104     //DebugLog('Loading modules...');
    105     $this->Modules = array();
    106     $Query = 'SELECT `Id`, `Name`,`Installed` FROM `SystemModule`';
    107     if($Installed) $Query .= ' WHERE `Installed`=1';
    108       else $Query .= ''; // WHERE `Installed`=0';
    109     $DbResult = $this->Database->query($Query);
    110     while($Module = $DbResult->fetch_array())
    111     {
    112       //echo($Module['Name'].',');
    113       include_once('Modules/'.$Module['Name'].'/'.$Module['Name'].'.php');
    114       $ModuleClassName = 'Module'.$Module['Name'];
    115       $NewModule = new $ModuleClassName($this->Database, $this);     
    116       $NewModule->Id = $Module['Id'];
    117       $NewModule->Installed = $Module['Installed'];
    118       $this->Modules[$Module['Name']] = $NewModule;
    119     }     
    120   }
    121  
    122   function Init()
     128  function StartAll()
    123129  {
    124130    $this->LoadModules();
     
    126132    {
    127133      //DebugLog('Init module '.$Module->Name);
    128       $this->Modules[$Index]->Init();
    129     }
    130   }
    131  
     134      $this->Modules[$Index]->Start();
     135    }
     136  }
     137 
     138  function StopAll()
     139  {
     140    foreach($this->Modules as $Index => $Module)
     141    {
     142      //DebugLog('Init module '.$Module->Name);
     143      $this->Modules[$Index]->Stop();
     144    }
     145  }
     146
    132147  function Install()
    133148  {
    134149    //DebugLog('Installing modular system core...');
    135     $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (
    136   `Id` int(11) NOT NULL AUTO_INCREMENT,
    137   `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    138   `Description` datetime NOT NULL,
    139   PRIMARY KEY (`Id`)
    140 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
    141     $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModule` (
    142   `Id` int(11) NOT NULL AUTO_INCREMENT,
    143   `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    144   `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    145   `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    146   `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    147   `Installed` int(11) NOT NULL,
    148   `Description` text COLLATE utf8_czech_ci NOT NULL,
    149   PRIMARY KEY (`Id`)
    150 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
    151    
    152     $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModuleDependency` (
    153   `Id` int(11) NOT NULL AUTO_INCREMENT,
    154   `Module` int(11) NOT NULL,
    155   `DependencyModule` int(11) NOT NULL,
    156   PRIMARY KEY (`Id`),
    157   KEY (`Module`),
    158   KEY (`DependencyModule`),
    159   UNIQUE (`Module` , `DependencyModule`)
    160 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
    161     $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
    162     $this->Database->query('ALTER TABLE `SystemModuleDependency` ADD CONSTRAINT `SystemModuleDependency_ibfk_2` FOREIGN KEY ( `DependencyModule` ) REFERENCES `SystemModule` (`Id`)');
    163    
    164     $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModel` (
    165   `Id` int(11) NOT NULL AUTO_INCREMENT,
    166   `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    167   `Module` int(11) NOT NULL,
    168   KEY (`Module`),
    169   PRIMARY KEY (`Id`)
    170 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
    171     $this->Database->query('ALTER TABLE `SystemModel` ADD CONSTRAINT `SystemModel_ibfk_1` FOREIGN KEY ( `Module` ) REFERENCES `SystemModule` (`Id`)');
    172    
    173     $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemModelProperty` (
    174   `Id` int(11) NOT NULL AUTO_INCREMENT,
    175   `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    176   `Type` varchar(255) COLLATE utf8_czech_ci NOT NULL,
    177   `Model` int(11) NOT NULL,
    178   KEY (`Model`),
    179   PRIMARY KEY (`Id`)
    180 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1;');
    181     $this->Database->query('ALTER TABLE `SystemModelProperty` ADD CONSTRAINT `SystemModelProperty_ibfk_1` FOREIGN KEY ( `Model` ) REFERENCES `SystemModel` (`Id`)');
    182150   
    183151    $this->ReloadList();
     
    190158  }
    191159 
    192   function UnInstall()
     160  function UnInstallAll()
    193161  {
    194162    DebugLog('Uninstalling modular system core...');
    195     foreach(array_reverse($this->Modules, true) as $Index => $Module)
    196       $this->Modules[$Index]->UnInstall();
    197    
    198     // Delete tables with reverse order
    199     $this->Database->query('ALTER TABLE `SystemModelProperty` DROP FOREIGN KEY `SystemModelProperty_ibfk_1`');
    200     $this->Database->query('DROP TABLE IF EXISTS `SystemModelProperty`');
    201     $this->Database->query('ALTER TABLE `SystemModel` DROP FOREIGN KEY `SystemModel_ibfk_1`');
    202     $this->Database->query('DROP TABLE IF EXISTS `SystemModel`');
    203     $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_1`');
    204     $this->Database->query('ALTER TABLE `SystemModuleDependency` DROP FOREIGN KEY `SystemModuleDependency_ibfk_2`');
    205     $this->Database->query('DROP TABLE IF EXISTS `SystemModuleDependency`');
    206     $this->Database->query('DROP TABLE IF EXISTS `SystemModule`');
    207     $this->Database->query('DROP TABLE IF EXISTS `SystemVersion`');
    208   }
    209  
    210   function IsInstalled()
    211   {
    212     $DbResult = $this->Database->query('SELECT table_name FROM information_schema.tables
    213 WHERE table_schema = "'.$this->Database->Database.'" AND table_name = "SystemVersion";');   
    214     if($DbResult->num_rows > 0) return(true);
    215       else return(false);
    216   }
    217  
    218   function ReloadList()
    219   {
    220     // Load list of modules from database
    221     $Modules = array();
    222     $DbResult = $this->Database->query('SELECT * FROM `SystemModule`');
    223     while($DbRow = $DbResult->fetch_assoc())
    224       $Modules[$DbRow['Name']] = $DbRow;
    225    
     163    foreach($this->Modules as $Index => $Module)
     164    {
     165      //DebugLog('Init module '.$Module->Name);
     166      $this->Modules[$Index]->Uninstall();
     167    }
     168  }
     169 
     170  /* @return Module */
     171  function SearchModuleById($Id)
     172  {
     173    foreach($this->Modules as $Module)
     174    {
     175      //DebugLog($Module->Name.' '.$Module->Id);
     176      if($Module->Id == $Id) return($Module->Name);
     177    }       
     178    return('');
     179  }
     180
     181  function ModulePresent($Name)
     182  {
     183    return(array_key_exists($Name, $this->Modules));
     184  }
     185 
     186  function RegisterModule(Module $Module)
     187  {
     188    $this->Modules[$Module->Name] = &$Module; 
     189    $Module->ModularSystem = $this;
     190    $Module->OnChange = &$this->OnModuleChange;
     191  }
     192 
     193  function UnregisterModule($Module)
     194  {
     195    unset($this->Modules[array_search($Module, $this->Modules)]); 
     196  }
     197 
     198  function ReloadFromDisk()
     199  {   
    226200    // Load list of modules on disk
    227201    $ModulesOnDisk = array();
     
    235209    // Add new
    236210    foreach($ModulesOnDisk as $ModuleName)
    237     if(!array_key_exists($ModuleName, $Modules))
    238     {
    239       DebugLog('Adding module '.$ModuleName.' to list');
     211    if(!array_key_exists($ModuleName, $this->Modules))
     212    {
     213      //DebugLog('Adding module '.$ModuleName.' to list');
    240214      include_once('Modules/'.$ModuleName.'/'.$ModuleName.'.php');
    241215      $ModuleClassName = 'Module'.$ModuleName;
    242216      if(class_exists($ModuleClassName))
    243217      {
    244         $Module = new $ModuleClassName($this->Database, $this);
    245         $this->Database->insert('SystemModule', array('Name' => $Module->Name,
    246           'Version' => $Module->Version, 'Creator' => $Module->Creator,
    247           'Description' => $Module->Description, 'License' => $Module->License,
    248           'Installed' => 0));
    249         unset($Module);
     218        $NewModule = new $ModuleClassName($this->Database, $this);
     219        $this->RegisterModule($NewModule);
    250220      } else throw new Exception('Missing class '.$ModuleClassName.' in module '.$ModuleName);
    251     }
     221    }   
    252222   
    253     // Remove missing
    254     foreach($Modules as $Module)
    255     if(($Module['Installed'] == 0) and !in_array($Module['Name'], $ModulesOnDisk))
    256     {
    257       DebugLog('Removing module '.$Module['Name'].' from list');
    258       $this->Database->query('DELETE FROM `SystemModule` WHERE `Id` = '.$Module['Id']);
    259     }
    260    
    261     // Reload dependencies
    262     $this->LoadModules(false);
    263223    foreach($this->Modules as $Module)
    264     {
    265       foreach($Module->Dependencies as $Dependency)
    266       {
    267         $this->Database->insert('SystemModuleDependency', array('Module' => $Module->Id,
    268           'DependencyModule' => $this->Modules[$Dependency]->Id));
    269       }
    270     }
    271   }
    272  
    273   function SearchModuleById($Id)
    274   {
    275     foreach($this->Modules as $Module)
    276     {
    277       DebugLog($Module->Name.' '.$Module->Id);
    278       if($Module->Id == $Id) return($Module->Name);
    279     }       
    280     return('');
    281   }
     224    if(array_key_exists($Module->Name, $ModulesOnDisk))
     225    {
     226      $this->ModularSystem->UnregisterModule($Module);
     227    }
     228  } 
    282229}
    283230
Note: See TracChangeset for help on using the changeset viewer.