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