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 
00024 
00030     public function getAllModules()
00031     {
00032         return $this->getConfig()->getAllModules();
00033     }
00034 
00040     public function getActiveModuleInfo()
00041     {
00042         $aModulePaths   = $this->getModulePaths();
00043 
00044         // Extract module paths from extended classes
00045         if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
00046             $aModulePaths = $this->extractModulePaths();
00047         }
00048 
00049         $aDisabledModules = $this->getDisabledModules();
00050         if ( is_array($aDisabledModules) && count($aDisabledModules) > 0  && count($aModulePaths) > 0 ) {
00051             $aModulePaths = array_diff_key($aModulePaths, array_flip($aDisabledModules));
00052         }
00053         return $aModulePaths;
00054     }
00055 
00061     public function getDisabledModuleInfo()
00062     {
00063         $aDisabledModules = $this->getDisabledModules();
00064         $aModulePaths     = array();
00065 
00066         if ( is_array($aDisabledModules) && count($aDisabledModules) > 0 ) {
00067             $aModulePaths   = $this->getModulePaths();
00068 
00069             // Extract module paths from extended classes
00070             if ( !is_array($aModulePaths) || count($aModulePaths) < 1 ) {
00071                 $aModulePaths = $this->extractModulePaths();
00072             }
00073 
00074             if ( is_array($aModulePaths) || count($aModulePaths) > 0 ) {
00075                 $aModulePaths = array_intersect_key($aModulePaths, array_flip($aDisabledModules));
00076             }
00077         }
00078 
00079         return $aModulePaths;
00080     }
00081 
00087     public function getModuleVersions()
00088     {
00089         return $this->getConfig()->getConfigParam('aModuleVersions');
00090     }
00091 
00097     public function getLegacyModules()
00098     {
00099         return $this->getConfig()->getConfigParam('aLegacyModules');
00100     }
00101 
00107     public function getDisabledModules()
00108     {
00109         return $this->getConfig()->getConfigParam('aDisabledModules');
00110     }
00111 
00117     public function getModulePaths()
00118     {
00119         return $this->getConfig()->getConfigParam('aModulePaths');
00120     }
00121 
00127     public function getModuleEvents()
00128     {
00129         return (array) $this->getConfig()->getConfigParam('aModuleEvents');
00130     }
00131 
00137     public function extractModulePaths()
00138     {
00139         $aModules     = $this->getAllModules();
00140         $aModulePaths = array();
00141 
00142         if (is_array($aModules) && count($aModules) > 0) {
00143             foreach ($aModules as $aModuleClasses) {
00144                 foreach ($aModuleClasses as $sModule) {
00145                     $sModuleId = substr($sModule, 0, strpos($sModule, "/"));
00146                     $aModulePaths[$sModuleId] = $sModuleId;
00147                 }
00148             }
00149         }
00150         return $aModulePaths;
00151     }
00152 
00158     public function getModuleFiles()
00159     {
00160         return $this->getConfig()->getConfigParam('aModuleFiles');
00161     }
00162 
00168     public function getModuleTemplates()
00169     {
00170         return $this->getConfig()->getConfigParam('aModuleTemplates');
00171     }
00172 
00181     public function getDisabledModuleClasses()
00182     {
00183         $aDisabledModules = $this->getDisabledModules();
00184         $aModules         = $this->getAllModules();
00185         $aModulePaths     = $this->getModulePaths();
00186 
00187         $aDisabledModuleClasses = array();
00188         if (isset($aDisabledModules) && is_array($aDisabledModules)) {
00189             //get all disabled module paths
00190             foreach ($aDisabledModules as $sId) {
00191                 $sPath = $aModulePaths[$sId];
00192                 if (!isset($sPath)) {
00193                     $sPath = $sId;
00194                 }
00195                 foreach ( $aModules as $sClass => $aModuleClasses ) {
00196                     foreach ( $aModuleClasses as $sModuleClass ) {
00197                         if (strpos($sModuleClass, $sPath."/") === 0 ) {
00198                             $aDisabledModuleClasses[] = $sModuleClass;
00199                         }
00200                     }
00201                 }
00202             }
00203         }
00204 
00205         return $aDisabledModuleClasses;
00206     }
00207 
00213     public function cleanup()
00214     {
00215         $aDeletedExt = $this->getDeletedExtensions();
00216 
00217         //collecting deleted extension IDs
00218         $aDeletedExtIds = $this->getDeletedExtensionIds($aDeletedExt);
00219 
00220         // removing from aModules config array
00221         $this->_removeFromModulesArray( $aDeletedExt );
00222 
00223         // removing from aDisabledModules array
00224         $this->_removeFromDisabledModulesArray( $aDeletedExtIds );
00225 
00226         // removing from aLegacyModules array
00227         $this->_removeFromLegacyModulesArray( $aDeletedExtIds );
00228 
00229         // removing from aModulePaths array
00230         $this->_removeFromModulesPathsArray( $aDeletedExtIds );
00231 
00232         // removing from aModuleEvents array
00233         $this->_removeFromModulesEventsArray( $aDeletedExtIds );
00234 
00235         // removing from aModuleVersions array
00236         $this->_removeFromModulesVersionsArray( $aDeletedExtIds );
00237 
00238         // removing from aModuleFiles array
00239         $this->_removeFromModulesFilesArray( $aDeletedExtIds );
00240 
00241         // removing from aModuleTemplates array
00242         $this->_removeFromModulesTemplatesArray( $aDeletedExtIds );
00243 
00244         //removing from config tables and templates blocks table
00245         $this->_removeFromDatabase( $aDeletedExtIds );
00246     }
00247 
00255     public function getDeletedExtensionIds($aDeletedExt)
00256     {
00257         $aDeletedExtIds = array();
00258         if ( !empty($aDeletedExt) ) {
00259             $oModule = oxNew('oxModule');
00260             foreach ( $aDeletedExt as $sOxClass => $aDeletedModules ) {
00261                 foreach ( $aDeletedModules as $sModulePath ) {
00262                     $aDeletedExtIds[] = $oModule->getIdByPath($sModulePath);
00263                 }
00264             }
00265         }
00266 
00267         if ( !empty( $aDeletedExtIds ) ) {
00268             $aDeletedExtIds = array_unique( $aDeletedExtIds );
00269         }
00270 
00271         return $aDeletedExtIds;
00272     }
00273 
00279     public function getDeletedExtensions()
00280     {
00281         $aModules = $this->getAllModules();
00282         $aDeletedExt = array();
00283 
00284         foreach ( $aModules as $sOxClass => $aModulesList ) {
00285             foreach ( $aModulesList as $sModulePath ) {
00286                 $sExtPath = $this->getConfig()->getModulesDir() . $sModulePath.'.php';
00287                 if ( !file_exists( $sExtPath ) ) {
00288                     $aDeletedExt[$sOxClass][] = $sModulePath;
00289                 }
00290             }
00291         }
00292 
00293         return $aDeletedExt;
00294     }
00295 
00305     public function diffModuleArrays($aAllModuleArray, $aRemModuleArray)
00306     {
00307        if (is_array($aAllModuleArray) && is_array($aRemModuleArray)) {
00308             foreach ($aAllModuleArray as $sClass => $aModuleChain) {
00309                 if (!is_array($aModuleChain)) {
00310                     $aModuleChain = array($aModuleChain);
00311                 }
00312                 if (isset($aRemModuleArray[$sClass])) {
00313                     if (!is_array($aRemModuleArray[$sClass])) {
00314                         $aRemModuleArray[$sClass] = array($aRemModuleArray[$sClass]);
00315                     }
00316                     $aAllModuleArray[$sClass] = array();
00317                     foreach ($aModuleChain as $sModule) {
00318                         if (!in_array($sModule, $aRemModuleArray[$sClass])) {
00319                             $aAllModuleArray[$sClass][] = $sModule;
00320                         }
00321                     }
00322                     if (!count($aAllModuleArray[$sClass])) {
00323                         unset ($aAllModuleArray[$sClass]);
00324                     }
00325                 } else {
00326                     $aAllModuleArray[$sClass] = $aModuleChain;
00327                 }
00328             }
00329 
00330        }
00331 
00332         return $aAllModuleArray;
00333     }
00334 
00342     public function buildModuleChains($aModuleArray)
00343     {
00344         $aModules = array();
00345         if (is_array($aModuleArray)) {
00346             foreach ($aModuleArray as $sClass => $aModuleChain) {
00347                 $aModules[$sClass] = implode('&', $aModuleChain);
00348             }
00349         }
00350         return $aModules;
00351     }
00352 
00360     protected function _removeFromModulesArray( $aDeletedExt )
00361     {
00362         $aExt = $this->getAllModules();
00363         $aUpdatedExt = $this->diffModuleArrays( $aExt, $aDeletedExt );
00364         $aUpdatedExt = $this->buildModuleChains( $aUpdatedExt );
00365 
00366         $this->getConfig()->saveShopConfVar( 'aarr', 'aModules', $aUpdatedExt );
00367     }
00368 
00376     protected function _removeFromDisabledModulesArray( $aDeletedExtIds )
00377     {
00378         $oConfig = $this->getConfig();
00379         $aDisabledExtensionIds = $this->getDisabledModules();
00380         $aDisabledExtensionIds = array_diff($aDisabledExtensionIds, $aDeletedExtIds);
00381         $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledExtensionIds );
00382     }
00383 
00391     protected function _removeFromLegacyModulesArray( $aDeletedExtIds )
00392     {
00393         $aLegacyExt = $this->getLegacyModules();
00394 
00395         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00396             if ( isset($aLegacyExt[$sDeletedExtId]) ) {
00397                 unset( $aLegacyExt[$sDeletedExtId] );
00398             }
00399         }
00400 
00401         $this->getConfig()->saveShopConfVar( 'aarr', 'aLegacyModules', $aLegacyExt );
00402     }
00403 
00411     protected function _removeFromModulesPathsArray( $aDeletedModule )
00412     {
00413         $aModulePaths = $this->getModulePaths();
00414 
00415         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00416             if ( isset($aModulePaths[$sDeletedModuleId]) ) {
00417                 unset( $aModulePaths[$sDeletedModuleId] );
00418             }
00419         }
00420 
00421         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00422     }
00423 
00431     protected function _removeFromModulesVersionsArray( $aDeletedModule )
00432     {
00433         $aModuleVersions = $this->getModuleVersions();
00434 
00435         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00436             if ( isset($aModuleVersions[$sDeletedModuleId]) ) {
00437                 unset( $aModuleVersions[$sDeletedModuleId] );
00438             }
00439         }
00440 
00441         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleVersions', $aModuleVersions );
00442     }
00443 
00451     protected function _removeFromModulesEventsArray( $aDeletedModule )
00452     {
00453         $aModuleEvents = $this->getModuleEvents();
00454 
00455         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00456             if ( isset($aModuleEvents[$sDeletedModuleId]) ) {
00457                 unset( $aModuleEvents[$sDeletedModuleId] );
00458             }
00459         }
00460 
00461         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleEvents', $aModuleEvents );
00462     }
00463 
00471     protected function _removeFromModulesFilesArray( $aDeletedModule )
00472     {
00473         $aModuleFiles = $this->getModuleFiles();
00474 
00475         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00476             if ( isset($aModuleFiles[$sDeletedModuleId]) ) {
00477                 unset( $aModuleFiles[$sDeletedModuleId] );
00478             }
00479         }
00480 
00481         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleFiles', $aModuleFiles );
00482     }
00483 
00491     protected function _removeFromModulesTemplatesArray( $aDeletedModule )
00492     {
00493         $aModuleTemplates = $this->getModuleTemplates();
00494 
00495         foreach ( $aDeletedModule as $sDeletedModuleId ) {
00496             if ( isset($aModuleTemplates[$sDeletedModuleId]) ) {
00497                 unset( $aModuleTemplates[$sDeletedModuleId] );
00498             }
00499         }
00500 
00501         $this->getConfig()->saveShopConfVar( 'aarr', 'aModuleTemplates', $aModuleTemplates );
00502     }
00503 
00511     protected function _removeFromDatabase( $aDeletedExtIds )
00512     {
00513         if ( !is_array($aDeletedExtIds) || !count($aDeletedExtIds) ) {
00514             return;
00515         }
00516 
00517         $oDb = oxDb::getDb();
00518 
00519         $aConfigIds = $sDelExtIds = array();
00520         foreach ( $aDeletedExtIds as $sDeletedExtId ) {
00521             $aConfigIds[] = $oDb->quote('module:'.$sDeletedExtId);
00522             $sDelExtIds[] = $oDb->quote($sDeletedExtId);
00523         }
00524 
00525         $sConfigIds = implode( ', ', $aConfigIds );
00526         $sDelExtIds = implode( ', ', $sDelExtIds );
00527 
00528         $aSql[] = "DELETE FROM oxconfig where oxmodule IN ($sConfigIds)";
00529         $aSql[] = "DELETE FROM oxconfigdisplay where oxcfgmodule IN ($sConfigIds)";
00530         $aSql[] = "DELETE FROM oxtplblocks where oxmodule IN ($sDelExtIds)";
00531 
00532         foreach ( $aSql as $sQuery ) {
00533             $oDb->execute( $sQuery );
00534         }
00535     }
00536 
00546     public function getModulesFromDir( $sModulesDir, $sVendorDir = null )
00547     {
00548         $sModulesDir  = oxRegistry::get('oxUtilsFile')->normalizeDir( $sModulesDir );
00549 
00550         foreach ( glob( $sModulesDir.'*' ) as $sModuleDirPath ) {
00551 
00552             $sModuleDirPath .= ( is_dir( $sModuleDirPath ) ) ?'/':'';
00553             $sModuleDirName  = basename( $sModuleDirPath );
00554 
00555             // skipping some file
00556             if ( in_array( $sModuleDirName, $this->_aSkipFiles ) || (!is_dir( $sModuleDirPath ) && substr($sModuleDirName, -4) != ".php")) {
00557                 continue;
00558             }
00559 
00560             if ( $this->_isVendorDir( $sModuleDirPath ) ) {
00561                 // scanning modules vendor directory
00562                 $this->getModulesFromDir( $sModuleDirPath, basename( $sModuleDirPath ) );
00563             } else {
00564                 // loading module info
00565                 $oModule = oxNew( 'oxModule' );
00566                 $sModuleDirName = ( !empty($sVendorDir) ) ? $sVendorDir.'/'.$sModuleDirName : $sModuleDirName;
00567                 $oModule->loadByDir( $sModuleDirName );
00568                 $sModuleId = $oModule->getId();
00569                 $this->_aModules[$sModuleId] = $oModule;
00570 
00571                 $aModulePaths = $this->getModulePaths();
00572 
00573                 if ( !is_array($aModulePaths) || !array_key_exists( $sModuleId, $aModulePaths ) ) {
00574                     // saving module path info
00575                     $this->_saveModulePath( $sModuleId, $sModuleDirName );
00576 
00577                     //checking if this is new module and if it extends any eshop class
00578                     if ( !$this->_extendsClasses( $sModuleDirName ) ) {
00579                         // if not - marking it as disabled by default
00580                         $oModule->deactivate();
00581                     }
00582                 }
00583             }
00584         }
00585         // sorting by name
00586         if ( $this->_aModules !== null ) {
00587             uasort($this->_aModules, array($this, '_sortModules'));
00588         }
00589 
00590         return $this->_aModules;
00591     }
00592 
00601     protected function _sortModules( $oModule1, $oModule2 )
00602     {
00603         return strcasecmp($oModule1->getTitle(), $oModule2->getTitle());
00604     }
00605 
00613     protected function _isVendorDir( $sModuleDir )
00614     {
00615         if ( is_dir( $sModuleDir ) && file_exists( $sModuleDir . 'vendormetadata.php' ) ) {
00616             return true;
00617         }
00618 
00619         return false;
00620     }
00621 
00629     protected function _extendsClasses ( $sModuleDir )
00630     {
00631         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00632         if (is_array($aModules)) {
00633             $sModules = implode( '&', $aModules );
00634 
00635             if ( preg_match("@(^|&+)".$sModuleDir."\b@", $sModules ) ) {
00636                 return true;
00637             }
00638         }
00639 
00640         return false;
00641     }
00642 
00651     protected function _saveModulePath( $sModuleId, $sModulePath )
00652     {
00653         $aModulePaths = $this->getModulePaths();
00654 
00655         $aModulePaths[$sModuleId] = $sModulePath;
00656         $this->getConfig()->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00657     }
00658 
00659 }