00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 include_once( 'lib/ezutils/classes/ezmodule.php' );
00041 include_once( 'lib/ezutils/classes/ezdebug.php' );
00042
00043 define( 'EZ_MODULE_FUNCTION_ERROR_NO_CLASS', 5 );
00044 define( 'EZ_MODULE_FUNCTION_ERROR_NO_CLASS_METHOD', 6 );
00045 define( 'EZ_MODULE_FUNCTION_ERROR_CLASS_INSTANTIATE_FAILED', 7 );
00046 define( 'EZ_MODULE_FUNCTION_ERROR_MISSING_PARAMETER', 8 );
00047
00048 class eZModuleFunctionInfo
00049 {
00050
00051
00052
00053 function eZModuleFunctionInfo( $moduleName )
00054 {
00055 $this->ModuleName = $moduleName;
00056 $this->IsValid = false;
00057 $this->FunctionList = array();
00058 $this->UseOldCall = false;
00059 }
00060
00061 function isValid()
00062 {
00063 return $this->IsValid;
00064 }
00065
00066 function loadDefinition()
00067 {
00068 $definitionFile = null;
00069 $pathList = eZModule::globalPathList();
00070 if ( $pathList )
00071 {
00072 foreach ( $pathList as $path )
00073 {
00074 $definitionFile = $path . '/' . $this->ModuleName . '/function_definition.php';
00075 if ( file_exists( $definitionFile ) )
00076 break;
00077 $definitionFile = null;
00078 }
00079 }
00080 if ( $definitionFile === null )
00081 {
00082 eZDebug::writeError( 'Missing function definition file for module: ' . $this->ModuleName,
00083 'eZModuleFunctionInfo::loadDefinition' );
00084 return false;
00085 }
00086 unset( $FunctionList );
00087 include( $definitionFile );
00088 if ( !isset( $FunctionList ) )
00089 {
00090 eZDebug::writeError( 'Missing function definition list for module: ' . $this->ModuleName,
00091 'eZModuleFunctionInfo::loadDefinition' );
00092 return false;
00093 }
00094 $this->FunctionList = $FunctionList;
00095 $this->IsValid = true;
00096 return true;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 function isParameterArray( $functionName, $parameterName )
00108 {
00109 if ( isset( $this->FunctionList[$functionName]['parameters'] ) )
00110 {
00111 $parameterList =& $this->FunctionList[$functionName]['parameters'];
00112 foreach ( array_keys( $parameterList ) as $key )
00113 {
00114 if ( $parameterList[$key]['name'] == $parameterName )
00115 {
00116 if ( $parameterList[$key]['type'] == 'array' )
00117 return true;
00118 return false;
00119 }
00120 }
00121 }
00122 return false;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132 function &preExecute( $functionName )
00133 {
00134
00135 $Return = false;
00136 $moduleName = $this->ModuleName;
00137 if ( !isset( $this->FunctionList[$functionName] ) )
00138 {
00139 eZDebug::writeError( "No such function '$functionName' in module '$moduleName'",
00140 'eZModuleFunctionInfo::execute' );
00141 return $Return;
00142 }
00143 $functionDefinition =& $this->FunctionList[$functionName];
00144 if ( !isset( $functionName['call_method'] ) )
00145 {
00146 eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'",
00147 'eZModuleFunctionInfo::execute' );
00148 return $Return;
00149 }
00150 if ( !isset( $functionName['parameters'] ) )
00151 {
00152 eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'",
00153 'eZModuleFunctionInfo::execute' );
00154 return $Return;
00155 }
00156 $callMethod =& $functionDefinition['call_method'];
00157 if ( isset( $callMethod['include_file'] ) and
00158 isset( $callMethod['class'] ) and
00159 isset( $callMethod['method'] ) )
00160 {
00161 $extension = false;
00162 if ( isset( $callMethod['extension'] ) )
00163 $extension = $callMethod['extension'];
00164
00165 if ( $extension )
00166 {
00167 include_once( 'lib/ezutils/classes/ezextension.php' );
00168 $extensionDir = eZExtension::baseDirectory();
00169 $callMethod['include_file'] = $extensionDir . '/' . $extension . '/modules/' . $callMethod['include_file'];
00170 }
00171 include_once( $callMethod['include_file'] );
00172 if ( !class_exists( $callMethod['class'] ) )
00173 {
00174 return $Return;
00175 }
00176 $classObject =& $this->objectForClass( $callMethod['class'] );
00177 if ( $classObject === null )
00178 {
00179 return $Return;
00180 }
00181 if ( !method_exists( $classObject, $callMethod['method'] ) )
00182 {
00183 return $Return;
00184 }
00185
00186 return $functionDefinition;
00187 }
00188 return $Return;
00189 }
00190
00191 function execute( $functionName, $functionParameters )
00192 {
00193 $moduleName = $this->ModuleName;
00194 if ( !isset( $this->FunctionList[$functionName] ) )
00195 {
00196 eZDebug::writeError( "No such function '$functionName' in module '$moduleName'",
00197 'eZModuleFunctionInfo::execute' );
00198 return null;
00199 }
00200 $functionDefinition =& $this->FunctionList[$functionName];
00201 if ( !isset( $functionName['call_method'] ) )
00202 {
00203 eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'",
00204 'eZModuleFunctionInfo::execute' );
00205 return null;
00206 }
00207 if ( !isset( $functionName['parameters'] ) )
00208 {
00209 eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'",
00210 'eZModuleFunctionInfo::execute' );
00211 return null;
00212 }
00213 $callMethod =& $functionDefinition['call_method'];
00214 if ( isset( $callMethod['include_file'] ) and
00215 isset( $callMethod['class'] ) and
00216 isset( $callMethod['method'] ) )
00217 {
00218 $extension = false;
00219 if ( isset( $callMethod['extension'] ) )
00220 $extension = $callMethod['extension'];
00221 $resultArray = $this->executeClassMethod( $extension, $callMethod['include_file'], $callMethod['class'], $callMethod['method'],
00222 $functionDefinition['parameters'], $functionParameters );
00223 }
00224 else
00225 {
00226 eZDebug::writeError( "No valid call methods found for function '$functionName' in module '$moduleName'",
00227 'eZModuleFunctionInfo::execute' );
00228 return null;
00229 }
00230 if ( !is_array( $resultArray ) )
00231 {
00232 eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result array",
00233 'eZFunctionHandler::execute' );
00234 return null;
00235 }
00236 if ( isset( $resultArray['internal_error'] ) )
00237 {
00238 switch ( $resultArray['internal_error'] )
00239 {
00240 case EZ_MODULE_FUNCTION_ERROR_NO_CLASS:
00241 {
00242 $className = $resultArray['internal_error_class_name'];
00243 eZDebug::writeError( "No class '$className' available for function '$functionName' in module '$moduleName'",
00244 'eZModuleFunctionInfo::execute' );
00245 return null;
00246 } break;
00247 case EZ_MODULE_FUNCTION_ERROR_NO_CLASS_METHOD:
00248 {
00249 $className = $resultArray['internal_error_class_name'];
00250 $classMethodName = $resultArray['internal_error_class_method_name'];
00251 eZDebug::writeError( "No method '$classMethodName' in class '$className' available for function '$functionName' in module '$moduleName'",
00252 'eZModuleFunctionInfo::execute' );
00253 return null;
00254 } break;
00255 case EZ_MODULE_FUNCTION_ERROR_CLASS_INSTANTIATE_FAILED:
00256 {
00257 $className = $resultArray['internal_error_class_name'];
00258 eZDebug::writeError( "Failed instantiating class '$className' which is needed for function '$functionName' in module '$moduleName'",
00259 'eZModuleFunctionInfo::execute' );
00260 return null;
00261 } break;
00262 case EZ_MODULE_FUNCTION_ERROR_MISSING_PARAMETER:
00263 {
00264 $parameterName = $resultArray['internal_error_parameter_name'];
00265 eZDebug::writeError( "Missing parameter '$parameterName' for function '$functionName' in module '$moduleName'",
00266 "eZModuleFunctionInfo::execute $moduleName::$functionName" );
00267 return null;
00268 } break;
00269 default:
00270 {
00271 $internalError = $resultArray['internal_error'];
00272 eZDebug::writeError( "Unknown internal error '$internalError' for function '$functionName' in module '$moduleName'",
00273 'eZModuleFunctionInfo::execute' );
00274 return null;
00275 } break;
00276 }
00277 return null;
00278 }
00279 else if ( isset( $resultArray['error'] ) )
00280 {
00281 }
00282 else if ( isset( $resultArray['result'] ) )
00283 {
00284 return $resultArray['result'];
00285 }
00286 else
00287 {
00288 eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result value",
00289 'eZFunctionHandler::execute' );
00290 }
00291 return null;
00292 }
00293
00294 function &objectForClass( $className )
00295 {
00296 $classObjectList =& $GLOBALS['eZModuleFunctionClassObjectList'];
00297 if ( !isset( $classObjectList ) )
00298 $classObjectList = array();
00299 if ( isset( $classObjectList[$className] ) )
00300 return $classObjectList[$className];
00301 $classObject = new $className();
00302 $classObjectList[$className] =& $classObject;
00303 return $classObject;
00304 }
00305
00306 function executeClassMethod( $extension, $includeFile, $className, $methodName,
00307 $functionParameterDefinitions, $functionParameters )
00308 {
00309 if ( $extension )
00310 {
00311 include_once( 'lib/ezutils/classes/ezextension.php' );
00312 $extensionDir = eZExtension::baseDirectory();
00313 $includeFile = $extensionDir . '/' . $extension . '/modules/' . $includeFile;
00314 }
00315 include_once( $includeFile );
00316 if ( !class_exists( $className ) )
00317 {
00318 return array( 'internal_error' => EZ_MODULE_FUNCTION_ERROR_NO_CLASS,
00319 'internal_error_class_name' => $className );
00320 }
00321 $classObject =& $this->objectForClass( $className );
00322 if ( $classObject === null )
00323 {
00324 return array( 'internal_error' => EZ_MODULE_FUNCTION_ERROR_CLASS_INSTANTIATE_FAILED,
00325 'internal_error_class_name' => $className );
00326 }
00327 if ( !method_exists( $classObject, $methodName ) )
00328 {
00329 return array( 'internal_error' => EZ_MODULE_FUNCTION_ERROR_NO_CLASS_METHOD,
00330 'internal_error_class_name' => $className,
00331 'internal_error_class_method_name' => $methodName );
00332 }
00333 $parameterArray = array();
00334 foreach ( $functionParameterDefinitions as $functionParameterDefinition )
00335 {
00336 $parameterName = $functionParameterDefinition['name'];
00337 if ( isset( $functionParameters[$parameterName] ) )
00338 {
00339
00340 $parameterArray[] = $functionParameters[$parameterName];
00341 }
00342 else
00343 {
00344 if ( $functionParameterDefinition['required'] )
00345 {
00346 return array( 'internal_error' => EZ_MODULE_FUNCTION_ERROR_MISSING_PARAMETER,
00347 'internal_error_parameter_name' => $parameterName );
00348 }
00349 else if ( isset( $functionParameterDefinition['default'] ) )
00350 {
00351 $parameterArray[] = $functionParameterDefinition['default'];
00352 }
00353 else
00354 {
00355 $parameterArray[] = null;
00356 }
00357 }
00358 }
00359 return $this->callClassMethod( $methodName, $classObject, $parameterArray );
00360 }
00361
00362 function callClassMethod( $methodName, &$classObject, $parameterArray )
00363 {
00364
00365
00366
00367
00368
00369 if ( $this->UseOldCall )
00370 return call_user_method_array( $methodName, $classObject, $parameterArray );
00371 else
00372 return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
00373 }
00374
00375
00376
00377 var $ModuleName;
00378 var $FunctionList;
00379 var $IsValid;
00380 var $UseOldCall;
00381 }
00382
00383 ?>