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 
00101     public function getDisabledModuleClasses()
00102     {
00103         $aDisabledModules = $this->getDisabledModules();
00104         $aModules         = $this->getAllModules();
00105         $aModulePaths     = $this->getModulePaths();
00106 
00107         $aDisabledModuleClasses = array();
00108         if (isset($aDisabledModules) && is_array($aDisabledModules)) {
00109             //get all disabled module paths
00110             foreach ($aDisabledModules as $sId) {
00111                 $sPath = $aModulePaths[$sId];
00112                 if (!isset($sPath)) {
00113                     $sPath = $sId;
00114                 }
00115                 foreach ( $aModules as $sClass => $aModuleClasses ) {
00116                     foreach ( $aModuleClasses as $sModuleClass ) {
00117                         if (strpos($sModuleClass, $sPath."/") === 0 ) {
00118                             $aDisabledModuleClasses[] = $sModuleClass;
00119                         }
00120                     }
00121                 }
00122             }
00123         }
00124 
00125         return $aDisabledModuleClasses;
00126     }
00127 
00133     public function cleanup()
00134     {
00135         $aDeletedExt = $this->getDeletedExtensions();
00136 
00137         //collecting deleted extension IDs
00138         $aDeletedExtIds = $this->getDeletedExtensionIds($aDeletedExt);
00139 
00140         // removing from aModules config varviable
00141         $this->_removeFromModulesArray( $aDeletedExt );
00142 
00143         // removing from aDisabledModules config varviable
00144         $this->_removeFromDisabledModulesArray( $aDeletedExtIds );
00145 
00146         // removing from aLegacyModules array
00147         $this->_removeFromLegacyModulesArray( $aDeletedExtIds );
00148 
00149         //removing from config tables and templates blocks table
00150         $this->_removeFromDatabase( $aDeletedExtIds );
00151     }
00152 
00160     public function getDeletedExtensionIds($aDeletedExt)
00161     {
00162         $aDeletedExtIds = array();
00163         if ( !empty($aDeletedExt) ) {
00164             $oModule = oxNew("oxModule");
00165             foreach ( $aDeletedExt as $sOxClass => $aDeletedModules ) {
00166                 foreach ( $aDeletedModules as $sModulePath ) {
00167                     $aDeletedExtIds[] = $oModule->getIdByPath($sModulePath);
00168                 }
00169             }
00170         }
00171 
00172         if ( !empty( $aDeletedExtIds ) ) {
00173             $aDeletedExtIds = array_unique( $aDeletedExtIds );
00174         }
00175 
00176         return $aDeletedExtIds;
00177     }
00178 
00185     public function getDeletedExtensions()
00186     {
00187         $aModules = $this->getAllModules();
00188 
00189         foreach ( $aModules as $sOxClass => $aModulesList ) {
00190             foreach ( $aModulesList as $sModulePath ) {
00191                 $sExtPath = $this->getConfig()->getModulesDir() . $sModulePath.".php";
00192                 if ( !file_exists( $sExtPath ) ) {
00193                     $aDeletedExt[$sOxClass][] = $sModulePath;
00194                 }
00195             }
00196         }
00197 
00198         return $aDeletedExt;
00199     }
00200 
00210     public function diffModuleArrays($aAllModuleArray, $aRemModuleArray)
00211     {
00212        if (is_array($aAllModuleArray) && is_array($aRemModuleArray)) {
00213             foreach ($aAllModuleArray as $sClass => $aModuleChain) {
00214                 if (!is_array($aModuleChain)) {
00215                     $aModuleChain = array($aModuleChain);
00216                 }
00217                 if (isset($aRemModuleArray[$sClass])) {
00218                     if (!is_array($aRemModuleArray[$sClass])) {
00219                         $aRemModuleArray[$sClass] = array($aRemModuleArray[$sClass]);
00220                     }
00221                     $aAllModuleArray[$sClass] = array();
00222                     foreach ($aModuleChain as $sModule) {
00223                         if (!in_array($sModule, $aRemModuleArray[$sClass])) {
00224                             $aAllModuleArray[$sClass][] = $sModule;
00225                         }
00226                     }
00227                     if (!count($aAllModuleArray[$sClass])) {
00228                         unset ($aAllModuleArray[$sClass]);
00229                     }
00230                 } else {
00231                     $aAllModuleArray[$sClass] = $aModuleChain;
00232                 }
00233             }
00234 
00235        }
00236 
00237         return $aAllModuleArray;
00238     }
00239 
00247     public function buildModuleChains($aModuleArray)
00248     {
00249         $aModules = array();
00250         if (is_array($aModuleArray)) {
00251             foreach ($aModuleArray as $sClass => $aModuleChain) {
00252                 $aModules[$sClass] = implode('&', $aModuleChain);
00253             }
00254         }
00255         return $aModules;
00256     }
00257 
00265     protected function _removeFromModulesArray( $aDeletedExt )
00266     {
00267         $aExt = $this->getAllModules();
00268         $aUpdatedExt = $this->diffModuleArrays( $aExt, $aDeletedExt );
00269         $aUpdatedExt = $this->buildModuleChains( $aUpdatedExt );
00270 
00271         $this->getConfig()->saveShopConfVar( 'aarr', 'aModules', $aUpdatedExt );
00272     }
00273 
00281     protected function _removeFromDisabledModulesArray( $aDeletedExtIds )
00282     {
00283         $oConfig = $this->getConfig();
00284         $aDisabledExtensionIds = $this->getDisabledModules();
00285         $aDisabledExtensionIds = array_diff($aDisabledExtensionIds, $aDeletedExtIds);
00286         $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledExtensionIds );
00287     }
00288 
00296     protected function _removeFromLegacyModulesArray( $aDeletedExtIds )
00297     {
00298         $aLegacyExt = $this->getLegacyModules();
00299 
00300         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00301             if ( isset($aLegacyExt[$sDeletedExtId]) ) {
00302                 unset( $aLegacyExt[$sDeletedExtId] );
00303             }
00304         }
00305 
00306         $this->getConfig()->saveShopConfVar( 'aarr', 'aLegacyModules', $aLegacyExt );
00307     }
00308 
00316     protected function _removeFromDatabase( $aDeletedExtIds )
00317     {
00318         if ( !is_array($aDeletedExtIds) || !count($aDeletedExtIds) ) {
00319             return;
00320         }
00321         $sDelExtIds = array();
00322 
00323         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00324             $aConfigIds[] = "'module:" . $sDeletedExtId . "'";
00325             $sDelExtIds[] = "'" . $sDeletedExtId . "'";
00326         }
00327 
00328         $sConfigIds = implode( ", ", $aConfigIds );
00329         $sDelExtIds = implode( ", ", $sDelExtIds );
00330 
00331         $aSql[] = "DELETE FROM oxconfig where oxmodule IN ($sConfigIds)";
00332         $aSql[] = "DELETE FROM oxconfigdisplay where oxcfgmodule IN ($sConfigIds)";
00333         $aSql[] = "DELETE FROM oxtplblocks where oxmodule IN ($sDelExtIds)";
00334 
00335         $oDb = oxDb::getDb();
00336         foreach ( $aSql as $sQuery ) {
00337             $oDb->execute( $sQuery );
00338         }
00339     }
00340 
00350     public function getModulesFromDir( $sModulesDir, $sVendorDir = null )
00351     {
00352         $sModulesDir  = oxUtilsFile::getInstance()->normalizeDir( $sModulesDir );
00353 
00354         foreach ( glob( $sModulesDir."*" ) as $sModuleDirPath ) {
00355 
00356             $sModuleDirPath .= ( is_dir( $sModuleDirPath ) ) ? "/" : "";
00357             $sModuleDirName  = basename( $sModuleDirPath );
00358 
00359             // skipping some file
00360             if ( in_array( $sModuleDirName, $this->_aSkipFiles ) || (!is_dir( $sModuleDirPath ) && substr($sModuleDirName, -4) != ".php")) {
00361                 continue;
00362             }
00363 
00364             if ( $this->_isVendorDir( $sModuleDirPath ) ) {
00365                 // scaning modules vendor directory
00366                 $this->getModulesFromDir( $sModuleDirPath, basename( $sModuleDirPath ) );
00367             } else {
00368                 // loading module info
00369                 $oModule = oxNew( "oxModule" );
00370                 $sModuleDirName = ( !empty($sVendorDir) ) ? $sVendorDir."/".$sModuleDirName : $sModuleDirName;
00371                 $oModule->loadByDir( $sModuleDirName );
00372                 $sModuleId = $oModule->getId();
00373                 $this->_aModules[$sModuleId] = $oModule;
00374 
00375                 $aModulePaths = $this->getModulePaths();
00376 
00377                 if ( !is_array($aModulePaths) || !array_key_exists( $sModuleId, $aModulePaths ) ) {
00378                     // saving module path info
00379                     $this->_saveModulePath( $sModuleId, $sModuleDirName );
00380 
00381                     //checking if this is new module and if it extends any eshop class
00382                     if ( !$this->_extendsClasses( $sModuleDirName ) ) {
00383                         // if not - marking it as disabled by default
00384                         $oModule->deactivate();
00385                     }
00386                 }
00387             }
00388         }
00389 
00390         return $this->_aModules;
00391     }
00392 
00400     protected function _isVendorDir( $sModuleDir )
00401     {
00402         if ( is_dir( $sModuleDir ) && file_exists( $sModuleDir . "vendormetadata.php" ) ) {
00403             return true;
00404         }
00405 
00406         return false;
00407     }
00408 
00416     protected function _extendsClasses ( $sModuleDir )
00417     {
00418         $aModules = $this->getConfig()->getConfigParam( "aModules" );
00419         if (is_array($aModules)) {
00420             $sModules = implode( "&", $aModules );
00421 
00422             if ( preg_match("@(^|&+)".$sModuleDir."\b@", $sModules ) ) {
00423                 return true;
00424             }
00425         }
00426 
00427         return false;
00428     }
00429 
00438     protected function _saveModulePath( $sModuleId, $sModulePath )
00439     {
00440         $aModulePaths = $this->getModulePaths();
00441 
00442         $aModulePaths[$sModuleId] = $sModulePath;
00443         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00444     }
00445 
00446 }