当前位置:Gxlcms > PHP教程 > 这是我的MVC框架ActionController的封装

这是我的MVC框架ActionController的封装

时间:2021-07-01 10:21:17 帮助过:24人阅读

这是我的MVC框架ActionController的封装
  1. /*
  2. * MST_Library v3.1
  3. * @autohr Janpoem
  4. */
  5. if (!defined('IN_MST_CORE'))
  6. exit('MST_ActionController Can\'t be include single!');
  7. if (!defined(MST_Core::LITE_MODE)) {
  8. MST_Core::import(array(
  9. 'MST/ActionController/Request',
  10. 'MST/ActionController/Router',
  11. ), MST_Core::P_LIB);
  12. }
  13. abstract class MST_ActionController {
  14. const
  15. NO_RENDER = false,
  16. IS_RENDER = 'CONTROLLER_IS_RENDER',
  17. PF_CONTROLLER = 'Controller',
  18. PF_ACTION = 'Action',
  19. FILE = 'file',
  20. VIEW = 'view',
  21. TEXT = 'text',
  22. ACTION = 'action',
  23. WIDGET = 'widget',
  24. CUSTOM_VIEW = 'custom_view';
  25. private static
  26. $_request = null,
  27. $_instance = null,
  28. $_currentView = null;
  29. # @todo 此处的处理应该放到controller被实例化以后, dispatch应该有一个具体含义
  30. final static public function dispatch(array $config = null, $beforeDispatch = null) {
  31. if (self::$_instance == null) {
  32. $request = new MST_ActionController_Request($config['request']);
  33. $router = new MST_ActionController_Router($config['routes']);
  34. $router->routing($request);
  35. $controller = $request['controller'];
  36. $controller = MST_String::camelize2($controller) . static::PF_CONTROLLER;
  37. if ($request['module'] != null) {
  38. $module = $request['module'];
  39. if (strpos($module, '/') !== false)
  40. $module = str_replace('/', '_', $module);
  41. $controller = $module . '_' . $controller;
  42. }
  43. if (is_callable($beforeDispatch)) {
  44. call_user_func_array($beforeDispatch, array($request, & $controller));
  45. }
  46. $GLOBALS['DATA_CACHE']['request'] = & $request;
  47. if (!class_exists($controller))
  48. MST_Core::error(202, $controller);
  49. else
  50. self::$_instance = new $controller();
  51. }
  52. }
  53. public
  54. $layout = false,
  55. $format = 'html',
  56. $params = null,
  57. $autoLoadHelper = false;
  58. protected
  59. $comet = 0,
  60. $viewPath = null,
  61. $defaultRender = self::VIEW;
  62. abstract public function application();
  63. private function __construct()
  64. {
  65. if ($this->comet <= 0)
  66. ob_start();
  67. $this->params = & $GLOBALS['DATA_CACHE']['request'];
  68. $this->viewPath = trim(
  69. $this->params['module'] . '/' . $this->params['controller'], '/');
  70. if ($this->application() !== self::NO_RENDER)
  71. $this->action($this->params['action']);
  72. }
  73. public function __destruct() {
  74. if (!defined(self::IS_RENDER) && self::$_currentView != null) {
  75. switch ($this->defaultRender) {
  76. case self::VIEW :
  77. case self::TEXT :
  78. case self::ACTION :
  79. case self::WIDGET :
  80. #$this->defaultRender = $mode;
  81. break;
  82. default :
  83. $this->defaultRender = self::VIEW;
  84. }
  85. $this->render(
  86. $this->defaultRender,
  87. self::$_currentView
  88. );
  89. }
  90. if (self::$_instance != null)
  91. self::$_instance = null;
  92. if (self::$_request != null)
  93. self::$_request = null;
  94. }
  95. protected function action($action) {
  96. $name = MST_String::camelize($action);
  97. $actName = $name . self::PF_ACTION;
  98. if (!method_exists($this, $actName))
  99. MST_Core::error(203, $actName);
  100. $actRef = new ReflectionMethod($this, $actName);
  101. if ($actRef->isPrivate() || $actRef->isProtected()
  102. && !constant(MST_ActionController_Router::IS_MAP))
  103. MST_Core::error(203, $actName);
  104. if ($this->$actName() !== self::NO_RENDER && self::$_currentView == null)
  105. self::$_currentView = $action;
  106. return $this;
  107. }
  108. /**
  109. * 输出,url跳转
  110. */
  111. protected function redirect($url) {
  112. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  113. define(self::IS_RENDER, true);
  114. header('Location:'.linkUri($url));
  115. return $this;
  116. }
  117. // render XML
  118. // render JAVASCRIPT
  119. protected function render(
  120. $mode = null,
  121. $content = null,
  122. array $options = null)
  123. {
  124. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  125. define(self::IS_RENDER, true);
  126. if ($mode == null) $mode = $this->defaultRender;
  127. if ($mode == self::VIEW)
  128. $content = $this->viewPath . '/' . $content;
  129. MST_ActionView::instance()
  130. ->assign($this)
  131. ->setOptions($options)
  132. ->render($mode, $content);
  133. return $this;
  134. }
  135. protected function customRender($file, $path, array $options = null) {
  136. return $this->render(self::CUSTOM_VIEW, array($file, $path), $options);
  137. }
  138. protected function setView($val) {
  139. self::$_currentView = $val;
  140. }
  141. protected function setViewOption($key, $val) {
  142. MST_ActionView::instance()->setOption($key, $val);
  143. return $this;
  144. }
  145. protected function getViewOption($key) {
  146. return MST_ActionView::instance()->getOption($key);
  147. }
  148. protected function setViewOptions(array $options) {
  149. MST_ActionView::instance()->setOptions($options);
  150. return $this;
  151. }
  152. protected function getViewOptions() {
  153. return MST_ActionView::instance()->getOptions();
  154. }
  155. protected function doComet(Closure $fn) {
  156. $times = 0;
  157. set_time_limit(0);
  158. while(true) {
  159. ob_flush();
  160. flush();
  161. $times++;
  162. $result = call_user_func($fn, $times, $this);
  163. if ($result === false) {
  164. break;
  165. }
  166. usleep(10000);
  167. sleep($this->comet);
  168. }
  169. }
  170. }

人气教程排行