Heim > Backend-Entwicklung > PHP-Tutorial > 这是我的MVC框架ActionController的封装

这是我的MVC框架ActionController的封装

WBOY
Freigeben: 2016-07-25 09:10:06
Original
1049 Leute haben es durchsucht
这是我的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 ob_start();
  66. $this->params = & $GLOBALS['DATA_CACHE']['request'];
  67. $this->viewPath = trim(
  68. $this->params['module'] . '/' . $this->params['controller'], '/');
  69. if ($this->application() !== self::NO_RENDER)
  70. $this->action($this->params['action']);
  71. }
  72. public function __destruct() {
  73. if (!defined(self::IS_RENDER) && self::$_currentView != null) {
  74. switch ($this->defaultRender) {
  75. case self::VIEW :
  76. case self::TEXT :
  77. case self::ACTION :
  78. case self::WIDGET :
  79. #$this->defaultRender = $mode;
  80. break;
  81. default :
  82. $this->defaultRender = self::VIEW;
  83. }
  84. $this->render(
  85. $this->defaultRender,
  86. self::$_currentView
  87. );
  88. }
  89. if (self::$_instance != null)
  90. self::$_instance = null;
  91. if (self::$_request != null)
  92. self::$_request = null;
  93. }
  94. protected function action($action) {
  95. $name = MST_String::camelize($action);
  96. $actName = $name . self::PF_ACTION;
  97. if (!method_exists($this, $actName))
  98. MST_Core::error(203, $actName);
  99. $actRef = new ReflectionMethod($this, $actName);
  100. if ($actRef->isPrivate() || $actRef->isProtected()
  101. && !constant(MST_ActionController_Router::IS_MAP))
  102. MST_Core::error(203, $actName);
  103. if ($this->$actName() !== self::NO_RENDER && self::$_currentView == null)
  104. self::$_currentView = $action;
  105. return $this;
  106. }
  107. /**
  108. * 输出,url跳转
  109. */
  110. protected function redirect($url) {
  111. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  112. define(self::IS_RENDER, true);
  113. header('Location:'.linkUri($url));
  114. return $this;
  115. }
  116. // render XML
  117. // render JAVASCRIPT
  118. protected function render(
  119. $mode = null,
  120. $content = null,
  121. array $options = null)
  122. {
  123. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  124. define(self::IS_RENDER, true);
  125. if ($mode == null) $mode = $this->defaultRender;
  126. if ($mode == self::VIEW)
  127. $content = $this->viewPath . '/' . $content;
  128. MST_ActionView::instance()
  129. ->assign($this)
  130. ->setOptions($options)
  131. ->render($mode, $content);
  132. return $this;
  133. }
  134. protected function customRender($file, $path, array $options = null) {
  135. return $this->render(self::CUSTOM_VIEW, array($file, $path), $options);
  136. }
  137. protected function setView($val) {
  138. self::$_currentView = $val;
  139. }
  140. protected function setViewOption($key, $val) {
  141. MST_ActionView::instance()->setOption($key, $val);
  142. return $this;
  143. }
  144. protected function getViewOption($key) {
  145. return MST_ActionView::instance()->getOption($key);
  146. }
  147. protected function setViewOptions(array $options) {
  148. MST_ActionView::instance()->setOptions($options);
  149. return $this;
  150. }
  151. protected function getViewOptions() {
  152. return MST_ActionView::instance()->getOptions();
  153. }
  154. protected function doComet(Closure $fn) {
  155. $times = 0;
  156. set_time_limit(0);
  157. while(true) {
  158. ob_flush();
  159. flush();
  160. $times++;
  161. $result = call_user_func($fn, $times, $this);
  162. if ($result === false) {
  163. break;
  164. }
  165. usleep(10000);
  166. sleep($this->comet);
  167. }
  168. }
  169. }
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage