This is the encapsulation of my MVC framework ActionController

WBOY
Release: 2016-07-25 09:10:06
Original
1010 people have browsed it
This is the encapsulation of my MVC framework 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. VIEW = ' view',
  13. TEXT = 'text',
  14. ACTION = 'action',
  15. WIDGET = 'widget',
  16. CUSTOM_VIEW = 'custom_view';
  17. private static
  18. $_request = null,
  19. $_instance = null,
  20. $_currentView = null;
  21. # @todo The processing here should be done after the controller is instantiated, dispatch should have a specific meaning
  22. final static public function dispatch(array $config = null, $beforeDispatch = null) {
  23. if (self ::$_instance == null) {
  24. $request = new MST_ActionController_Request($config['request']);
  25. $router = new MST_ActionController_Router($config['routes']);
  26. $router->routing($ request);
  27. $controller = $request['controller'];
  28. $controller = MST_String::camelize2($controller) . static::PF_CONTROLLER;
  29. if ($request['module'] != null) {
  30. $ module = $request['module'];
  31. if (strpos($module, '/') !== false)
  32. $module = str_replace('/', '_', $module);
  33. $controller = $ module . '_' . $controller;
  34. }
  35. if (is_callable($beforeDispatch)) {
  36. call_user_func_array($beforeDispatch, array($request, & $controller));
  37. }
  38. $GLOBALS['DATA_CACHE'][' request'] = & $request;
  39. if (!class_exists($controller))
  40. MST_Core::error(202, $controller);
  41. else
  42. self::$_instance = new $controller();
  43. }
  44. }
  45. public
  46. $layout = false,
  47. $format = 'html',
  48. $params = null,
  49. $autoLoadHelper = false;
  50. protected
  51. $comet = 0,
  52. $viewPath = null,
  53. $defaultRender = self:: VIEW;
  54. abstract public function application();
  55. private function __construct()
  56. {
  57. if ($this->comet <= 0)
  58. ob_start();
  59. $this->params = & $GLOBALS ['DATA_CACHE']['request'];
  60. $this->viewPath = trim(
  61. $this->params['module'] . '/' . $this->params['controller'], '/');
  62. if ($this->application() !== self::NO_RENDER)
  63. $this->action($this->params['action']);
  64. }
  65. public function __destruct() {
  66. if (!defined(self::IS_RENDER) && self::$_currentView != null) {
  67. switch ($this->defaultRender) {
  68. case self::VIEW :
  69. case self:: TEXT :
  70. case self::ACTION :
  71. case self::WIDGET :
  72. #$this->defaultRender = $mode;
  73. break;
  74. default :
  75. $this->defaultRender = self::VIEW;
  76. }
  77. $this->render(
  78. $this->defaultRender,
  79. self::$_currentView
  80. );
  81. }
  82. if (self::$_instance != null)
  83. self::$_instance = null;
  84. if ( self::$_request != null)
  85. self::$_request = null;
  86. }
  87. protected function action($action) {
  88. $name = MST_String::camelize($action);
  89. $actName = $name . self::PF_ACTION;
  90. if (!method_exists($this, $actName))
  91. MST_Core::error(203, $actName);
  92. $actRef = new ReflectionMethod($this, $actName);
  93. if ($actRef- >isPrivate() || $actRef->isProtected()
  94. && !constant(MST_ActionController_Router::IS_MAP))
  95. MST_Core::error(203, $actName);
  96. if ($this->$actName() !== self::NO_RENDER && self::$_currentView == null)
  97. self::$_currentView = $action;
  98. return $this;
  99. }
  100. /**
  101. * Output, url jump
  102. */
  103. protected function redirect($ url) {
  104. if (defined(self::IS_RENDER)) return self::NO_RENDER;
  105. define(self::IS_RENDER, true);
  106. header('Location:'.linkUri($url));
  107. return $this ;
  108. }
  109. // render XML
  110. // render JAVASCRIPT
  111. protected function render(
  112. $mode = null,
  113. $content = null,
  114. array $options = null)
  115. {
  116. if (defined(self::IS_RENDER) ) return self::NO_RENDER;
  117. define(self::IS_RENDER, true);
  118. if ($mode == null) $mode = $this->defaultRender;
  119. if ($mode == self::VIEW)
  120. $content = $this->viewPath . '/' . $content;
  121. MST_ActionView::instance()
  122. ->assign($this)
  123. ->setOptions($options)
  124. ->render($mode , $content);
  125. return $this;
  126. }
  127. protected function customRender($file, $path, array $options = null) {
  128. return $this->render(self::CUSTOM_VIEW, array($file, $path), $options);
  129. }
  130. protected function setView($val) {
  131. self::$_currentView = $val;
  132. }
  133. protected function setViewOption($key, $val) {
  134. MST_ActionView::instance()->setOption($key, $val);
  135. return $this;
  136. }
  137. protected function getViewOption($key) {
  138. return MST_ActionView::instance()->getOption($key);
  139. }
  140. protected function setViewOptions(array $options) {
  141. MST_ActionView::instance()->setOptions($options);
  142. return $this;
  143. }
  144. protected function getViewOptions() {
  145. return MST_ActionView::instance()->getOptions();
  146. }
  147. protected function doComet(Closure $fn) {
  148. $times = 0;
  149. set_time_limit(0);
  150. while(true) {
  151. ob_flush();
  152. flush();
  153. $times++;
  154. $result = call_user_func($fn, $times, $this);
  155. if ($result === false) {
  156. break;
  157. }
  158. usleep(10000);
  159. sleep($this->comet);
  160. }
  161. }
  162. }
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!