PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezutils/classes/ezmodulefunctioninfo.php

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