|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZModuleFunctionInfo class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package lib 00009 */ 00010 00011 /*! 00012 \class eZModuleFunctionInfo ezmodulefunctioninfo.php 00013 \brief The class eZModuleFunctionInfo does 00014 00015 */ 00016 00017 class eZModuleFunctionInfo 00018 { 00019 const ERROR_NO_CLASS = 5; 00020 const ERROR_NO_CLASS_METHOD = 6; 00021 const ERROR_CLASS_INSTANTIATE_FAILED = 7; 00022 const ERROR_MISSING_PARAMETER = 8; 00023 00024 /*! 00025 Constructor 00026 */ 00027 function eZModuleFunctionInfo( $moduleName ) 00028 { 00029 $this->ModuleName = $moduleName; 00030 $this->IsValid = false; 00031 $this->FunctionList = array(); 00032 } 00033 00034 function isValid() 00035 { 00036 return $this->IsValid; 00037 } 00038 00039 function loadDefinition() 00040 { 00041 $definitionFile = null; 00042 $pathList = eZModule::globalPathList(); 00043 if ( $pathList ) 00044 { 00045 foreach ( $pathList as $path ) 00046 { 00047 $definitionFile = $path . '/' . $this->ModuleName . '/function_definition.php'; 00048 if ( file_exists( $definitionFile ) ) 00049 break; 00050 $definitionFile = null; 00051 } 00052 } 00053 if ( $definitionFile === null ) 00054 { 00055 eZDebug::writeError( 'Missing function definition file for module: ' . $this->ModuleName, __METHOD__ ); 00056 return false; 00057 } 00058 unset( $FunctionList ); 00059 include( $definitionFile ); 00060 if ( !isset( $FunctionList ) ) 00061 { 00062 eZDebug::writeError( 'Missing function definition list for module: ' . $this->ModuleName, __METHOD__ ); 00063 return false; 00064 } 00065 $this->FunctionList = $FunctionList; 00066 $this->IsValid = true; 00067 return true; 00068 } 00069 00070 /*! 00071 Check if a parameter for a function is an array 00072 00073 \param functionName function name 00074 \param parameterName parameter name 00075 00076 \return true if parameter is supposed to be array 00077 */ 00078 function isParameterArray( $functionName, $parameterName ) 00079 { 00080 if ( isset( $this->FunctionList[$functionName]['parameters'] ) ) 00081 { 00082 $parameterList = $this->FunctionList[$functionName]['parameters']; 00083 foreach ( $parameterList as $parameter ) 00084 { 00085 if ( $parameter['name'] == $parameterName ) 00086 { 00087 if ( $parameter['type'] == 'array' ) 00088 { 00089 return true; 00090 } 00091 return false; 00092 } 00093 } 00094 } 00095 return false; 00096 } 00097 00098 /*! 00099 Pre execute, used by template compilation to check as much as possible before runtime. 00100 00101 \param functionName function name 00102 00103 \return function definition, false if fails. 00104 */ 00105 function preExecute( $functionName ) 00106 { 00107 /* Code copied from this->execute() */ 00108 $moduleName = $this->ModuleName; 00109 if ( !isset( $this->FunctionList[$functionName] ) ) 00110 { 00111 eZDebug::writeError( "No such function '$functionName' in module '$moduleName'", __METHOD__ ); 00112 return false; 00113 } 00114 $functionDefinition = $this->FunctionList[$functionName]; 00115 if ( !isset( $functionDefinition['call_method'] ) ) 00116 { 00117 eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'", __METHOD__ ); 00118 return false; 00119 } 00120 if ( !isset( $functionDefinition['parameters'] ) ) 00121 { 00122 eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'", __METHOD__ ); 00123 return false; 00124 } 00125 $callMethod = $functionDefinition['call_method']; 00126 if ( isset( $callMethod['class'] ) && 00127 isset( $callMethod['method'] ) ) 00128 { 00129 if ( !class_exists( $callMethod['class'] ) ) 00130 { 00131 return false; 00132 } 00133 $classObject = $this->objectForClass( $callMethod['class'] ); 00134 if ( $classObject === null ) 00135 { 00136 return false; 00137 } 00138 if ( !method_exists( $classObject, $callMethod['method'] ) ) 00139 { 00140 return false; 00141 } 00142 00143 return $functionDefinition; 00144 } 00145 return false; 00146 } 00147 00148 function execute( $functionName, $functionParameters ) 00149 { 00150 $moduleName = $this->ModuleName; 00151 if ( !isset( $this->FunctionList[$functionName] ) ) 00152 { 00153 eZDebug::writeError( "No such function '$functionName' in module '$moduleName'", __METHOD__ ); 00154 return null; 00155 } 00156 $functionDefinition = $this->FunctionList[$functionName]; 00157 if ( !isset( $functionDefinition['call_method'] ) ) 00158 { 00159 eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'", __METHOD__ ); 00160 return null; 00161 } 00162 if ( !isset( $functionDefinition['parameters'] ) ) 00163 { 00164 eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'", __METHOD__ ); 00165 return null; 00166 } 00167 $callMethod = $functionDefinition['call_method']; 00168 if ( isset( $callMethod['class'] ) && 00169 isset( $callMethod['method'] ) ) 00170 { 00171 $resultArray = $this->executeClassMethod( $callMethod['class'], $callMethod['method'], 00172 $functionDefinition['parameters'], $functionParameters ); 00173 } 00174 else 00175 { 00176 eZDebug::writeError( "No valid call methods found for function '$functionName' in module '$moduleName'", __METHOD__ ); 00177 return null; 00178 } 00179 if ( !is_array( $resultArray ) ) 00180 { 00181 eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result array", __METHOD__ ); 00182 return null; 00183 } 00184 if ( isset( $resultArray['internal_error'] ) ) 00185 { 00186 switch ( $resultArray['internal_error'] ) 00187 { 00188 case eZModuleFunctionInfo::ERROR_NO_CLASS: 00189 { 00190 $className = $resultArray['internal_error_class_name']; 00191 eZDebug::writeError( "No class '$className' available for function '$functionName' in module '$moduleName'", __METHOD__ ); 00192 return null; 00193 } break; 00194 case eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD: 00195 { 00196 $className = $resultArray['internal_error_class_name']; 00197 $classMethodName = $resultArray['internal_error_class_method_name']; 00198 eZDebug::writeError( "No method '$classMethodName' in class '$className' available for function '$functionName' in module '$moduleName'", __METHOD__ ); 00199 return null; 00200 } break; 00201 case eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED: 00202 { 00203 $className = $resultArray['internal_error_class_name']; 00204 eZDebug::writeError( "Failed instantiating class '$className' which is needed for function '$functionName' in module '$moduleName'", __METHOD__ ); 00205 return null; 00206 } break; 00207 case eZModuleFunctionInfo::ERROR_MISSING_PARAMETER: 00208 { 00209 $parameterName = $resultArray['internal_error_parameter_name']; 00210 eZDebug::writeError( "Missing parameter '$parameterName' for function '$functionName' in module '$moduleName'", 00211 __METHOD__ . " $moduleName::$functionName" ); 00212 return null; 00213 } break; 00214 default: 00215 { 00216 $internalError = $resultArray['internal_error']; 00217 eZDebug::writeError( "Unknown internal error '$internalError' for function '$functionName' in module '$moduleName'", __METHOD__ ); 00218 return null; 00219 } break; 00220 } 00221 return null; 00222 } 00223 else if ( isset( $resultArray['error'] ) ) 00224 { 00225 } 00226 else if ( isset( $resultArray['result'] ) ) 00227 { 00228 return $resultArray['result']; 00229 } 00230 else 00231 { 00232 eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result value", __METHOD__ ); 00233 } 00234 return null; 00235 } 00236 00237 function objectForClass( $className ) 00238 { 00239 if ( !isset( $GLOBALS['eZModuleFunctionClassObjectList'] ) ) 00240 { 00241 $GLOBALS['eZModuleFunctionClassObjectList'] = array(); 00242 } 00243 if ( isset( $GLOBALS['eZModuleFunctionClassObjectList'][$className] ) ) 00244 { 00245 return $GLOBALS['eZModuleFunctionClassObjectList'][$className]; 00246 } 00247 return $GLOBALS['eZModuleFunctionClassObjectList'][$className] = new $className(); 00248 } 00249 00250 function executeClassMethod( $className, $methodName, 00251 $functionParameterDefinitions, $functionParameters ) 00252 { 00253 if ( !class_exists( $className ) ) 00254 { 00255 return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS, 00256 'internal_error_class_name' => $className ); 00257 } 00258 $classObject = $this->objectForClass( $className ); 00259 if ( $classObject === null ) 00260 { 00261 return array( 'internal_error' => eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED, 00262 'internal_error_class_name' => $className ); 00263 } 00264 if ( !method_exists( $classObject, $methodName ) ) 00265 { 00266 return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD, 00267 'internal_error_class_name' => $className, 00268 'internal_error_class_method_name' => $methodName ); 00269 } 00270 $parameterArray = array(); 00271 foreach ( $functionParameterDefinitions as $functionParameterDefinition ) 00272 { 00273 $parameterName = $functionParameterDefinition['name']; 00274 if ( isset( $functionParameters[$parameterName] ) ) 00275 { 00276 // Do type checking 00277 $parameterArray[] = $functionParameters[$parameterName]; 00278 } 00279 else 00280 { 00281 if ( $functionParameterDefinition['required'] ) 00282 { 00283 return array( 'internal_error' => eZModuleFunctionInfo::ERROR_MISSING_PARAMETER, 00284 'internal_error_parameter_name' => $parameterName ); 00285 } 00286 else if ( isset( $functionParameterDefinition['default'] ) ) 00287 { 00288 $parameterArray[] = $functionParameterDefinition['default']; 00289 } 00290 else 00291 { 00292 $parameterArray[] = null; 00293 } 00294 } 00295 } 00296 00297 return call_user_func_array( array( $classObject, $methodName ), $parameterArray ); 00298 } 00299 00300 /*! 00301 \deprecated use call_user_func_array() instead 00302 */ 00303 function callClassMethod( $methodName, $classObject, $parameterArray ) 00304 { 00305 /* echo "*********** fetching START **************** <br>"; 00306 var_dump( $methodName ); 00307 var_dump( $classObject ); 00308 var_dump( $parameterArray ); 00309 echo "*********** fetching END ******************<br><br><br>";*/ 00310 00311 return call_user_func_array( array( $classObject, $methodName ), $parameterArray ); 00312 } 00313 00314 00315 /// \privatesection 00316 public $ModuleName; 00317 public $FunctionList; 00318 public $IsValid; 00319 } 00320 00321 ?>