oxutilsobject.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsObject extends oxSuperCfg
00007 {
00013     protected $_aClassNameCache = array();
00014 
00020     protected static $_aLoadedArticles = array();
00021 
00027     protected static $_aInstanceCache = array();
00028 
00034     private static $_instance = null;
00035 
00041     public static function getInstance()
00042     {
00043         // disable caching for test modules
00044         if ( defined( 'OXID_PHP_UNIT' ) ) {
00045             static $inst = array();
00046             self::$_instance = $inst[oxClassCacheKey()];
00047         }
00048 
00049         if ( !self::$_instance instanceof oxUtilsObject ) {
00050 
00051             // allow modules
00052             $oUtilsObject = new oxUtilsObject();
00053             self::$_instance = $oUtilsObject->oxNew( 'oxUtilsObject' );
00054 
00055             if ( defined( 'OXID_PHP_UNIT' ) ) {
00056                 $inst[oxClassCacheKey()] = self::$_instance;
00057             }
00058         }
00059         return self::$_instance;
00060     }
00061 
00073     public function oxNew( $sClassName, $sParams = null )
00074     {
00075 
00076         $sClassName = strtolower( $sClassName );
00077         $sCacheKey  = ($sParams !== null )?$sClassName.md5( serialize( $sParams ) ):$sClassName;
00078 
00079         if ( !defined( 'OXID_PHP_UNIT' ) ) {
00080             if ( isset( self::$_aInstanceCache[$sCacheKey] ) ) {
00081                 return clone self::$_aInstanceCache[$sCacheKey];
00082             }
00083         }
00084 
00085         // performance
00086         if ( isset( $this->_aClassNameCache[$sClassName] ) ) {
00087             $sActionClassName = $this->_aClassNameCache[$sClassName];
00088         } else {
00089             $sActionClassName = $this->getClassName( $sClassName );
00090             //expect __autoload() (oxfunctions.php) to do its job when class_exists() is called
00091             if ( !class_exists( $sActionClassName ) ) {
00092                 $oEx = new oxSystemComponentException();
00093                 $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00094                 $oEx->setComponent($sClassName);
00095                 $oEx->debugOut();
00096                 throw $oEx;
00097             }
00098             // performance
00099             $this->_aClassNameCache[$sClassName] = $sActionClassName;
00100         }
00101 
00102         if ( $sParams ) {
00103             $oActionObject = new $sActionClassName( $sParams );
00104         } else {
00105             $oActionObject = new $sActionClassName();
00106         }
00107 
00108         if ( $oActionObject instanceof oxBase ) {
00109             self::$_aInstanceCache[$sCacheKey] = clone $oActionObject;
00110         }
00111 
00112         return $oActionObject;
00113     }
00114 
00123     public function oxNewArticle( $sOxID, $aProperties = array())
00124     {
00125         if ( $sOxID && isset( self::$_aLoadedArticles[$sOxID] ) ) {
00126             return self::$_aLoadedArticles[$sOxID];
00127         }
00128 
00129         $oActionObject = $this->oxNew( 'oxarticle' );
00130 
00131         // adding object prioperties
00132         foreach ( $aProperties as $sPropertyName => $sPropertyVal ) {
00133             $oActionObject->$sPropertyName = $sPropertyVal;
00134         }
00135 
00136         $oActionObject->load( $sOxID );
00137 
00138         self::$_aLoadedArticles[$sOxID] = $oActionObject;
00139         return $oActionObject;
00140     }
00141 
00149     public function resetInstanceCache($sClassName = null)
00150     {
00151         if ($sClassName && isset(self::$_aInstanceCache[$sClassName])) {
00152             unset(self::$_aInstanceCache[$sClassName]);
00153             return;
00154         }
00155 
00156         //looping due to possible memory "leak".
00157         if (is_array(self::$_aInstanceCache)) {
00158             foreach (self::$_aInstanceCache as $sKey => $oInstance) {
00159                 unset(self::$_aInstanceCache[$sKey]);
00160             }
00161         }
00162 
00163         self::$_aInstanceCache = array();
00164     }
00165 
00171     public function generateUId()
00172     {
00173         return substr( $this->getSession()->getId(), 0, 3 ) . substr( md5( uniqid( '', true ).'|'.microtime() ), 0, 29 );
00174     }
00175 
00176 
00184     public function getClassName( $sClassName )
00185     {
00186         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00187         if ( is_array( $aModules ) && array_key_exists( $sClassName, $aModules ) ) {
00188             //multiple inheritance implementation
00189             //in case we have multiple modules:
00190             //like oxoutput => sub/suboutput1&sub/suboutput2&sub/suboutput3
00191             $aClassChain = explode( "&", $aModules[$sClassName] );
00192 
00193             $sParent = $sClassName;
00194 
00195             //security: just preventing string termination
00196             $sParent = str_replace(chr(0), '', $sParent);
00197 
00198             //building middle classes if needed
00199             $sClassName = $this->_makeSafeModuleClassParents( $aClassChain, $sParent );
00200         }
00201 
00202         // check if there is a path, if yes, remove it
00203         $sClassName = basename( $sClassName );
00204 
00205         return $sClassName;
00206     }
00207 
00216     public function isModuleActive( $sClassName, $sModuleName )
00217     {
00218         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00219         if ( is_array( $aModules ) && array_key_exists( $sClassName, $aModules ) ) {
00220             $aClassChain = explode( "&", $aModules[$sClassName] );
00221             foreach ($aClassChain as $sModule) {
00222                 if ( basename($sModule) == $sModuleName ) {
00223                     return true;
00224                 }
00225             }
00226         }
00227         return false;
00228     }
00229 
00240     private function _makeSafeModuleClassParents( $aClassChain, $sBaseModule )
00241     {
00242         $myConfig = $this->getConfig();
00243         $sParent = $sBaseModule;
00244 
00245         //building middle classes if needed
00246         foreach ($aClassChain as $sModule) {
00247             //creating middle classes
00248             //e.g. class suboutput1_parent extends oxoutput {}
00249             //     class suboutput2_parent extends suboutput1 {}
00250             //$sModuleClass = $this->getClassName($sModule);
00251 
00252             //security: just preventing string termination
00253             $sModule = str_replace(chr(0), '', $sModule);
00254 
00255             //get parent and module class names from sub/suboutput2
00256             $sParentClass = basename($sParent);
00257             $sModuleClass = basename($sModule);
00258 
00259             //P
00260             //$sInitClass = "class ".$sModuleClass."_parent extends $sParentClass { function ".$sModuleClass."_parent(){ return ".$sParentClass."::".$sParentClass."();} }";
00261             $sInitClass = "class ".$sModuleClass."_parent extends $sParentClass {}";
00262 
00263             //initializing middle class
00264             if (!class_exists($sModuleClass."_parent", false)) {
00265                 eval($sInitClass);
00266             }
00267             $sParentPath = $myConfig->getConfigParam( 'sShopDir' )."/modules/".$sModule.".php";
00268 
00269             //including original file
00270             if ( file_exists( $sParentPath ) ) {
00271                 require_once $sParentPath;
00272             } elseif ( !class_exists( $sModuleClass ) ) { //to avoid problems with unitest and only throw a exception if class does not exists MAFI
00273                 $oEx = new oxSystemComponentException();
00274                 $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00275                 $oEx->setComponent($sModule);
00276             }
00277 
00278             $sParent = $sModule;
00279         }
00280 
00281         //returning the last module from the chain
00282         $sClassName = $aClassChain[count($aClassChain) - 1];
00283         return $sClassName;
00284     }
00285 }

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5