首頁 >後端開發 >php教程 >這是我的MVC框架ActionController的封裝

這是我的MVC框架ActionController的封裝

WBOY
WBOY原創
2016-07-25 09:10:061036瀏覽
這是我的 MVC 框架 ActionController 的封裝
  1. /*
  2. * MST_Library v3.1
  3. * @autohr Janpoem
  4. */
  5. (!define('IN_MST_CORE'))
  6. exit('MST_ActionController 不能包含單一!');
  7. if (!defined(MST_Core::LITE_MODE)) {
  8. MST_Core: :import(Core::LITE_MODE)) {
  9. MST_Corearray:import(Corearray (
  10. 'MST/ActionController/Request',
  11. 'MST/ActionController/Router',
  12. ), MST_Core::P_LIB);
  13. }
  14. 抽象類別MST_ActionController {}
  15. 抽象類別MST_ActionController {
  16. const
  17. NO_RENDER = false,
  18. IS_RENDER = 'CONTROLLER_IS_RENDER',
  19. PF_CONTROLLER = '控制器',
  20. PF_ACTION = '操作🎜> VIEW = '視圖',
  21. TEXT = '文本',
  22. ACTION = '操作',
  23. WIDGET = '小部件',
  24. CUSTOM_VIEW = '自訂視圖';
  25. private static
  26. $_request = null,
  27. $_instance = null,
  28. $_currentView = null;
  29. # @todo 此處的處理應該放在controller被實例化以後,dispatch 應該有一個具體的意義
  30. 最終靜態公共函數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. } ']['request'] = & $request;
  46. if (!class_exists($controller))
  47. MST_Core::error(202, $controller);
  48. else
  49. self::$_instance = new $controller();
  50. }
  51. }
  52. public
  53. $layout = false,
  54. $format = 'html',
  55. $params = null,
  56. $format = 'html',
  57. $params = null,
  58. $format = 'html',
  59. $params = null,
  60. $format = 'html',
  61. $params = null,
  62. $format = 'html',
  63. $params = null,
  64. $format = 'html',
  65. $params = null,
  66. $format = 'html',
  67. $params = null,
  68. $autoLoadHelper = false;
  69. protected
  70. $comet = 0,
  71. $viewPath = null,
  72. $defaultRender = self::VIEW;
  73. 抽象公共函數應用();
  74. 私有函數__construct()
  75. {
  76. if ($this->comet ob_start();
  77. $this->params = & $GLOBALS['DATA_CACHE']['request'];
  78. $this->viewPath = trim(
  79. $this->params['module'] . '/' . $this-> ;params[' controller'], '/');
  80. if ($this->application() !== self::NO_RENDER)
  81. $this->action($this->params ['action']);
  82. }
  83. public function __destruct() {
  84. if (!define(self::IS_RENDER) && self::$_currentView != null) {
  85. switch ($this->defaultRender ) {
  86. case self::VIEW :
  87. case self::TEXT :
  88. case self::ACTION :
  89. case self::WIDGET :
  90. # $this->defaultRender = $modeaultRender = $mode ;
  91. break;
  92. 預設:
  93. $this->defaultRender = self::VIEW;
  94. }
  95. $this->render(
  96. $this->defaultRender,
  97. self::$_currentView
  98. );
  99. }
  100. if (self::$_instance != null)
  101. self::$_instance = null ;
  102. if (self::$_request ! = null)
  103. self::$_request = null;
  104. }
  105. protected function action($action) {
  106. $name = MST_String::camelize($action);
  107. $actName = $name 。 self::PF_ACTION;
  108. if (!method_exists($this, $actName))
  109. MST_Core::error(203, $actName);
  110. $actRef = new ReflectionMethod($this, $actName);
  111. if ($actRef->isPrivate() || $actRef->isProtected()
  112. && !constant(MST_ActionController_Router::IS_MAP))
  113. MST_Core::error(203, $actName) if ($this->$actName() !== self::NO_RENDER && self::$_currentView == null)
  114. self::$_currentView = $action;
  115. return $this;
  116. }
  117. /**
  118. * 輸出,url跳轉
  119. */
  120. protected 函數重定向($url) {
  121. if (define(self::IS_RENDER)) return self::NO_RENDER;
  122. Define (self::IS_RENDER, true);
  123. header('Location:'.linkUri($url));
  124. return $this;
  125. }
  126. // 渲染XML
  127. // 渲染JAVASCRIPT
  128. protected 函數render(
  129. $mode = null,
  130. $content = null,
  131. array $options = null)
  132. {
  133. if (define(self) : :IS_RENDER)) return self::NO_RENDER; Define(self::IS_RENDER, true); if ($mode == null) $mode = $this->defaultRender; if ( $momode == self::VIEW) $content = $this->viewPath 。 '/' 。 $content; MST_ActionView::instance() ->分配($this) ->setOptions($options) ->render($mode, $content); 回傳$this; }
  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. lush(true);
  160. ob_flush();
  161. lush( ); $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. }
}
複製程式碼
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn