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 
00218     private function _makeSafeModuleClassParents( $aClassChain, $sBaseModule )
00219     {
00220         $myConfig = $this->getConfig();
00221         $sParent = $sBaseModule;
00222 
00223         //building middle classes if needed
00224         foreach ($aClassChain as $sModule) {
00225             //creating middle classes
00226             //e.g. class suboutput1_parent extends oxoutput {}
00227             //     class suboutput2_parent extends suboutput1 {}
00228             //$sModuleClass = $this->getClassName($sModule);
00229 
00230             //security: just preventing string termination
00231             $sModule = str_replace(chr(0), '', $sModule);
00232 
00233             //get parent and module class names from sub/suboutput2
00234             $sParentClass = basename($sParent);
00235             $sModuleClass = basename($sModule);
00236 
00237             //P
00238             //$sInitClass = "class ".$sModuleClass."_parent extends $sParentClass { function ".$sModuleClass."_parent(){ return ".$sParentClass."::".$sParentClass."();} }";
00239             $sInitClass = "class ".$sModuleClass."_parent extends $sParentClass {}";
00240 
00241             //initializing middle class
00242             if (!class_exists($sModuleClass."_parent", false)) {
00243                 eval($sInitClass);
00244             }
00245             $sParentPath = $myConfig->getConfigParam( 'sShopDir' )."/modules/".$sModule.".php";
00246 
00247             //including original file
00248             if ( file_exists( $sParentPath ) ) {
00249                 require_once $sParentPath;
00250             } elseif ( !class_exists( $sModuleClass ) ) { //to avoid problems with unitest and only throw a exception if class does not exists MAFI
00251                 $oEx = new oxSystemComponentException();
00252                 $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00253                 $oEx->setComponent($sModule);
00254             }
00255 
00256             $sParent = $sModule;
00257         }
00258 
00259         //returning the last module from the chain
00260         $sClassName = $aClassChain[count($aClassChain) - 1];
00261         return $sClassName;
00262     }
00263 }

Generated on Thu Dec 4 12:04:57 2008 for OXID eShop CE by  doxygen 1.5.5