PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezutils/classes/ezmodulefunctioninfo.php

https://github.com/eeggenberger/ezpublish
PHP | 321 lines | 262 code | 17 blank | 42 comment | 46 complexity | eb675f6113af4cc1e4da9c0d980a59bb MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZModuleFunctionInfo class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZModuleFunctionInfo ezmodulefunctioninfo.php
  12. \brief The class eZModuleFunctionInfo does
  13. */
  14. class eZModuleFunctionInfo
  15. {
  16. const ERROR_NO_CLASS = 5;
  17. const ERROR_NO_CLASS_METHOD = 6;
  18. const ERROR_CLASS_INSTANTIATE_FAILED = 7;
  19. const ERROR_MISSING_PARAMETER = 8;
  20. /*!
  21. Constructor
  22. */
  23. function eZModuleFunctionInfo( $moduleName )
  24. {
  25. $this->ModuleName = $moduleName;
  26. $this->IsValid = false;
  27. $this->FunctionList = array();
  28. }
  29. function isValid()
  30. {
  31. return $this->IsValid;
  32. }
  33. function loadDefinition()
  34. {
  35. $definitionFile = null;
  36. $pathList = eZModule::globalPathList();
  37. if ( $pathList )
  38. {
  39. foreach ( $pathList as $path )
  40. {
  41. $definitionFile = $path . '/' . $this->ModuleName . '/function_definition.php';
  42. if ( file_exists( $definitionFile ) )
  43. break;
  44. $definitionFile = null;
  45. }
  46. }
  47. if ( $definitionFile === null )
  48. {
  49. eZDebug::writeError( 'Missing function definition file for module: ' . $this->ModuleName, __METHOD__ );
  50. return false;
  51. }
  52. unset( $FunctionList );
  53. include( $definitionFile );
  54. if ( !isset( $FunctionList ) )
  55. {
  56. eZDebug::writeError( 'Missing function definition list for module: ' . $this->ModuleName, __METHOD__ );
  57. return false;
  58. }
  59. $this->FunctionList = $FunctionList;
  60. $this->IsValid = true;
  61. return true;
  62. }
  63. /*!
  64. Check if a parameter for a function is an array
  65. \param functionName function name
  66. \param parameterName parameter name
  67. \return true if parameter is supposed to be array
  68. */
  69. function isParameterArray( $functionName, $parameterName )
  70. {
  71. if ( isset( $this->FunctionList[$functionName]['parameters'] ) )
  72. {
  73. $parameterList = $this->FunctionList[$functionName]['parameters'];
  74. foreach ( $parameterList as $parameter )
  75. {
  76. if ( $parameter['name'] == $parameterName )
  77. {
  78. if ( $parameter['type'] == 'array' )
  79. {
  80. return true;
  81. }
  82. return false;
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. /*!
  89. Pre execute, used by template compilation to check as much as possible before runtime.
  90. \param functionName function name
  91. \return function definition, false if fails.
  92. */
  93. function preExecute( $functionName )
  94. {
  95. /* Code copied from this->execute() */
  96. $moduleName = $this->ModuleName;
  97. if ( !isset( $this->FunctionList[$functionName] ) )
  98. {
  99. eZDebug::writeError( "No such function '$functionName' in module '$moduleName'", __METHOD__ );
  100. return false;
  101. }
  102. $functionDefinition = $this->FunctionList[$functionName];
  103. if ( !isset( $functionName['call_method'] ) )
  104. {
  105. eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  106. return false;
  107. }
  108. if ( !isset( $functionName['parameters'] ) )
  109. {
  110. eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  111. return false;
  112. }
  113. $callMethod = $functionDefinition['call_method'];
  114. if ( isset( $callMethod['class'] ) &&
  115. isset( $callMethod['method'] ) )
  116. {
  117. if ( !class_exists( $callMethod['class'] ) )
  118. {
  119. return false;
  120. }
  121. $classObject = $this->objectForClass( $callMethod['class'] );
  122. if ( $classObject === null )
  123. {
  124. return false;
  125. }
  126. if ( !method_exists( $classObject, $callMethod['method'] ) )
  127. {
  128. return false;
  129. }
  130. return $functionDefinition;
  131. }
  132. return false;
  133. }
  134. function execute( $functionName, $functionParameters )
  135. {
  136. $moduleName = $this->ModuleName;
  137. if ( !isset( $this->FunctionList[$functionName] ) )
  138. {
  139. eZDebug::writeError( "No such function '$functionName' in module '$moduleName'", __METHOD__ );
  140. return null;
  141. }
  142. $functionDefinition = $this->FunctionList[$functionName];
  143. if ( !isset( $functionName['call_method'] ) )
  144. {
  145. eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  146. return null;
  147. }
  148. if ( !isset( $functionName['parameters'] ) )
  149. {
  150. eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  151. return null;
  152. }
  153. $callMethod = $functionDefinition['call_method'];
  154. if ( isset( $callMethod['class'] ) &&
  155. isset( $callMethod['method'] ) )
  156. {
  157. $resultArray = $this->executeClassMethod( $callMethod['class'], $callMethod['method'],
  158. $functionDefinition['parameters'], $functionParameters );
  159. }
  160. else
  161. {
  162. eZDebug::writeError( "No valid call methods found for function '$functionName' in module '$moduleName'", __METHOD__ );
  163. return null;
  164. }
  165. if ( !is_array( $resultArray ) )
  166. {
  167. eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result array", __METHOD__ );
  168. return null;
  169. }
  170. if ( isset( $resultArray['internal_error'] ) )
  171. {
  172. switch ( $resultArray['internal_error'] )
  173. {
  174. case eZModuleFunctionInfo::ERROR_NO_CLASS:
  175. {
  176. $className = $resultArray['internal_error_class_name'];
  177. eZDebug::writeError( "No class '$className' available for function '$functionName' in module '$moduleName'", __METHOD__ );
  178. return null;
  179. } break;
  180. case eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD:
  181. {
  182. $className = $resultArray['internal_error_class_name'];
  183. $classMethodName = $resultArray['internal_error_class_method_name'];
  184. eZDebug::writeError( "No method '$classMethodName' in class '$className' available for function '$functionName' in module '$moduleName'", __METHOD__ );
  185. return null;
  186. } break;
  187. case eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED:
  188. {
  189. $className = $resultArray['internal_error_class_name'];
  190. eZDebug::writeError( "Failed instantiating class '$className' which is needed for function '$functionName' in module '$moduleName'", __METHOD__ );
  191. return null;
  192. } break;
  193. case eZModuleFunctionInfo::ERROR_MISSING_PARAMETER:
  194. {
  195. $parameterName = $resultArray['internal_error_parameter_name'];
  196. eZDebug::writeError( "Missing parameter '$parameterName' for function '$functionName' in module '$moduleName'",
  197. __METHOD__ . " $moduleName::$functionName" );
  198. return null;
  199. } break;
  200. default:
  201. {
  202. $internalError = $resultArray['internal_error'];
  203. eZDebug::writeError( "Unknown internal error '$internalError' for function '$functionName' in module '$moduleName'", __METHOD__ );
  204. return null;
  205. } break;
  206. }
  207. return null;
  208. }
  209. else if ( isset( $resultArray['error'] ) )
  210. {
  211. }
  212. else if ( isset( $resultArray['result'] ) )
  213. {
  214. return $resultArray['result'];
  215. }
  216. else
  217. {
  218. eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result value", __METHOD__ );
  219. }
  220. return null;
  221. }
  222. function objectForClass( $className )
  223. {
  224. if ( !isset( $GLOBALS['eZModuleFunctionClassObjectList'] ) )
  225. {
  226. $GLOBALS['eZModuleFunctionClassObjectList'] = array();
  227. }
  228. if ( isset( $GLOBALS['eZModuleFunctionClassObjectList'][$className] ) )
  229. {
  230. return $GLOBALS['eZModuleFunctionClassObjectList'][$className];
  231. }
  232. return $GLOBALS['eZModuleFunctionClassObjectList'][$className] = new $className();
  233. }
  234. function executeClassMethod( $className, $methodName,
  235. $functionParameterDefinitions, $functionParameters )
  236. {
  237. if ( !class_exists( $className ) )
  238. {
  239. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS,
  240. 'internal_error_class_name' => $className );
  241. }
  242. $classObject = $this->objectForClass( $className );
  243. if ( $classObject === null )
  244. {
  245. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED,
  246. 'internal_error_class_name' => $className );
  247. }
  248. if ( !method_exists( $classObject, $methodName ) )
  249. {
  250. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD,
  251. 'internal_error_class_name' => $className,
  252. 'internal_error_class_method_name' => $methodName );
  253. }
  254. $parameterArray = array();
  255. foreach ( $functionParameterDefinitions as $functionParameterDefinition )
  256. {
  257. $parameterName = $functionParameterDefinition['name'];
  258. if ( isset( $functionParameters[$parameterName] ) )
  259. {
  260. // Do type checking
  261. $parameterArray[] = $functionParameters[$parameterName];
  262. }
  263. else
  264. {
  265. if ( $functionParameterDefinition['required'] )
  266. {
  267. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_MISSING_PARAMETER,
  268. 'internal_error_parameter_name' => $parameterName );
  269. }
  270. else if ( isset( $functionParameterDefinition['default'] ) )
  271. {
  272. $parameterArray[] = $functionParameterDefinition['default'];
  273. }
  274. else
  275. {
  276. $parameterArray[] = null;
  277. }
  278. }
  279. }
  280. return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
  281. }
  282. /*!
  283. \deprecated use call_user_func_array() instead
  284. */
  285. function callClassMethod( $methodName, $classObject, $parameterArray )
  286. {
  287. /* echo "*********** fetching START **************** <br>";
  288. var_dump( $methodName );
  289. var_dump( $classObject );
  290. var_dump( $parameterArray );
  291. echo "*********** fetching END ******************<br><br><br>";*/
  292. return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
  293. }
  294. /// \privatesection
  295. public $ModuleName;
  296. public $FunctionList;
  297. public $IsValid;
  298. }
  299. ?>