This is the package of my MVC framework Actioncontroller
-
- /*
- * Mst_library v3.1
- * @autohr Janpoem
- */
- if (!defined (' In_mst_core '))
- Exit (' Mst_actioncontroller can\ ' t be include single! ');
- if (!defined (Mst_core::lite_mode)) {
- Mst_core::import (Array (
- ' Mst/actioncontroller/request ',
- ' Mst/actioncontroller/router ',
- ), Mst_core::P _lib);
- }
- Abstract class Mst_actioncontroller {
- Const
- No_render= false,
- Is_render= ' Controller_is_render ',
- Pf_controller= ' CONTROLLER ',
- Pf_action= ' ACTION ',
- file= ' file ',
- view= ' View ',
- text= ' text ',
- Action= ' action ',
- widget= ' Widgets ',
- custom_view= ' Custom_view ';
- private static
- $_request = NULL,
- $_instance = NULL,
- $_currentview = null;
- # @todo The processing here should be placed after the controller is instantiated, dispatch should have a specific meaning
- Final static public Function dispatch (array $config = NULL, $beforeDispatch = null) {
- if (self::$_instance = = null) {
- $request = new Mst_actioncontroller_request ($config [' request ']);
- $router = new Mst_actioncontroller_router ($config [' routes ']);
- $router->routing ($request);
- $controller = $request [' Controller '];
- $controller = Mst_string::camelize2 ($controller). Static::P F_controller;
- if ($request [' module ']! = null) {
- $module = $request [' module '];
- if (Strpos ($module, '/')!== false)
- $module = Str_replace ('/', ' _ ', $module);
- $controller = $module. '_' . $controller;
- }
- if (is_callable ($beforeDispatch)) {
- Call_user_func_array ($beforeDispatch, Array ($request, & $controller));
- }
- $GLOBALS [' Data_cache '] [' request '] = & $request;
- if (!class_exists ($controller))
- Mst_core::error (202, $controller);
- Else
- Self::$_instance = new $controller ();
- }
- }
- Public
- $layout = False,
- $format = ' html ',
- $params = NULL,
- $autoLoadHelper = false;
- Protected
- $comet = 0,
- $viewPath = NULL,
- $defaultRender = Self::view;
- Abstract public Function application ();
- Private Function __construct ()
- {
- if ($this->comet <= 0)
- Ob_start ();
- $this->params = & $GLOBALS [' Data_cache '] [' request '];
- $this->viewpath = Trim (
- $this->params[' module '). '/' . $this->params[' controller '], '/');
- if ($this->application ()!== Self::no_render)
- $this->action ($this->params[' action ');
- }
- Public Function __destruct () {
- if (!defined (self::is_render) && Self::$_currentview! = null) {
- Switch ($this->defaultrender) {
- Case Self::view:
- Case Self::text:
- Case Self::action:
- Case Self::widget:
- # $this->defaultrender = $mode;
- Break
- Default:
- $this->defaultrender = Self::view;
- }
- $this->render (
- $this->defaultrender,
- Self::$_currentview
- );
- }
- if (self::$_instance! = null)
- Self::$_instance = null;
- if (self::$_request! = null)
- Self::$_request = null;
- }
- protected function Action ($action) {
- $name = Mst_string::camelize ($action);
- $actName = $name. Self::P f_action;
- if (!method_exists ($this, $actName))
- Mst_core::error (203, $actName);
- $actRef = new Reflectionmethod ($this, $actName);
- if ($actRef->isprivate () | | $actRef->isprotected ()
- &&!constant (Mst_actioncontroller_router::is_map))
- Mst_core::error (203, $actName);
- if ($this, $actName ()!== self::no_render && Self::$_currentview = = null)
- Self::$_currentview = $action;
- return $this;
- }
- /**
- * Output, URL jump
- */
- protected function Redirect ($url) {
- if (defined (Self::is_render)) return self::no_render;
- Define (Self::is_render, true);
- Header (' Location: '. Linkuri ($url));
- return $this;
- }
- Render XML
- Render JAVASCRIPT
- protected function Render (
- $mode = NULL,
- $content = NULL,
- Array $options = null)
- {
- if (defined (Self::is_render)) return self::no_render;
- Define (Self::is_render, true);
- if ($mode = = null) $mode = $this->defaultrender;
- if ($mode = = Self::view)
- $content = $this->viewpath. '/' . $content;
- Mst_actionview::instance ()
- ->assign ($this)
- ->setoptions ($options)
- ->render ($mode, $content);
- return $this;
- }
- protected function Customrender ($file, $path, array $options = null) {
- return $this->render (Self::custom_view, Array ($file, $path), $options);
- }
- protected function Setview ($val) {
- Self::$_currentview = $val;
- }
- protected function Setviewoption ($key, $val) {
- Mst_actionview::instance ()->setoption ($key, $val);
- return $this;
- }
- protected function Getviewoption ($key) {
- Return Mst_actionview::instance ()->getoption ($key);
- }
- protected function setviewoptions (array $options) {
- Mst_actionview::instance ()->setoptions ($options);
- return $this;
- }
- protected function getviewoptions () {
- Return Mst_actionview::instance ()->getoptions ();
- }
- protected function Docomet (Closure $fn) {
- $times = 0;
- Set_time_limit (0);
- while (true) {
- Ob_flush ();
- Flush ();
- $times + +;
- $result = Call_user_func ($FN, $times, $this);
- if ($result = = = False) {
- Break
- }
- Usleep (10000);
- Sleep ($this->comet);
- }
- }
- }
Copy Code |