Source for file ClassMethodsActionSource.class.php

Documentation is available at ClassMethodsActionSource.class.php

  1. <?php
  2.  
  3. require_once HARMONI."actionHandler/ActionSource.abstract.php";
  4.  
  5. /**
  6. * The ClassMethodsActionSource looks for actions as methods located within a class. The action names correspond to a method
  7. * of the same name within a class. The class name is the same as module name.
  8. *
  9. * @package harmoni.actions.sources
  10. *
  11. * @copyright Copyright &copy; 2005, Middlebury College
  12. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  13. *
  14. * @version $Id: ClassMethodsActionSource.class.php,v 1.6 2007/09/04 20:25:29 adamfranco Exp $
  15. */
  16. class ClassMethodsActionSource extends ActionSource{
  17.  
  18. /**
  19. * @var string $_basePath The base path on the filesystem to look for module folders.
  20. * @access private
  21. ***/
  22. var $_basePath;
  23. /**
  24. * @var string $_fileExtension The extension to add onto action names to locate their associated files.
  25. * @access private
  26. ***/
  27. var $_fileExtension;
  28. /**
  29. * Constructor
  30. * @param string $basePath The base path on the filesystem which contains the module class declarations.
  31. * @param string $fileExtension The extension to add onto module names to find the files, such that
  32. * the module "main" might look for a file named "main.class.php" with a file extension of ".class.php".
  33. * @return void
  34. * @access public
  35. */
  36. function ClassMethodsActionSource($basePath, $fileExtension) {
  37. $this->_basePath = ereg_replace(DIRECTORY_SEPARATOR."$", "", $basePath);
  38. $this->_fileExtension = $fileExtension;
  39. }
  40. /**
  41. * Checks to see if a given modules/action pair exists for execution within this source.
  42. * @param string $module
  43. * @param string $action
  44. * @access public
  45. * @return boolean
  46. */
  47. function actionExists($module, $action)
  48. {
  49. $fullPath = $this->_mkFullPath($module, $action);
  50. if (file_exists($fullPath)) {
  51. include_once($fullPath);
  52. if (class_exists($module)) {
  53. $class = @new $module;
  54. if (method_exists($class, $action)) {
  55. unset($class);
  56. return true;
  57. }
  58. unset($class);
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * Makes a full pathname to an action file.
  65. * @access private
  66. * @return string
  67. */
  68. function _mkFullPath($module, $action)
  69. {
  70. return $this->_basePath . DIRECTORY_SEPARATOR . $module . $this->_fileExtension;
  71. }
  72. /**
  73. * Executes the specified action in the specified module, using the Harmoni object as a base.
  74. * @param string $module The module in which to execute.
  75. * @param string $action The specific action to execute.
  76. * @param ref object $harmoni A reference to a {@link Harmoni} object.
  77. * @access public
  78. * @return ref mixed A {@link Layout} or TRUE/FALSE
  79. */
  80. function executeAction($module, $action, $harmoni)
  81. {
  82. $fullPath = $this->_mkFullPath($module, $action);
  83. if (!$this->actionExists($module, $action)) {
  84. throwError( new Error("ClassesActionSource::executeAction($module, $action) - could not proceed because the file to include does not exist!", "ActionHandler", true));
  85. }
  86. include_once($fullPath);
  87. if (!class_exists($module)) {
  88. throwError( new Error("ClassesActionSource::executeAction($module, $action) - could not proceed because the class name '$action' is not defined!","ActionHandler", true));
  89. }
  90. $class = @new $module;
  91. $method = $action;
  92. if (!method_exists($class, $method)) {
  93. throwError( new Error("ClassesActionSource::executeAction($module, $action) - could not proceed because the method '$method()' is not defined in the class '$action'!","ActionHandler", true));
  94. }
  95. $result =$class->$method($harmoni);
  96. return $result;
  97. }
  98. }

Documentation generated on Wed, 19 Sep 2007 10:22:04 -0400 by phpDocumentor 1.3.0RC3