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