PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezutils/classes/ezmodulefunctioninfo.php

https://github.com/StephanBoganskyXrow/ezpublish
PHP | 344 lines | 262 code | 18 blank | 64 comment | 46 complexity | 591db41cd5c0fc5d257a9285e145507a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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-2011 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, __METHOD__ );
  72. return false;
  73. }
  74. unset( $FunctionList );
  75. include( $definitionFile );
  76. if ( !isset( $FunctionList ) )
  77. {
  78. eZDebug::writeError( 'Missing function definition list for module: ' . $this->ModuleName, __METHOD__ );
  79. return false;
  80. }
  81. $this->FunctionList = $FunctionList;
  82. $this->IsValid = true;
  83. return true;
  84. }
  85. /*!
  86. Check if a parameter for a function is an array
  87. \param functionName function name
  88. \param parameterName parameter name
  89. \return true if parameter is supposed to be array
  90. */
  91. function isParameterArray( $functionName, $parameterName )
  92. {
  93. if ( isset( $this->FunctionList[$functionName]['parameters'] ) )
  94. {
  95. $parameterList = $this->FunctionList[$functionName]['parameters'];
  96. foreach ( $parameterList as $parameter )
  97. {
  98. if ( $parameter['name'] == $parameterName )
  99. {
  100. if ( $parameter['type'] == 'array' )
  101. {
  102. return true;
  103. }
  104. return false;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. /*!
  111. Pre execute, used by template compilation to check as much as possible before runtime.
  112. \param functionName function name
  113. \return function definition, false if fails.
  114. */
  115. function preExecute( $functionName )
  116. {
  117. /* Code copied from this->execute() */
  118. $moduleName = $this->ModuleName;
  119. if ( !isset( $this->FunctionList[$functionName] ) )
  120. {
  121. eZDebug::writeError( "No such function '$functionName' in module '$moduleName'", __METHOD__ );
  122. return false;
  123. }
  124. $functionDefinition = $this->FunctionList[$functionName];
  125. if ( !isset( $functionName['call_method'] ) )
  126. {
  127. eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  128. return false;
  129. }
  130. if ( !isset( $functionName['parameters'] ) )
  131. {
  132. eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  133. return false;
  134. }
  135. $callMethod = $functionDefinition['call_method'];
  136. if ( isset( $callMethod['class'] ) &&
  137. isset( $callMethod['method'] ) )
  138. {
  139. if ( !class_exists( $callMethod['class'] ) )
  140. {
  141. return false;
  142. }
  143. $classObject = $this->objectForClass( $callMethod['class'] );
  144. if ( $classObject === null )
  145. {
  146. return false;
  147. }
  148. if ( !method_exists( $classObject, $callMethod['method'] ) )
  149. {
  150. return false;
  151. }
  152. return $functionDefinition;
  153. }
  154. return false;
  155. }
  156. function execute( $functionName, $functionParameters )
  157. {
  158. $moduleName = $this->ModuleName;
  159. if ( !isset( $this->FunctionList[$functionName] ) )
  160. {
  161. eZDebug::writeError( "No such function '$functionName' in module '$moduleName'", __METHOD__ );
  162. return null;
  163. }
  164. $functionDefinition = $this->FunctionList[$functionName];
  165. if ( !isset( $functionName['call_method'] ) )
  166. {
  167. eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  168. return null;
  169. }
  170. if ( !isset( $functionName['parameters'] ) )
  171. {
  172. eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'", __METHOD__ );
  173. return null;
  174. }
  175. $callMethod = $functionDefinition['call_method'];
  176. if ( isset( $callMethod['class'] ) &&
  177. isset( $callMethod['method'] ) )
  178. {
  179. $resultArray = $this->executeClassMethod( $callMethod['class'], $callMethod['method'],
  180. $functionDefinition['parameters'], $functionParameters );
  181. }
  182. else
  183. {
  184. eZDebug::writeError( "No valid call methods found for function '$functionName' in module '$moduleName'", __METHOD__ );
  185. return null;
  186. }
  187. if ( !is_array( $resultArray ) )
  188. {
  189. eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result array", __METHOD__ );
  190. return null;
  191. }
  192. if ( isset( $resultArray['internal_error'] ) )
  193. {
  194. switch ( $resultArray['internal_error'] )
  195. {
  196. case eZModuleFunctionInfo::ERROR_NO_CLASS:
  197. {
  198. $className = $resultArray['internal_error_class_name'];
  199. eZDebug::writeError( "No class '$className' available for function '$functionName' in module '$moduleName'", __METHOD__ );
  200. return null;
  201. } break;
  202. case eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD:
  203. {
  204. $className = $resultArray['internal_error_class_name'];
  205. $classMethodName = $resultArray['internal_error_class_method_name'];
  206. eZDebug::writeError( "No method '$classMethodName' in class '$className' available for function '$functionName' in module '$moduleName'", __METHOD__ );
  207. return null;
  208. } break;
  209. case eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED:
  210. {
  211. $className = $resultArray['internal_error_class_name'];
  212. eZDebug::writeError( "Failed instantiating class '$className' which is needed for function '$functionName' in module '$moduleName'", __METHOD__ );
  213. return null;
  214. } break;
  215. case eZModuleFunctionInfo::ERROR_MISSING_PARAMETER:
  216. {
  217. $parameterName = $resultArray['internal_error_parameter_name'];
  218. eZDebug::writeError( "Missing parameter '$parameterName' for function '$functionName' in module '$moduleName'",
  219. __METHOD__ . " $moduleName::$functionName" );
  220. return null;
  221. } break;
  222. default:
  223. {
  224. $internalError = $resultArray['internal_error'];
  225. eZDebug::writeError( "Unknown internal error '$internalError' for function '$functionName' in module '$moduleName'", __METHOD__ );
  226. return null;
  227. } break;
  228. }
  229. return null;
  230. }
  231. else if ( isset( $resultArray['error'] ) )
  232. {
  233. }
  234. else if ( isset( $resultArray['result'] ) )
  235. {
  236. return $resultArray['result'];
  237. }
  238. else
  239. {
  240. eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result value", __METHOD__ );
  241. }
  242. return null;
  243. }
  244. function objectForClass( $className )
  245. {
  246. if ( !isset( $GLOBALS['eZModuleFunctionClassObjectList'] ) )
  247. {
  248. $GLOBALS['eZModuleFunctionClassObjectList'] = array();
  249. }
  250. if ( isset( $GLOBALS['eZModuleFunctionClassObjectList'][$className] ) )
  251. {
  252. return $GLOBALS['eZModuleFunctionClassObjectList'][$className];
  253. }
  254. return $GLOBALS['eZModuleFunctionClassObjectList'][$className] = new $className();
  255. }
  256. function executeClassMethod( $className, $methodName,
  257. $functionParameterDefinitions, $functionParameters )
  258. {
  259. if ( !class_exists( $className ) )
  260. {
  261. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS,
  262. 'internal_error_class_name' => $className );
  263. }
  264. $classObject = $this->objectForClass( $className );
  265. if ( $classObject === null )
  266. {
  267. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED,
  268. 'internal_error_class_name' => $className );
  269. }
  270. if ( !method_exists( $classObject, $methodName ) )
  271. {
  272. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD,
  273. 'internal_error_class_name' => $className,
  274. 'internal_error_class_method_name' => $methodName );
  275. }
  276. $parameterArray = array();
  277. foreach ( $functionParameterDefinitions as $functionParameterDefinition )
  278. {
  279. $parameterName = $functionParameterDefinition['name'];
  280. if ( isset( $functionParameters[$parameterName] ) )
  281. {
  282. // Do type checking
  283. $parameterArray[] = $functionParameters[$parameterName];
  284. }
  285. else
  286. {
  287. if ( $functionParameterDefinition['required'] )
  288. {
  289. return array( 'internal_error' => eZModuleFunctionInfo::ERROR_MISSING_PARAMETER,
  290. 'internal_error_parameter_name' => $parameterName );
  291. }
  292. else if ( isset( $functionParameterDefinition['default'] ) )
  293. {
  294. $parameterArray[] = $functionParameterDefinition['default'];
  295. }
  296. else
  297. {
  298. $parameterArray[] = null;
  299. }
  300. }
  301. }
  302. return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
  303. }
  304. /*!
  305. \deprecated use call_user_func_array() instead
  306. */
  307. function callClassMethod( $methodName, $classObject, $parameterArray )
  308. {
  309. /* echo "*********** fetching START **************** <br>";
  310. var_dump( $methodName );
  311. var_dump( $classObject );
  312. var_dump( $parameterArray );
  313. echo "*********** fetching END ******************<br><br><br>";*/
  314. return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
  315. }
  316. /// \privatesection
  317. public $ModuleName;
  318. public $FunctionList;
  319. public $IsValid;
  320. }
  321. ?>