oxmodulelist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxModuleList extends oxSuperCfg
00008 {
00014     protected $_aModule = array();
00015 
00021     protected $_aSkipFiles = array( "functions.php", "vendormetadata.php" );
00022 
00023 
00029     public function getAllModules()
00030     {
00031         return $this->getConfig()->getAllModules();
00032     }
00033 
00039     public function getActiveModuleInfo()
00040     {
00041         $aModules       = $this->getAllModules();
00042         $aModulePaths   = $this->getModulePaths();
00043 
00044         if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
00045            // $aModulePaths = array();
00046             if ( is_array($aModules) && count($aModules) > 0 ) {
00047                 foreach ($aModules as $aModuleClasses) {
00048                     foreach ($aModuleClasses as $sModule) {
00049                         $sModuleId = substr( $sModule, 0, strpos( $sModule, "/" ) );
00050                         $aModulePaths[$sModuleId] = $sModuleId;
00051                     }
00052                 }
00053             }
00054         }
00055 
00056         $aDisabledModules = $this->getDisabledModules();
00057         if ( is_array($aDisabledModules) && count($aDisabledModules) > 0  && count($aModulePaths) > 0 ) {
00058             $aModulePaths = array_diff_key($aModulePaths, array_flip($aDisabledModules));
00059         }
00060         return $aModulePaths;
00061     }
00062 
00068     public function getLegacyModules()
00069     {
00070         return $this->getConfig()->getConfigParam('aLegacyModules');
00071     }
00072 
00078     public function getDisabledModules()
00079     {
00080         return $this->getConfig()->getConfigParam('aDisabledModules');
00081     }
00082 
00088     public function getModulePaths()
00089     {
00090         return $this->getConfig()->getConfigParam('aModulePaths');
00091     }
00092 
00098     public function getModuleFiles()
00099     {
00100         return $this->getConfig()->getConfigParam('aModuleFiles');
00101     }
00102 
00108     public function getModuleTemplates()
00109     {
00110         return $this->getConfig()->getConfigParam('aModuleTemplates');
00111     }
00112 
00121     public function getDisabledModuleClasses()
00122     {
00123         $aDisabledModules = $this->getDisabledModules();
00124         $aModules         = $this->getAllModules();
00125         $aModulePaths     = $this->getModulePaths();
00126 
00127         $aDisabledModuleClasses = array();
00128         if (isset($aDisabledModules) && is_array($aDisabledModules)) {
00129             //get all disabled module paths
00130             foreach ($aDisabledModules as $sId) {
00131                 $sPath = $aModulePaths[$sId];
00132                 if (!isset($sPath)) {
00133                     $sPath = $sId;
00134                 }
00135                 foreach ( $aModules as $sClass => $aModuleClasses ) {
00136                     foreach ( $aModuleClasses as $sModuleClass ) {
00137                         if (strpos($sModuleClass, $sPath."/") === 0 ) {
00138                             $aDisabledModuleClasses[] = $sModuleClass;
00139                         }
00140                     }
00141                 }
00142             }
00143         }
00144 
00145         return $aDisabledModuleClasses;
00146     }
00147 
00153     public function cleanup()
00154     {
00155         $aDeletedExt = $this->getDeletedExtensions();
00156 
00157         //collecting deleted extension IDs
00158         $aDeletedExtIds = $this->getDeletedExtensionIds($aDeletedExt);
00159 
00160         // removing from aModules config varviable
00161         $this->_removeFromModulesArray( $aDeletedExt );
00162 
00163         // removing from aDisabledModules config varviable
00164         $this->_removeFromDisabledModulesArray( $aDeletedExtIds );
00165 
00166         // removing from aLegacyModules array
00167         $this->_removeFromLegacyModulesArray( $aDeletedExtIds );
00168 
00169         // removing from aModulePaths array
00170         $this->_removeFromModulesPathsArray( $aDeletedExtIds );
00171 
00172         // removing from aModuleFiles array
00173         $this->_removeFromModulesFilesArray( $aDeletedExtIds );
00174 
00175         // removing from aModuleTemplates array
00176         $this->_removeFromModulesTemplatesArray( $aDeletedExtIds );
00177 
00178         //removing from config tables and templates blocks table
00179         $this->_removeFromDatabase( $aDeletedExtIds );
00180     }
00181 
00189     public function getDeletedExtensionIds($aDeletedExt)
00190     {
00191         $aDeletedExtIds = array();
00192         if ( !empty($aDeletedExt) ) {
00193             $oModule = oxNew("oxModule");
00194             foreach ( $aDeletedExt as $sOxClass => $aDeletedModules ) {
00195                 foreach ( $aDeletedModules as $sModulePath ) {
00196                     $aDeletedExtIds[] = $oModule->getIdByPath($sModulePath);
00197                 }
00198             }
00199         }
00200 
00201         if ( !empty( $aDeletedExtIds ) ) {
00202             $aDeletedExtIds = array_unique( $aDeletedExtIds );
00203         }
00204 
00205         return $aDeletedExtIds;
00206     }
00207 
00214     public function getDeletedExtensions()
00215     {
00216         $aModules = $this->getAllModules();
00217 
00218         foreach ( $aModules as $sOxClass => $aModulesList ) {
00219             foreach ( $aModulesList as $sModulePath ) {
00220                 $sExtPath = $this->getConfig()->getModulesDir() . $sModulePath.".php";
00221                 if ( !file_exists( $sExtPath ) ) {
00222                     $aDeletedExt[$sOxClass][] = $sModulePath;
00223                 }
00224             }
00225         }
00226 
00227         return $aDeletedExt;
00228     }
00229 
00239     public function diffModuleArrays($aAllModuleArray, $aRemModuleArray)
00240     {
00241        if (is_array($aAllModuleArray) && is_array($aRemModuleArray)) {
00242             foreach ($aAllModuleArray as $sClass => $aModuleChain) {
00243                 if (!is_array($aModuleChain)) {
00244                     $aModuleChain = array($aModuleChain);
00245                 }
00246                 if (isset($aRemModuleArray[$sClass])) {
00247                     if (!is_array($aRemModuleArray[$sClass])) {
00248                         $aRemModuleArray[$sClass] = array($aRemModuleArray[$sClass]);
00249                     }
00250                     $aAllModuleArray[$sClass] = array();
00251                     foreach ($aModuleChain as $sModule) {
00252                         if (!in_array($sModule, $aRemModuleArray[$sClass])) {
00253                             $aAllModuleArray[$sClass][] = $sModule;
00254                         }
00255                     }
00256                     if (!count($aAllModuleArray[$sClass])) {
00257                         unset ($aAllModuleArray[$sClass]);
00258                     }
00259                 } else {
00260                     $aAllModuleArray[$sClass] = $aModuleChain;
00261                 }
00262             }
00263 
00264        }
00265 
00266         return $aAllModuleArray;
00267     }
00268 
00276     public function buildModuleChains($aModuleArray)
00277     {
00278         $aModules = array();
00279         if (is_array($aModuleArray)) {
00280             foreach ($aModuleArray as $sClass => $aModuleChain) {
00281                 $aModules[$sClass] = implode('&', $aModuleChain);
00282             }
00283         }
00284         return $aModules;
00285     }
00286 
00294     protected function _removeFromModulesArray( $aDeletedExt )
00295     {
00296         $aExt = $this->getAllModules();
00297         $aUpdatedExt = $this->diffModuleArrays( $aExt, $aDeletedExt );
00298         $aUpdatedExt = $this->buildModuleChains( $aUpdatedExt );
00299 
00300         $this->getConfig()->saveShopConfVar( 'aarr', 'aModules', $aUpdatedExt );
00301     }
00302 
00310     protected function _removeFromDisabledModulesArray( $aDeletedExtIds )
00311     {
00312         $oConfig = $this->getConfig();
00313         $aDisabledExtensionIds = $this->getDisabledModules();
00314         $aDisabledExtensionIds = array_diff($aDisabledExtensionIds, $aDeletedExtIds);
00315         $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledExtensionIds );
00316     }
00317 
00325     protected function _removeFromLegacyModulesArray( $aDeletedExtIds )
00326     {
00327         $aLegacyExt = $this->getLegacyModules();
00328 
00329         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00330             if ( isset($aLegacyExt[$sDeletedExtId]) ) {
00331                 unset( $aLegacyExt[$sDeletedExtId] );
00332             }
00333         }
00334 
00335         $this->getConfig()->saveShopConfVar( 'aarr', 'aLegacyModules', $aLegacyExt );
00336     }
00337 
00345     protected function _removeFromModulesPathsArray( $aDeletedModule )
00346     {
00347         $aModulePaths = $this->getModulePaths();
00348 
00349         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00350             if ( isset($aModulePaths[$sDeletedModuleId]) ) {
00351                 unset( $aModulePaths[$sDeletedModuleId] );
00352             }
00353         }
00354 
00355         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00356     }
00357 
00365     protected function _removeFromModulesFilesArray( $aDeletedModule )
00366     {
00367         $aModuleFiles = $this->getModuleFiles();
00368 
00369         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00370             if ( isset($aModuleFiles[$sDeletedModuleId]) ) {
00371                 unset( $aModuleFiles[$sDeletedModuleId] );
00372             }
00373         }
00374 
00375         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleFiles', $aModuleFiles );
00376     }
00377 
00385     protected function _removeFromModulesTemplatesArray( $aDeletedModule )
00386     {
00387         $aModuleTemplates = $this->getModuleTemplates();
00388 
00389         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00390             if ( isset($aModuleTemplates[$sDeletedModuleId]) ) {
00391                 unset( $aModuleTemplates[$sDeletedModuleId] );
00392             }
00393         }
00394 
00395         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleTemplates', $aModuleTemplates );
00396     }
00397 
00405     protected function _removeFromDatabase( $aDeletedExtIds )
00406     {
00407         if ( !is_array($aDeletedExtIds) || !count($aDeletedExtIds) ) {
00408             return;
00409         }
00410         $sDelExtIds = array();
00411 
00412         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00413             $aConfigIds[] = "'module:" . $sDeletedExtId . "'";
00414             $sDelExtIds[] = "'" . $sDeletedExtId . "'";
00415         }
00416 
00417         $sConfigIds = implode( ", ", $aConfigIds );
00418         $sDelExtIds = implode( ", ", $sDelExtIds );
00419 
00420         $aSql[] = "DELETE FROM oxconfig where oxmodule IN ($sConfigIds)";
00421         $aSql[] = "DELETE FROM oxconfigdisplay where oxcfgmodule IN ($sConfigIds)";
00422         $aSql[] = "DELETE FROM oxtplblocks where oxmodule IN ($sDelExtIds)";
00423 
00424         $oDb = oxDb::getDb();
00425         foreach ( $aSql as $sQuery ) {
00426             $oDb->execute( $sQuery );
00427         }
00428     }
00429 
00439     public function getModulesFromDir( $sModulesDir, $sVendorDir = null )
00440     {
00441         $sModulesDir  = oxUtilsFile::getInstance()->normalizeDir( $sModulesDir );
00442 
00443         foreach ( glob( $sModulesDir."*" ) as $sModuleDirPath ) {
00444 
00445             $sModuleDirPath .= ( is_dir( $sModuleDirPath ) ) ? "/" : "";
00446             $sModuleDirName  = basename( $sModuleDirPath );
00447 
00448             // skipping some file
00449             if ( in_array( $sModuleDirName, $this->_aSkipFiles ) || (!is_dir( $sModuleDirPath ) && substr($sModuleDirName, -4) != ".php")) {
00450                 continue;
00451             }
00452 
00453             if ( $this->_isVendorDir( $sModuleDirPath ) ) {
00454                 // scaning modules vendor directory
00455                 $this->getModulesFromDir( $sModuleDirPath, basename( $sModuleDirPath ) );
00456             } else {
00457                 // loading module info
00458                 $oModule = oxNew( "oxModule" );
00459                 $sModuleDirName = ( !empty($sVendorDir) ) ? $sVendorDir."/".$sModuleDirName : $sModuleDirName;
00460                 $oModule->loadByDir( $sModuleDirName );
00461                 $sModuleId = $oModule->getId();
00462                 $this->_aModules[$sModuleId] = $oModule;
00463 
00464                 $aModulePaths = $this->getModulePaths();
00465 
00466                 if ( !is_array($aModulePaths) || !array_key_exists( $sModuleId, $aModulePaths ) ) {
00467                     // saving module path info
00468                     $this->_saveModulePath( $sModuleId, $sModuleDirName );
00469 
00470                     //checking if this is new module and if it extends any eshop class
00471                     if ( !$this->_extendsClasses( $sModuleDirName ) ) {
00472                         // if not - marking it as disabled by default
00473                         $oModule->deactivate();
00474                     }
00475                 }
00476             }
00477         }
00478 
00479         return $this->_aModules;
00480     }
00481 
00489     protected function _isVendorDir( $sModuleDir )
00490     {
00491         if ( is_dir( $sModuleDir ) && file_exists( $sModuleDir . "vendormetadata.php" ) ) {
00492             return true;
00493         }
00494 
00495         return false;
00496     }
00497 
00505     protected function _extendsClasses ( $sModuleDir )
00506     {
00507         $aModules = $this->getConfig()->getConfigParam( "aModules" );
00508         if (is_array($aModules)) {
00509             $sModules = implode( "&", $aModules );
00510 
00511             if ( preg_match("@(^|&+)".$sModuleDir."\b@", $sModules ) ) {
00512                 return true;
00513             }
00514         }
00515 
00516         return false;
00517     }
00518 
00527     protected function _saveModulePath( $sModuleId, $sModulePath )
00528     {
00529         $aModulePaths = $this->getModulePaths();
00530 
00531         $aModulePaths[$sModuleId] = $sModulePath;
00532         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00533     }
00534 
00535 }