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 
00072     public function oxNew( $sClassName, $sParams = null )
00073     {
00074 
00075         $sClassName = strtolower( $sClassName );
00076         $sCacheKey  = ($sParams !== null )?$sClassName.md5( serialize( $sParams ) ):$sClassName;
00077 
00078         if ( !defined( 'OXID_PHP_UNIT' ) ) {
00079             if ( isset( self::$_aInstanceCache[$sCacheKey] ) ) {
00080                 return clone self::$_aInstanceCache[$sCacheKey];
00081             }
00082         }
00083 
00084         // performance
00085         if ( isset( $this->_aClassNameCache[$sClassName] ) ) {
00086             $sActionClassName = $this->_aClassNameCache[$sClassName];
00087         } else {
00088             $sActionClassName = $this->getClassName( $sClassName );
00089             //expect __autoload() (oxfunctions.php) to do its job when class_exists() is called
00090             if ( !class_exists( $sActionClassName ) ) {
00091                 $oEx = new oxSystemComponentException();
00092                 $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00093                 $oEx->setComponent($sClassName);
00094                 $oEx->debugOut();
00095                 throw $oEx;
00096             }
00097             // performance
00098             $this->_aClassNameCache[$sClassName] = $sActionClassName;
00099         }
00100 
00101         if ( $sParams ) {
00102             $oActionObject = new $sActionClassName( $sParams );
00103         } else {
00104             $oActionObject = new $sActionClassName();
00105         }
00106 
00107         if ( $oActionObject instanceof oxBase ) {
00108             self::$_aInstanceCache[$sCacheKey] = clone $oActionObject;
00109         }
00110 
00111         return $oActionObject;
00112     }
00113 
00122     public function oxNewArticle( $sOxID, $aProperties = array())
00123     {
00124         if ( $sOxID && isset( self::$_aLoadedArticles[$sOxID] ) ) {
00125             return self::$_aLoadedArticles[$sOxID];
00126         }
00127 
00128         $oActionObject = $this->oxNew( 'oxarticle' );
00129 
00130         // adding object prioperties
00131         foreach ( $aProperties as $sPropertyName => $sPropertyVal ) {
00132             $oActionObject->$sPropertyName = $sPropertyVal;
00133         }
00134 
00135         $oActionObject->load( $sOxID );
00136 
00137         self::$_aLoadedArticles[$sOxID] = $oActionObject;
00138         return $oActionObject;
00139     }
00140 
00148     public function resetInstanceCache($sClassName = null)
00149     {
00150         if ($sClassName && isset(self::$_aInstanceCache[$sClassName])) {
00151             unset(self::$_aInstanceCache[$sClassName]);
00152             return;
00153         }
00154 
00155         //looping due to possible memory "leak".
00156         if (is_array(self::$_aInstanceCache)) {
00157             foreach (self::$_aInstanceCache as $sKey => $oInstance) {
00158                 unset(self::$_aInstanceCache[$sKey]);
00159             }
00160         }
00161 
00162         self::$_aInstanceCache = array();
00163     }
00164 
00170     public function generateUId()
00171     {
00172         return substr( $this->getSession()->getId(), 0, 3 ) . substr( md5( uniqid( '', true ).'|'.microtime() ), 0, 29 );
00173     }
00174 
00175 
00183     public function getClassName( $sClassName )
00184     {
00185         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00186         if ( is_array( $aModules ) && array_key_exists( $sClassName, $aModules ) ) {
00187             //multiple inheritance implementation
00188             //in case we have multiple modules:
00189             //like oxoutput => sub/suboutput1&sub/suboutput2&sub/suboutput3
00190             $aClassChain = explode( "&", $aModules[$sClassName] );
00191 
00192             $sParent = $sClassName;
00193 
00194             //security: just preventing string termination
00195             $sParent = str_replace(chr(0), '', $sParent);
00196 
00197             //building middle classes if needed
00198             $sClassName = $this->_makeSafeModuleClassParents( $aClassChain, $sParent );
00199         }
00200 
00201         // check if there is a path, if yes, remove it
00202         $sClassName = basename( $sClassName );
00203 
00204         return $sClassName;
00205     }
00206 
00215     public function isModuleActive( $sClassName, $sModuleName )
00216     {
00217         $aModules = $this->getConfig()->getConfigParam( 'aModules' );
00218         if ( is_array( $aModules ) && array_key_exists( $sClassName, $aModules ) ) {
00219             $aClassChain = explode( "&", $aModules[$sClassName] );
00220             foreach ($aClassChain as $sModule) {
00221                 if ( basename($sModule) == $sModuleName ) {
00222                     return true;
00223                 }
00224             }
00225         }
00226         return false;
00227     }
00228 
00239     private function _makeSafeModuleClassParents( $aClassChain, $sBaseModule )
00240     {
00241         $myConfig = $this->getConfig();
00242         $sParent = $sBaseModule;
00243 
00244         //building middle classes if needed
00245         foreach ($aClassChain as $sModule) {
00246             //creating middle classes
00247             //e.g. class suboutput1_parent extends oxoutput {}
00248             //     class suboutput2_parent extends suboutput1 {}
00249             //$sModuleClass = $this->getClassName($sModule);
00250 
00251             //security: just preventing string termination
00252             $sModule = str_replace(chr(0), '', $sModule);
00253 
00254             //get parent and module class names from sub/suboutput2
00255             $sParentClass = basename($sParent);
00256             $sModuleClass = basename($sModule);
00257 
00258             //P
00259             //$sInitClass = "class ".$sModuleClass."_parent extends $sParentClass { function ".$sModuleClass."_parent(){ return ".$sParentClass."::".$sParentClass."();} }";
00260             $sInitClass = "class ".$sModuleClass."_parent extends $sParentClass {}";
00261 
00262             //initializing middle class
00263             if (!class_exists($sModuleClass."_parent", false)) {
00264                 eval($sInitClass);
00265             }
00266             $sParentPath = $myConfig->getConfigParam( 'sShopDir' )."/modules/".$sModule.".php";
00267 
00268             //including original file
00269             if ( file_exists( $sParentPath ) ) {
00270                 include_once $sParentPath;
00271             } elseif ( !class_exists( $sModuleClass ) ) {
00272                 //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 by  doxygen 1.6.2