oxmodulelist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxModuleList extends oxSuperCfg
00008 {
00015     protected $_aModules = array();
00016 
00022     protected $_aSkipFiles = array( 'functions.php', 'vendormetadata.php' );
00023 
00029     public function getAllModules()
00030     {
00031         return $this->getConfig()->getAllModules();
00032     }
00033 
00039     public function getActiveModuleInfo()
00040     {
00041         $aModulePaths   = $this->getModulePaths();
00042 
00043         // Extract module paths from extended classes
00044         if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
00045             $aModulePaths = $this->extractModulePaths();
00046         }
00047 
00048         $aDisabledModules = $this->getDisabledModules();
00049         if ( is_array($aDisabledModules) && count($aDisabledModules) > 0  && count($aModulePaths) > 0 ) {
00050             $aModulePaths = array_diff_key($aModulePaths, array_flip($aDisabledModules));
00051         }
00052         return $aModulePaths;
00053     }
00054 
00060     public function getDisabledModuleInfo()
00061     {
00062         $aDisabledModules = $this->getDisabledModules();
00063         $aModulePaths     = array();
00064 
00065         if ( is_array($aDisabledModules) && count($aDisabledModules) > 0 ) {
00066             $aModulePaths   = $this->getModulePaths();
00067 
00068             // Extract module paths from extended classes
00069             if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
00070                 $aModulePaths = $this->extractModulePaths();
00071             }
00072 
00073             if ( is_array($aModulePaths) || count($aModulePaths) > 0 ) {
00074                 $aModulePaths = array_intersect_key($aModulePaths, array_flip($aDisabledModules));
00075             }
00076         }
00077 
00078         return $aModulePaths;
00079     }
00080 
00086     public function getModuleVersions()
00087     {
00088         return $this->getConfig()->getConfigParam('aModuleVersions');
00089     }
00090 
00096     public function getModules()
00097     {
00098         return $this->getConfig()->getConfigParam('aModules');
00099     }
00100 
00106     public function getLegacyModules()
00107     {
00108         return $this->getConfig()->getConfigParam('aLegacyModules');
00109     }
00110 
00116     public function getDisabledModules()
00117     {
00118         return $this->getConfig()->getConfigParam('aDisabledModules');
00119     }
00120 
00126     public function getModulePaths()
00127     {
00128         return $this->getConfig()->getConfigParam('aModulePaths');
00129     }
00130 
00136     public function getModuleEvents()
00137     {
00138         return (array) $this->getConfig()->getConfigParam('aModuleEvents');
00139     }
00140 
00146     public function extractModulePaths()
00147     {
00148         $aModules     = $this->getAllModules();
00149         $aModulePaths = array();
00150 
00151         if (is_array($aModules) && count($aModules) > 0) {
00152             foreach ($aModules as $aModuleClasses) {
00153                 foreach ($aModuleClasses as $sModule) {
00154                     $sModuleId = substr($sModule, 0, strpos($sModule, "/"));
00155                     $aModulePaths[$sModuleId] = $sModuleId;
00156                 }
00157             }
00158         }
00159         return $aModulePaths;
00160     }
00161 
00167     public function getModuleFiles()
00168     {
00169         return $this->getConfig()->getConfigParam('aModuleFiles');
00170     }
00171 
00177     public function getModuleTemplates()
00178     {
00179         return $this->getConfig()->getConfigParam('aModuleTemplates');
00180     }
00181 
00190     public function getDisabledModuleClasses()
00191     {
00192         $aDisabledModules = $this->getDisabledModules();
00193         $aModules         = $this->getAllModules();
00194         $aModulePaths     = $this->getModulePaths();
00195 
00196         $aDisabledModuleClasses = array();
00197         if (isset($aDisabledModules) && is_array($aDisabledModules)) {
00198             //get all disabled module paths
00199             foreach ($aDisabledModules as $sId) {
00200                 $sPath = $aModulePaths[$sId];
00201                 if (!isset($sPath)) {
00202                     $sPath = $sId;
00203                 }
00204                 foreach ( $aModules as $sClass => $aModuleClasses ) {
00205                     foreach ( $aModuleClasses as $sModuleClass ) {
00206                         if (strpos($sModuleClass, $sPath."/") === 0 ) {
00207                             $aDisabledModuleClasses[] = $sModuleClass;
00208                         }
00209                     }
00210                 }
00211             }
00212         }
00213 
00214         return $aDisabledModuleClasses;
00215     }
00216 
00222     public function cleanup()
00223     {
00224         $aDeletedExt = $this->getDeletedExtensions();
00225 
00226         //collecting deleted extension IDs
00227         $aDeletedExtIds = $this->getDeletedExtensionIds($aDeletedExt);
00228 
00229         // removing from aModules config array
00230         $this->_removeFromModulesArray( $aDeletedExt );
00231 
00232         // removing from aDisabledModules array
00233         $this->_removeFromDisabledModulesArray( $aDeletedExtIds );
00234 
00235         // removing from aLegacyModules array
00236         $this->_removeFromLegacyModulesArray( $aDeletedExtIds );
00237 
00238         // removing from aModulePaths array
00239         $this->_removeFromModulesPathsArray( $aDeletedExtIds );
00240 
00241         // removing from aModuleEvents array
00242         $this->_removeFromModulesEventsArray( $aDeletedExtIds );
00243 
00244         // removing from aModuleVersions array
00245         $this->_removeFromModulesVersionsArray( $aDeletedExtIds );
00246 
00247         // removing from aModuleFiles array
00248         $this->_removeFromModulesFilesArray( $aDeletedExtIds );
00249 
00250         // removing from aModuleTemplates array
00251         $this->_removeFromModulesTemplatesArray( $aDeletedExtIds );
00252 
00253         //removing from config tables and templates blocks table
00254         $this->_removeFromDatabase( $aDeletedExtIds );
00255     }
00256 
00264     public function getDeletedExtensionIds($aDeletedExt)
00265     {
00266         $aDeletedExtIds = array();
00267         if ( !empty($aDeletedExt) ) {
00268             $oModule = oxNew('oxModule');
00269             foreach ( $aDeletedExt as $sOxClass => $aDeletedModules ) {
00270                 foreach ( $aDeletedModules as $sModulePath ) {
00271                     $aDeletedExtIds[] = $oModule->getIdByPath($sModulePath);
00272                 }
00273             }
00274         }
00275 
00276         if ( !empty( $aDeletedExtIds ) ) {
00277             $aDeletedExtIds = array_unique( $aDeletedExtIds );
00278         }
00279 
00280         return $aDeletedExtIds;
00281     }
00282 
00288     public function getDeletedExtensions()
00289     {
00290         $aModules = $this->getAllModules();
00291         $aDeletedExt = array();
00292 
00293         foreach ( $aModules as $sOxClass => $aModulesList ) {
00294             foreach ( $aModulesList as $sModulePath ) {
00295                 $sExtPath = $this->getConfig()->getModulesDir() . $sModulePath.'.php';
00296                 if ( !file_exists( $sExtPath ) ) {
00297                     $aDeletedExt[$sOxClass][] = $sModulePath;
00298                 }
00299             }
00300         }
00301 
00302         return $aDeletedExt;
00303     }
00304 
00314     public function diffModuleArrays($aAllModuleArray, $aRemModuleArray)
00315     {
00316        if (is_array($aAllModuleArray) && is_array($aRemModuleArray)) {
00317             foreach ($aAllModuleArray as $sClass => $aModuleChain) {
00318                 if (!is_array($aModuleChain)) {
00319                     $aModuleChain = array($aModuleChain);
00320                 }
00321                 if (isset($aRemModuleArray[$sClass])) {
00322                     if (!is_array($aRemModuleArray[$sClass])) {
00323                         $aRemModuleArray[$sClass] = array($aRemModuleArray[$sClass]);
00324                     }
00325                     $aAllModuleArray[$sClass] = array();
00326                     foreach ($aModuleChain as $sModule) {
00327                         if (!in_array($sModule, $aRemModuleArray[$sClass])) {
00328                             $aAllModuleArray[$sClass][] = $sModule;
00329                         }
00330                     }
00331                     if (!count($aAllModuleArray[$sClass])) {
00332                         unset ($aAllModuleArray[$sClass]);
00333                     }
00334                 } else {
00335                     $aAllModuleArray[$sClass] = $aModuleChain;
00336                 }
00337             }
00338 
00339        }
00340 
00341         return $aAllModuleArray;
00342     }
00343 
00351     public function buildModuleChains($aModuleArray)
00352     {
00353         $aModules = array();
00354         if (is_array($aModuleArray)) {
00355             foreach ($aModuleArray as $sClass => $aModuleChain) {
00356                 $aModules[$sClass] = implode('&', $aModuleChain);
00357             }
00358         }
00359         return $aModules;
00360     }
00361 
00369     protected function _removeFromModulesArray( $aDeletedExt )
00370     {
00371         $aExt = $this->getAllModules();
00372         $aUpdatedExt = $this->diffModuleArrays( $aExt, $aDeletedExt );
00373         $aUpdatedExt = $this->buildModuleChains( $aUpdatedExt );
00374 
00375         $this->getConfig()->saveShopConfVar( 'aarr', 'aModules', $aUpdatedExt );
00376     }
00377 
00385     protected function _removeFromDisabledModulesArray( $aDeletedExtIds )
00386     {
00387         $oConfig = $this->getConfig();
00388         $aDisabledExtensionIds = $this->getDisabledModules();
00389         $aDisabledExtensionIds = array_diff($aDisabledExtensionIds, $aDeletedExtIds);
00390         $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledExtensionIds );
00391     }
00392 
00400     protected function _removeFromLegacyModulesArray( $aDeletedExtIds )
00401     {
00402         $aLegacyExt = $this->getLegacyModules();
00403 
00404         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00405             if ( isset($aLegacyExt[$sDeletedExtId]) ) {
00406                 unset( $aLegacyExt[$sDeletedExtId] );
00407             }
00408         }
00409 
00410         $this->getConfig()->saveShopConfVar( 'aarr', 'aLegacyModules', $aLegacyExt );
00411     }
00412 
00420     protected function _removeFromModulesPathsArray( $aDeletedModule )
00421     {
00422         $aModulePaths = $this->getModulePaths();
00423 
00424         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00425             if ( isset($aModulePaths[$sDeletedModuleId]) ) {
00426                 unset( $aModulePaths[$sDeletedModuleId] );
00427             }
00428         }
00429 
00430         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00431     }
00432 
00440     protected function _removeFromModulesVersionsArray( $aDeletedModule )
00441     {
00442         $aModuleVersions = $this->getModuleVersions();
00443 
00444         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00445             if ( isset($aModuleVersions[$sDeletedModuleId]) ) {
00446                 unset( $aModuleVersions[$sDeletedModuleId] );
00447             }
00448         }
00449 
00450         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleVersions', $aModuleVersions );
00451     }
00452 
00460     protected function _removeFromModulesEventsArray( $aDeletedModule )
00461     {
00462         $aModuleEvents = $this->getModuleEvents();
00463 
00464         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00465             if ( isset($aModuleEvents[$sDeletedModuleId]) ) {
00466                 unset( $aModuleEvents[$sDeletedModuleId] );
00467             }
00468         }
00469 
00470         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleEvents', $aModuleEvents );
00471     }
00472 
00480     protected function _removeFromModulesFilesArray( $aDeletedModule )
00481     {
00482         $aModuleFiles = $this->getModuleFiles();
00483 
00484         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00485             if ( isset($aModuleFiles[$sDeletedModuleId]) ) {
00486                 unset( $aModuleFiles[$sDeletedModuleId] );
00487             }
00488         }
00489 
00490         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleFiles', $aModuleFiles );
00491     }
00492 
00500     protected function _removeFromModulesTemplatesArray( $aDeletedModule )
00501     {
00502         $aModuleTemplates = $this->getModuleTemplates();
00503 
00504         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00505             if ( isset($aModuleTemplates[$sDeletedModuleId]) ) {
00506                 unset( $aModuleTemplates[$sDeletedModuleId] );
00507             }
00508         }
00509 
00510         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleTemplates', $aModuleTemplates );
00511     }
00512 
00520     protected function _removeFromDatabase( $aDeletedExtIds )
00521     {
00522         if ( !is_array($aDeletedExtIds) || !count($aDeletedExtIds) ) {
00523             return;
00524         }
00525 
00526         $oDb = oxDb::getDb();
00527 
00528         $aConfigIds = $sDelExtIds = array();
00529         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00530             $aConfigIds[] = $oDb->quote('module:'.$sDeletedExtId);
00531             $sDelExtIds[] = $oDb->quote($sDeletedExtId);
00532         }
00533 
00534         $sConfigIds = implode( ', ', $aConfigIds );
00535         $sDelExtIds = implode( ', ', $sDelExtIds );
00536 
00537         $aSql[] = "DELETE FROM oxconfig where oxmodule IN ($sConfigIds)";
00538         $aSql[] = "DELETE FROM oxconfigdisplay where oxcfgmodule IN ($sConfigIds)";
00539         $aSql[] = "DELETE FROM oxtplblocks where oxmodule IN ($sDelExtIds)";
00540 
00541         foreach ( $aSql as $sQuery ) {
00542             $oDb->execute( $sQuery );
00543         }
00544     }
00545 
00555     public function getModulesFromDir( $sModulesDir, $sVendorDir = null )
00556     {
00557         $sModulesDir  = oxRegistry::get('oxUtilsFile')->normalizeDir( $sModulesDir );
00558 
00559         foreach ( glob( $sModulesDir.'*' ) as $sModuleDirPath ) {
00560 
00561             $sModuleDirPath .= ( is_dir( $sModuleDirPath ) ) ?'/':'';
00562             $sModuleDirName  = basename( $sModuleDirPath );
00563 
00564             // skipping some file
00565             if ( in_array( $sModuleDirName, $this->_aSkipFiles ) || (!is_dir( $sModuleDirPath ) && substr($sModuleDirName, -4) != ".php")) {
00566                 continue;
00567             }
00568 
00569             if ( $this->_isVendorDir( $sModuleDirPath ) ) {
00570                 // scanning modules vendor directory
00571                 $this->getModulesFromDir( $sModuleDirPath, basename( $sModuleDirPath ) );
00572             } else {
00573                 // loading module info
00574                 $oModule = oxNew( 'oxModule' );
00575                 $sModuleDirName = ( !empty($sVendorDir) ) ? $sVendorDir.'/'.$sModuleDirName : $sModuleDirName;
00576                 $oModule->loadByDir( $sModuleDirName );
00577                 $sModuleId = $oModule->getId();
00578                 $this->_aModules[$sModuleId] = $oModule;
00579 
00580                 $aModulePaths = $this->getModulePaths();
00581 
00582                 if ( !is_array($aModulePaths) || !array_key_exists( $sModuleId, $aModulePaths ) ) {
00583                     // saving module path info
00584                     $this->_saveModulePath( $sModuleId, $sModuleDirName );
00585 
00586                     //checking if this is new module and if it extends any eshop class
00587                     if ( !$this->_extendsClasses( $sModuleDirName ) ) {
00588                         // if not - marking it as disabled by default
00589                         $oModule->deactivate();
00590                     }
00591                 }
00592             }
00593         }
00594         // sorting by name
00595         if ( $this->_aModules !== null ) {
00596             uasort($this->_aModules, array($this, '_sortModules'));
00597         }
00598 
00599         return $this->_aModules;
00600     }
00601 
00610     protected function _sortModules( $oModule1, $oModule2 )
00611     {
00612         return strcasecmp($oModule1->getTitle(), $oModule2->getTitle());
00613     }
00614 
00622     protected function _isVendorDir( $sModuleDir )
00623     {
00624         if ( is_dir( $sModuleDir ) && file_exists( $sModuleDir . 'vendormetadata.php' ) ) {
00625             return true;
00626         }
00627 
00628         return false;
00629     }
00630 
00638     protected function _extendsClasses ( $sModuleDir )
00639     {
00640         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00641         if (is_array($aModules)) {
00642             $sModules = implode( '&', $aModules );
00643 
00644             if ( preg_match("@(^|&+)".$sModuleDir."\b@", $sModules ) ) {
00645                 return true;
00646             }
00647         }
00648 
00649         return false;
00650     }
00651 
00660     protected function _saveModulePath( $sModuleId, $sModulePath )
00661     {
00662         $aModulePaths = $this->getModulePaths();
00663 
00664         $aModulePaths[$sModuleId] = $sModulePath;
00665         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00666     }
00667 
00668 }