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
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
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
00086 if ( isset( $this->_aClassNameCache[$sClassName] ) ) {
00087 $sActionClassName = $this->_aClassNameCache[$sClassName];
00088 } else {
00089 $sActionClassName = $this->getClassName( $sClassName );
00090
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
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
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
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
00189
00190
00191 $aClassChain = explode( "&", $aModules[$sClassName] );
00192
00193 $sParent = $sClassName;
00194
00195
00196 $sParent = str_replace(chr(0), '', $sParent);
00197
00198
00199 $sClassName = $this->_makeSafeModuleClassParents( $aClassChain, $sParent );
00200 }
00201
00202
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
00246 foreach ($aClassChain as $sModule) {
00247
00248
00249
00250
00251
00252
00253 $sModule = str_replace(chr(0), '', $sModule);
00254
00255
00256 $sParentClass = basename($sParent);
00257 $sModuleClass = basename($sModule);
00258
00259
00260
00261 $sInitClass = "class ".$sModuleClass."_parent extends $sParentClass {}";
00262
00263
00264 if (!class_exists($sModuleClass."_parent", false)) {
00265 eval($sInitClass);
00266 }
00267 $sParentPath = $myConfig->getConfigParam( 'sShopDir' )."/modules/".$sModule.".php";
00268
00269
00270 if ( file_exists( $sParentPath ) ) {
00271 require_once $sParentPath;
00272 } elseif ( !class_exists( $sModuleClass ) ) {
00273 $oEx = new oxSystemComponentException();
00274 $oEx->setMessage('EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND');
00275 $oEx->setComponent($sModule);
00276 }
00277
00278 $sParent = $sModule;
00279 }
00280
00281
00282 $sClassName = $aClassChain[count($aClassChain) - 1];
00283 return $sClassName;
00284 }
00285 }