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             self::$_instance = modInstances::getMod( __CLASS__ );
00046         }
00047 
00048         if ( !self::$_instance instanceof oxUtilsObject ) {
00049 
00050             // allow modules
00051             $oUtilsObject = new oxUtilsObject();
00052             self::$_instance = $oUtilsObject->oxNew( 'oxUtilsObject' );
00053 
00054             if ( defined( 'OXID_PHP_UNIT' ) ) {
00055                 modInstances::addMod( __CLASS__, self::$_instance);
00056             }
00057         }
00058         return self::$_instance;
00059     }
00060 
00071     public function oxNew( $sClassName )
00072     {
00073         $aArgs = func_get_args();
00074         array_shift( $aArgs );
00075         $iArgCnt = count( $aArgs );
00076         $blCacheObj = $iArgCnt < 2;
00077         $sClassName = strtolower( $sClassName );
00078 
00079         if ( !defined( 'OXID_PHP_UNIT' ) && $blCacheObj ) {
00080             $sCacheKey  = ( $iArgCnt )?$sClassName.md5( serialize( $aArgs ) ):$sClassName;
00081             if ( isset( self::$_aInstanceCache[$sCacheKey] ) ) {
00082                 return clone self::$_aInstanceCache[$sCacheKey];
00083             }
00084         }
00085 
00086         // performance
00087         if ( isset( $this->_aClassNameCache[$sClassName] ) ) {
00088             $sActionClassName = $this->_aClassNameCache[$sClassName];
00089         } else {
00090             $sActionClassName = $this->getClassName( $sClassName );
00091             //expect __autoload() (oxfunctions.php) to do its job when class_exists() is called
00092             if ( !class_exists( $sActionClassName ) ) {
00093                 $oEx = oxNew( "oxSystemComponentException" );
00094                 $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00095                 $oEx->setComponent($sClassName);
00096                 $oEx->debugOut();
00097                 throw $oEx;
00098             }
00099             // performance
00100             $this->_aClassNameCache[$sClassName] = $sActionClassName;
00101         }
00102 
00103         $oActionObject = $this->_getObject( $sActionClassName, $iArgCnt, $aArgs );
00104         if ( $blCacheObj && $oActionObject instanceof oxBase ) {
00105             self::$_aInstanceCache[$sCacheKey] = clone $oActionObject;
00106         }
00107 
00108         return $oActionObject;
00109     }
00110 
00123     protected function _getObject( $sClassName, $iArgCnt, $aParams )
00124     {
00125         // dynamic creation (if parameter count < 4) gives more performance for regular objects
00126         switch( $iArgCnt ) {
00127             case 0:
00128                 $oObj = new $sClassName();
00129                 break;
00130             case 1:
00131                 $oObj = new $sClassName( $aParams[0] );
00132                 break;
00133             case 2:
00134                 $oObj = new $sClassName( $aParams[0], $aParams[1] );
00135                 break;
00136             case 3:
00137                 $oObj = new $sClassName( $aParams[0], $aParams[1], $aParams[2] );
00138                 break;
00139             default:
00140                 try {
00141                     // unlimited constructor arguments support
00142                     $oRo = new ReflectionClass( $sClassName );
00143                     $oObj = $oRo->newInstanceArgs( $aParams );
00144                 } catch ( ReflectionException $oRefExcp ) {
00145                     // something went wrong?
00146                     $oEx = oxNew( "oxSystemComponentException" );
00147                     $oEx->setMessage( $oRefExcp->getMessage() );
00148                     $oEx->setComponent( $sClassName );
00149                     $oEx->debugOut();
00150                     throw $oEx;
00151                 }
00152         }
00153 
00154         return $oObj;
00155     }
00156 
00165     public function oxNewArticle( $sOxID, $aProperties = array())
00166     {
00167         if ( $sOxID && isset( self::$_aLoadedArticles[$sOxID] ) ) {
00168             return self::$_aLoadedArticles[$sOxID];
00169         }
00170 
00171         $oActionObject = $this->oxNew( 'oxarticle' );
00172 
00173         // adding object prioperties
00174         foreach ( $aProperties as $sPropertyName => $sPropertyVal ) {
00175             $oActionObject->$sPropertyName = $sPropertyVal;
00176         }
00177 
00178         $oActionObject->load( $sOxID );
00179 
00180         self::$_aLoadedArticles[$sOxID] = $oActionObject;
00181         return $oActionObject;
00182     }
00183 
00191     public function resetInstanceCache($sClassName = null)
00192     {
00193         if ($sClassName && isset(self::$_aInstanceCache[$sClassName])) {
00194             unset(self::$_aInstanceCache[$sClassName]);
00195             return;
00196         }
00197 
00198         //looping due to possible memory "leak".
00199         if (is_array(self::$_aInstanceCache)) {
00200             foreach (self::$_aInstanceCache as $sKey => $oInstance) {
00201                 unset(self::$_aInstanceCache[$sKey]);
00202             }
00203         }
00204 
00205         self::$_aInstanceCache = array();
00206     }
00207 
00213     public function generateUId()
00214     {
00215         return substr( $this->getSession()->getId(), 0, 3 ) . substr( md5( uniqid( '', true ).'|'.microtime() ), 0, 29 );
00216     }
00217 
00218 
00226     public function getClassName( $sClassName )
00227     {
00228 
00229         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00230 
00231         if ( is_array( $aModules ) ) {
00232 
00233             $aModules = array_change_key_case( $aModules );
00234 
00235             if ( array_key_exists( $sClassName, $aModules ) ) {
00236                 //multiple inheritance implementation
00237                 //in case we have multiple modules:
00238                 //like oxoutput => sub/suboutput1&sub/suboutput2&sub/suboutput3
00239                 $aClassChain = explode( "&", $aModules[$sClassName] );
00240 
00241                 $sParent = $sClassName;
00242 
00243                 //security: just preventing string termination
00244                 $sParent = str_replace(chr(0), '', $sParent);
00245 
00246                 //building middle classes if needed
00247                 $sClassName = $this->_makeSafeModuleClassParents( $aClassChain, $sParent );
00248             }
00249         }
00250 
00251         // check if there is a path, if yes, remove it
00252         $sClassName = basename( $sClassName );
00253 
00254         return $sClassName;
00255     }
00256 
00265     public function isModuleActive( $sClassName, $sModuleName )
00266     {
00267         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00268         if ( is_array( $aModules ) && array_key_exists( $sClassName, $aModules ) ) {
00269             $aClassChain = explode( "&", $aModules[$sClassName] );
00270             foreach ($aClassChain as $sModule) {
00271                 if ( basename($sModule) == $sModuleName ) {
00272                     return true;
00273                 }
00274             }
00275         }
00276         return false;
00277     }
00278 
00289     private function _makeSafeModuleClassParents( $aClassChain, $sBaseModule )
00290     {
00291         $myConfig = $this->getConfig();
00292         $sParent = $sBaseModule;
00293 
00294         //building middle classes if needed
00295         foreach ($aClassChain as $sModule) {
00296             //creating middle classes
00297             //e.g. class suboutput1_parent extends oxoutput {}
00298             //     class suboutput2_parent extends suboutput1 {}
00299             //$sModuleClass = $this->getClassName($sModule);
00300 
00301             //security: just preventing string termination
00302             $sModule = str_replace(chr(0), '', $sModule);
00303 
00304             //get parent and module class names from sub/suboutput2
00305             $sModuleClass = basename($sModule);
00306 
00307             if ( !class_exists( $sModuleClass, false ) ) {
00308                 $sParentClass = basename($sParent);
00309                 //P
00310                 //$sInitClass = "class ".$sModuleClass."_parent extends $sParentClass { function ".$sModuleClass."_parent(){ return ".$sParentClass."::".$sParentClass."();} }";
00311                 $sInitClass = "class ".$sModuleClass."_parent extends $sParentClass {}";
00312 
00313                 //initializing middle class
00314                 if (!class_exists($sModuleClass."_parent", false)) {
00315                     eval($sInitClass);
00316                 }
00317                 $sParentPath = $myConfig->getConfigParam( 'sShopDir' )."/modules/".$sModule.".php";
00318 
00319                 //including original file
00320                 if ( file_exists( $sParentPath ) ) {
00321                     include_once $sParentPath;
00322                 } elseif ( !class_exists( $sModuleClass ) ) {
00323                     //to avoid problems with unitest and only throw a exception if class does not exists MAFI
00324                     $oEx = oxNew( "oxSystemComponentException" );
00325                     $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00326                     $oEx->setComponent($sModule);
00327                 }
00328             }
00329 
00330             $sParent = $sModule;
00331         }
00332 
00333         //returning the last module from the chain
00334         $sClassName = $aClassChain[count($aClassChain) - 1];
00335         return $sClassName;
00336     }
00337 }