PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezutils/classes/ezmodulefunctioninfo.php

https://github.com/aurelienRT1/ezpublish
PHP | 359 lines | 277 code | 18 blank | 64 comment | 46 complexity | b869af2aa2a3ff6d4cd34b9ed1b98e59 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //
  3. // Definition of eZModuleFunctionInfo class
  4. //
  5. // Created on: <06-Oct-2002 16:27:36 amos>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. /*!
  33. \class eZModuleFunctionInfo ezmodulefunctioninfo.php
  34. \brief The class eZModuleFunctionInfo does
  35. */
  36. class eZModuleFunctionInfo
  37. {
  38. const ERROR_NO_CLASS = 5;
  39. const ERROR_NO_CLASS_METHOD = 6;
  40. const ERROR_CLASS_INSTANTIATE_FAILED = 7;
  41. const ERROR_MISSING_PARAMETER = 8;
  42. /*!
  43. Constructor
  44. */
  45. function eZModuleFunctionInfo( $moduleName )
  46. {
  47. $this->ModuleName = $moduleName;
  48. $this->IsValid = false;
  49. $this->FunctionList = array();
  50. }
  51. function isValid()
  52. {
  53. return $this->IsValid;
  54. }
  55. function loadDefinition()
  56. {
  57. $definitionFile = null;
  58. $pathList = eZModule::globalPathList();
  59. if ( $pathList )
  60. {
  61. foreach ( $pathList as $path )
  62. {
  63. $definitionFile = $path . '/' . $this->ModuleName . '/function_definition.php';
  64. if ( file_exists( $definitionFile ) )
  65. break;
  66. $definitionFile = null;
  67. }
  68. }
  69. if ( $definitionFile === null )
  70. {
  71. eZDebug::writeError( 'Missing function definition file for module: ' . $this->ModuleName,
  72. 'eZModuleFunctionInfo::loadDefinition' );
  73. return false;
  74. }
  75. unset( $FunctionList );
  76. include( $definitionFile );
  77. if ( !isset( $FunctionList ) )
  78. {
  79. eZDebug::writeError( 'Missing function definition list for module: ' . $this->ModuleName,
  80. 'eZModuleFunctionInfo::loadDefinition' );
  81. return false;
  82. }
  83. $this->FunctionList = $FunctionList;
  84. $this->IsValid = true;
  85. return true;
  86. }
  87. /*!
  88. Check if a parameter for a function is an array
  89. \param functionName function name
  90. \param parameterName parameter name
  91. \return true if parameter is supposed to be array
  92. */
  93. function isParameterArray( $functionName, $parameterName )
  94. {
  95. if ( isset( $this->FunctionList[$functionName]['parameters'] ) )
  96. {
  97. $parameterList = $this->FunctionList[$functionName]['parameters'];
  98. foreach ( $parameterList as $parameter )
  99. {
  100. if ( $parameter['name'] == $parameterName )
  101. {
  102. if ( $parameter['type'] == 'array' )
  103. {
  104. return true;
  105. }
  106. return false;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. /*!
  113. Pre execute, used by template compilation to check as much as possible before runtime.
  114. \param functionName function name
  115. \return function definition, false if fails.
  116. */
  117. function preExecute( $functionName )
  118. {
  119. /* Code copied from this->execute() */
  120. $moduleName = $this->ModuleName;
  121. if ( !isset( $this->FunctionList[$functionName] ) )
  122. {
  123. eZDebug::writeError( "No such function '$functionName' in module '$moduleName'",
  124. 'eZModuleFunctionInfo::execute' );
  125. return false;
  126. }
  127. $functionDefinition = $this->FunctionList[$functionName];
  128. if ( !isset( $functionName['call_method'] ) )
  129. {
  130. eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'",
  131. 'eZModuleFunctionInfo::execute' );
  132. return false;
  133. }
  134. if ( !isset( $functionName['parameters'] ) )
  135. {
  136. eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'",
  137. 'eZModuleFunctionInfo::execute' );
  138. return false;
  139. }
  140. $callMethod = $functionDefinition['call_method'];
  141. if ( isset( $callMethod['class'] ) &&
  142. isset( $callMethod['method'] ) )
  143. {
  144. if ( !class_exists( $callMethod['class'] ) )
  145. {
  146. return false;
  147. }
  148. $classObject = $this->objectForClass( $callMethod['class'] );
  149. if ( $classObject === null )
  150. {
  151. return false;
  152. }
  153. if ( !method_exists( $classObject, $callMethod['method'] ) )
  154. {
  155. return false;
  156. }
  157. return $functionDefinition;
  158. }
  159. return false;
  160. }
  161. function execute( $functionName, $functionParameters )
  162. {
  163. $moduleName = $this->ModuleName;
  164. if ( !isset( $this->FunctionList[$functionName] ) )
  165. {
  166. eZDebug::writeError( "No such function '$functionName' in module '$moduleName'",
  167. 'eZModuleFunctionInfo::execute' );
  168. return null;
  169. }
  170. $functionDefinition = $this->FunctionList[$functionName];
  171. if ( !isset( $functionName['call_method'] ) )
  172. {
  173. eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'",
  174. 'eZModuleFunctionInfo::execute' );
  175. return null;
  176. }
  177. if ( !isset( $functionName['parameters'] ) )
  178. {
  179. eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'",
  180. 'eZModuleFunctionInfo::execute' );
  181. return null;
  182. }
  183. $callMethod = $functionDefinition['call_method'];
  184. if ( isset( $callMethod['class'] ) &&
  185. isset( $callMethod['method'] ) )
  186. {
  187. $resultArray = $this->executeClassMethod( $callMethod['class'], $callMethod['method'],
  188. $functionDefinition['parameters'], $functionParameters );
  189. }
  190. else
  191. {
  192. eZDebug::writeError( "No valid call methods found for function '$functionName' in module '$moduleName'",
  193. 'eZModuleFunctionInfo::execute' );
  194. return null;
  195. }
  196. if ( !is_array( $resultArray ) )
  197. {
  198. eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result array",
  199. 'eZFunctionHandler::execute' );
  200. return null;
  201. }
  202. if ( isset( $resultArray['internal_error'] ) )
  203. {
  204. switch ( $resultArray['internal_error'] )
  205. {
  206. case eZModuleFunctionInfo::ERROR_NO_CLASS:
  207. {
  208. $className = $resultArray['internal_error_class_name'];
  209. eZDebug::writeError( "No class '$className' available for function '$functionName' in module '$moduleName'",
  210. 'eZModuleFunctionInfo::execute' );
  211. return null;
  212. } break;
  213. case eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD:
  214. {
  215. $className = $resultArray['internal_error_class_name'];
  216. $classMethodName = $resultArray['internal_error_class_method_name'];
  217. eZDebug::writeError( "No method '$classMethodName' in class '$className' available for function '$functionName' in module '$moduleName'",
  218. 'eZModuleFunctionInfo::execute' );
  219. return null;
  220. } break;
  221. case eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED:
  222. {
  223. $className = $resultArray['internal_error_class_name'];
  224. eZDebug::writeError( "Failed instantiating class '$className' which is needed for function '$functionName' in module '$moduleName'",
  225. 'eZModuleFunctionInfo::execute' );
  226. return null;
  227. } break;
  228. case eZModuleFunctionInfo::ERROR_MISSING_PARAMETER:
  229. {
  230. $parameterName = $resultArray['internal_error_parameter_name'];
  231. eZDebug::writeError( "Missing parameter '$parameterName' for function '$functionName' in module '$moduleName'",
  232. "eZModuleFunctionInfo::execute $moduleName::$functionName" );
  233. return null;
  234. } break;
  235. default:
  236. {
  237. $internalError = $resultArray['internal_error'];
  238. eZDebug::writeError( "Unknown internal error '$internalError' for function '$functionName' in module '$moduleName'",
  239. 'eZModuleFunctionInfo::execute' );
  240. return null;
  241. } break;
  242. }
  243. return null;
  244. }
  245. else if ( isset( $resultArray['error'] ) )
  246. {
  247. }
  248. else if ( isset( $resultArray['result'] ) )
  249. {
  250. return $resultArray['result'];
  251. }
  252. else
  253. {
  254. eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result value",
  255. 'eZFunctionHandler::execute' );
  256. }
  257. return null;
  258. }
  259. function objectForClass( $className )
  260. {
  261. if ( !isset( $GLOBALS['eZModuleFunctionClassObjectList'] ) )
  262. {
  263. $GLOBALS['eZModuleFunctionClassObjectList'] = array();
  264. }
  265. if ( isset( $GLOBALS['eZModuleFunctionClassObjectList'][$className] ) )
  266. {
  267. return $GLOBALS['eZModuleFunctionClassObjectList'][$className];
  268. }
  269. return $GLOBALS['eZModuleFunctionClassObjectList'][$className] = new $className();
  270. }
  271. function executeClassMethod( $className, $methodName,
  272. $functionParameterDefinitions, $functionParameters )
  273. {
  274. if ( !class_exists( $className ) )
  275. {
  276. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS,
  277. 'internal_error_class_name' => $className );
  278. }
  279. $classObject = $this->objectForClass( $className );
  280. if ( $classObject === null )
  281. {
  282. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED,
  283. 'internal_error_class_name' => $className );
  284. }
  285. if ( !method_exists( $classObject, $methodName ) )
  286. {
  287. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD,
  288. 'internal_error_class_name' => $className,
  289. 'internal_error_class_method_name' => $methodName );
  290. }
  291. $parameterArray = array();
  292. foreach ( $functionParameterDefinitions as $functionParameterDefinition )
  293. {
  294. $parameterName = $functionParameterDefinition['name'];
  295. if ( isset( $functionParameters[$parameterName] ) )
  296. {
  297. // Do type checking
  298. $parameterArray[] = $functionParameters[$parameterName];
  299. }
  300. else
  301. {
  302. if ( $functionParameterDefinition['required'] )
  303. {
  304. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_MISSING_PARAMETER,
  305. 'internal_error_parameter_name' => $parameterName );
  306. }
  307. else if ( isset( $functionParameterDefinition['default'] ) )
  308. {
  309. $parameterArray[] = $functionParameterDefinition['default'];
  310. }
  311. else
  312. {
  313. $parameterArray[] = null;
  314. }
  315. }
  316. }
  317. return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
  318. }
  319. /*!
  320. \deprecated use call_user_func_array() instead
  321. */
  322. function callClassMethod( $methodName, $classObject, $parameterArray )
  323. {
  324. /* echo "*********** fetching START **************** <br>";
  325. var_dump( $methodName );
  326. var_dump( $classObject );
  327. var_dump( $parameterArray );
  328. echo "*********** fetching END ******************<br><br><br>";*/
  329. return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
  330. }
  331. /// \privatesection
  332. public $ModuleName;
  333. public $FunctionList;
  334. public $IsValid;
  335. }
  336. ?>