PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Controller/libraries/Atomik/Controller.php

http://atomikframework.googlecode.com/
PHP | 185 lines | 118 code | 15 blank | 52 comment | 8 complexity | d5b47136cf25a9021d372730e7f28962 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, CC-BY-3.0
  1. <?php
  2. /**
  3. * Atomik Framework
  4. * Copyright (c) 2008-2009 Maxime Bouroumeau-Fuseau
  5. *
  6. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  7. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  8. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  9. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  10. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  11. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. * THE SOFTWARE.
  13. *
  14. * @package Atomik
  15. * @subpackage Controller
  16. * @author Maxime Bouroumeau-Fuseau
  17. * @copyright 2008-2009 (c) Maxime Bouroumeau-Fuseau
  18. * @license http://www.opensource.org/licenses/mit-license.php
  19. * @link http://www.atomikframework.com
  20. */
  21. /**
  22. * @package Atomik
  23. * @subpackage Controller
  24. */
  25. class Atomik_Controller
  26. {
  27. /** @var string */
  28. protected $_action;
  29. /** @var array */
  30. protected $_params;
  31. /** @var array */
  32. protected $_data;
  33. /** @var string */
  34. protected $_httpMethod;
  35. /** @var Atomik */
  36. protected $_helpers;
  37. public function __construct()
  38. {
  39. $this->_helpers = Atomik::instance();
  40. $this->init();
  41. }
  42. /**
  43. * Called after __construct()
  44. */
  45. protected function init() {}
  46. /**
  47. * Called before an action
  48. */
  49. protected function preDispatch() {}
  50. /**
  51. * Called after an action
  52. */
  53. protected function postDispatch() {}
  54. /**
  55. * Dispatches a request to a controller action
  56. *
  57. * @param string $action
  58. * @param string $httpMethod
  59. * @param array $vars
  60. */
  61. public function _dispatch($action, $httpMethod, $vars = array())
  62. {
  63. $this->_action = $action;
  64. $this->_data = $_POST;
  65. $this->_params = array_merge(Atomik::get('request'), $vars);
  66. $this->_httpMethod = $httpMethod;
  67. $args = array();
  68. $methodName = str_replace(' ', '', ucwords(str_replace('_', ' ', $action)));
  69. $methodName{0} = strtolower($methodName{0});
  70. try {
  71. $method = new ReflectionMethod($this, $methodName);
  72. if (!$method->isPublic()) {
  73. if (ControllerPlugin::$config['controller_must_exists']) {
  74. throw new Atomik_HttpException("'" . get_class($this) . "::$methodName()' is not a public method", 404);
  75. }
  76. return false;
  77. }
  78. // building method parameters using request params
  79. foreach ($method->getParameters() as $param) {
  80. if (array_key_exists($param->getName(), $this->_params)) {
  81. $args[] = $this->_params[$param->getName()];
  82. } else if (!$param->isOptional()) {
  83. throw new Atomik_Exception("Missing parameter '" . $param->getName() . "' in '" .
  84. get_class($this) . "::$methodName()'");
  85. } else {
  86. $args[] = $param->getDefaultValue();
  87. }
  88. }
  89. } catch (ReflectionException $e) {
  90. // do not stop if __call() exist, so it allows us to trap method calls
  91. if (!method_exists($this, '__call')) {
  92. if (ControllerPlugin::$config['controller_must_exists']) {
  93. throw new Atomik_HttpException("Method '" . get_class($this) . "::$methodName()' not found", 404);
  94. }
  95. return false;
  96. }
  97. }
  98. $this->preDispatch();
  99. call_user_func_array(array($this, $methodName), $args);
  100. $this->postDispatch();
  101. }
  102. /**
  103. * Forward the current action to another action from the same controller
  104. *
  105. * @param string $action
  106. * @param array $params
  107. */
  108. protected function _forward($action, $params = array())
  109. {
  110. $className = ltrim(substr(get_class($this), 0, -10), '\\');
  111. $className = substr($className, strlen(ltrim(ControllerPlugin::$config['default_namespace'], '\\')));
  112. $view = trim(str_replace('\\', '/', strtolower($className)) . '/' . $action, '/');
  113. $this->_setView($view);
  114. return $this->_dispatch(basename($action), $this->_httpMethod, $params);
  115. }
  116. // ------------------------------------------------------------------------------
  117. // Shortcut methods
  118. protected function _setView($view)
  119. {
  120. Atomik::setView($view);
  121. }
  122. protected function _trigger404($message = 'Not found')
  123. {
  124. Atomik::trigger404($message);
  125. }
  126. protected function _noRender()
  127. {
  128. Atomik::noRender();
  129. }
  130. protected function _hasParam($name)
  131. {
  132. return Atomik::has($name, $this->_params);
  133. }
  134. protected function _getParam($name, $default = null)
  135. {
  136. return Atomik::get($name, $default, $this->_params);
  137. }
  138. protected function _setLayout($layout)
  139. {
  140. Atomik::set('app/layout', $layout);
  141. }
  142. protected function _redirect($url, $useUrl = true, $httpCode = 302)
  143. {
  144. Atomik::redirect($url, $useUrl, $httpCode);
  145. }
  146. protected function _flash($message, $label = 'default')
  147. {
  148. Atomik::flash($message, $label);
  149. }
  150. protected function _isPost()
  151. {
  152. return Atomik::get('app/http_method') == 'POST';
  153. }
  154. protected function _setHeader($name, $value)
  155. {
  156. header("$name: $value");
  157. }
  158. }