PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ezcomponents/MvcTools/src/interfaces/controller.php

http://hppg.googlecode.com/
PHP | 180 lines | 64 code | 15 blank | 101 comment | 6 complexity | 3900c801a41d07468a7f27f58b925334 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * File containing the ezcMvcController class
  4. *
  5. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  6. * @license http://ez.no/licenses/new_bsd New BSD License
  7. * @version 1.1.3
  8. * @filesource
  9. * @package MvcTools
  10. */
  11. /**
  12. * Interface defining controller classes.
  13. *
  14. * Controllers process the client's request and returns variables usable by the
  15. * view-manager in an instance of an ezcMvcResult. Controllers should not
  16. * access request variables directly but should use the passed ezcMvcRequest.
  17. * The process is done through the createResult() method, but is not limited to
  18. * use protected nor private methods. The result of running a controller is an
  19. * instance of ezcMvcResult.
  20. *
  21. * @package MvcTools
  22. * @version 1.1.3
  23. * @mainclass
  24. */
  25. abstract class ezcMvcController
  26. {
  27. /**
  28. * Contains the action to run
  29. * @var string
  30. */
  31. protected $action;
  32. /**
  33. * Contains the original request
  34. * @var ezcMvcRequest
  35. */
  36. protected $request;
  37. /**
  38. * Holds the properties of this class.
  39. *
  40. * @var array(string=>mixed)
  41. */
  42. private $properties = array();
  43. /**
  44. * Holds the router associated with the action
  45. *
  46. * @var ezcMvcRouter
  47. */
  48. private $router;
  49. /**
  50. * Creates a new controller object and sets all the request variables as class variables.
  51. *
  52. * @throws ezcMvcControllerException if the action method is empty
  53. * @param string $action
  54. * @param ezcMvcRequest $request
  55. */
  56. public function __construct( $action, ezcMvcRequest $request )
  57. {
  58. if ( ezcBase::inDevMode() && ( !is_string( $action ) || strlen( $action ) == 0 ) )
  59. {
  60. throw new ezcMvcControllerException( "The '" . get_class( $this ) . "' controller requires an action." );
  61. }
  62. $this->action = $action;
  63. $this->setRequestVariables( $request );
  64. }
  65. /**
  66. * Sets the property $name to $value, all properties are readonly though so
  67. * an exception is thrown for every set.
  68. *
  69. * @throws ezcBasePropertyPermissionException if a read-only property is
  70. * tried to be modified.
  71. *
  72. * @param string $name
  73. * @param mixed $value
  74. * @ignore
  75. */
  76. public function __set( $name, $value )
  77. {
  78. throw new ezcBasePropertyPermissionException( $name, ezcBasePropertyPermissionException::READ );
  79. }
  80. /**
  81. * Returns the value of the property $name.
  82. *
  83. * @throws ezcBasePropertyNotFoundException if the property does not exist.
  84. * @param string $name
  85. * @ignore
  86. */
  87. public function __get( $name )
  88. {
  89. if ( isset( $this->properties[$name] ) )
  90. {
  91. return $this->properties[$name];
  92. }
  93. throw new ezcBasePropertyNotFoundException( $name );
  94. }
  95. /**
  96. * Returns true if the property $name is set, otherwise false.
  97. *
  98. * @param string $name
  99. * @return bool
  100. * @ignore
  101. */
  102. public function __isset( $name )
  103. {
  104. return array_key_exists( $name, $this->properties );
  105. }
  106. /**
  107. * Sets the router associated with this request.
  108. *
  109. * @param ezcMvcRouter $router
  110. */
  111. public function setRouter( ezcMvcRouter $router )
  112. {
  113. $this->router = $router;
  114. }
  115. /**
  116. * Returns the router associated with this request.
  117. *
  118. * @return ezcMvcRouter
  119. */
  120. public function getRouter()
  121. {
  122. return $this->router;
  123. }
  124. /**
  125. * Loops over all the variables in the request, and sets them as object properties.
  126. *
  127. * @param ezcMvcRequest $request
  128. */
  129. protected function setRequestVariables( ezcMvcRequest $request )
  130. {
  131. foreach ( $request->variables as $key => $value )
  132. {
  133. $this->properties[$key] = $value;
  134. }
  135. $this->request = $request;
  136. }
  137. /**
  138. * Creates a method name to call from an $action name.
  139. *
  140. * @param string $action
  141. * @return string
  142. */
  143. public static function createActionMethodName( $action )
  144. {
  145. $actionMethod = 'do' . preg_replace( '@[^A-Za-z]@', '', preg_replace( '@[A-Za-z]+@e', 'ucfirst( "\\0" )', $action ) );
  146. return $actionMethod;
  147. }
  148. /**
  149. * Runs the controller to process the query and return variables usable
  150. * to render the view.
  151. *
  152. * @throws ezcMvcActionNotFoundException if the action method could not be found
  153. * @return ezcMvcResult|ezcMvcInternalRedirect
  154. */
  155. public function createResult()
  156. {
  157. $actionMethod = self::createActionMethodName( $this->action );
  158. if ( method_exists( $this, $actionMethod ) )
  159. {
  160. return $this->$actionMethod();
  161. }
  162. throw new ezcMvcActionNotFoundException( $this->action );
  163. }
  164. }
  165. ?>