Base.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace Qii\Controller;
  3. /**
  4. * Qii_Controller_Abstract class
  5. * @author Zhu Jinhui
  6. */
  7. abstract class Base
  8. {
  9. /**
  10. * @var array $actions action对应的方法列表,设置了的就转发
  11. */
  12. public $actions = array();
  13. /**
  14. * @var \Qii\Autoloader\Psr4::getInstance() $load
  15. */
  16. public $load;
  17. /**
  18. * @var \Qii\Language\Loader $language
  19. */
  20. public $language;
  21. /**
  22. * @var Qii\Controller\Base $controller
  23. */
  24. public $controller;
  25. /**
  26. * @var string $controllerId controller名
  27. */
  28. public $controllerId = 'index';
  29. /**
  30. * @var string $actionId action名
  31. */
  32. public $actionId = 'index';
  33. /**
  34. * @var Qii\Request\Base $request
  35. */
  36. public $request;
  37. /**
  38. * @var Qii_Response_Abstract $response
  39. */
  40. public $response;
  41. /**
  42. * @var Qii_Driver_xxx_Connection
  43. */
  44. public $db;
  45. /**
  46. * @var mixed
  47. */
  48. public $view;
  49. /**
  50. * @var Qii_Cache_Abslute $cache
  51. */
  52. public $cache;
  53. /**
  54. * 是否启用view模块
  55. * @var bool
  56. */
  57. public $enableView = false;
  58. /**
  59. * 是否启用Model
  60. * @var bool
  61. */
  62. public $enableModel = false;
  63. public function __construct()
  64. {
  65. $this->load = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
  66. $this->request = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Request\Http');
  67. $this->controllerId = $this->request->controller;
  68. $this->actionId = $this->request->action;
  69. //载入model
  70. if ($this->enableModel) {
  71. $this->enableModel();
  72. }
  73. //载入view
  74. if ($this->enableView) {
  75. $this->view = $this->setView();
  76. }
  77. if (!$this->beforeRun()) {
  78. exit();
  79. }
  80. }
  81. /**
  82. * 启用view后调用初始化View方法
  83. */
  84. protected function initView()
  85. {
  86. }
  87. /**
  88. * 设置view
  89. *
  90. * @param string $engine
  91. * @param array $policy
  92. * @return mixed
  93. */
  94. public function setView($engine = 'smarty', $policy = array())
  95. {
  96. $viewConfigure = \Qii::appConfigure('view');
  97. //如果之前实例化过相同的就不再实例化
  98. if (!$engine) $engine = $viewConfigure['engine'];
  99. $policy = (array)$policy;
  100. if (!$policy) {
  101. $policy = array_merge($policy, $viewConfigure[$engine]);
  102. }
  103. $viewEngine = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\View\Loader');
  104. $viewEngine->setView($engine, $policy);
  105. $this->view = $viewEngine->getView();
  106. if(method_exists($this, 'initView'))
  107. {
  108. $this->initView();
  109. }
  110. return $this->view ;
  111. }
  112. /**
  113. * 设置缓存
  114. *
  115. * @param string $engine 缓存方法
  116. * @param array $policy 缓存策略
  117. */
  118. public function setCache($engine = '', $policy = array())
  119. {
  120. $engine = $engine == '' ? \Qii::appConfigure('cache') : $engine;
  121. $basicPolicy = array(
  122. 'servers' => $this->getCachePolicy($engine),
  123. );
  124. if ($basicPolicy['servers']) {
  125. $policy = array_merge($basicPolicy, $policy);
  126. }
  127. $loader = new \Qii\Cache\Loader($engine);
  128. return $this->cache = $loader->initialization($policy);
  129. }
  130. /**
  131. * 获取缓存的策略
  132. * @param String $cache 缓存的内容
  133. * @return multitype:multitype:Ambigous <>
  134. */
  135. final public function getCachePolicy($cache)
  136. {
  137. $data = array();
  138. if (!$cache) return $data;
  139. $cacheInfo = \Qi\Config\Register::getAppConfigure(\Qi\Config\Register::get(\Qii\Consts\Config::APP_INI_FILE), $cache);
  140. if (!$cacheInfo) return $data;
  141. $servers = explode(";", $cacheInfo['servers']);
  142. $ports = explode(";", $cacheInfo['ports']);
  143. for ($i = 0; $i < count($servers); $i++) {
  144. $data[] = array('host' => $servers[$i], 'port' => $ports[$i]);
  145. }
  146. return $data;
  147. }
  148. final public function enableModel()
  149. {
  150. return $this->db = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Driver\Model');
  151. }
  152. /**
  153. * 获取view
  154. *
  155. * @return mixed
  156. */
  157. public function getView()
  158. {
  159. return $this->view;
  160. }
  161. /**
  162. * 设置request
  163. * @param $request
  164. */
  165. public function setRequest(\Qii\Request\Base $request)
  166. {
  167. $this->request = $request;
  168. }
  169. /**
  170. * 只要继承的方法调用parent::__construct()就开始执行
  171. * 此方法如果返回false,将不再往下继续执行
  172. */
  173. protected function beforeRun()
  174. {
  175. return true;
  176. }
  177. /**
  178. * 执行完dispatch后调用
  179. */
  180. protected function afterRun()
  181. {
  182. }
  183. /**
  184. * 转发
  185. * @param String $controller
  186. * @param String $action
  187. * @return mixed
  188. */
  189. final public function dispatch($controller, $action)
  190. {
  191. $this->request->setControllerName($controller);
  192. $this->request->setActionName($action);
  193. \Qii::getInstance()->dispatcher->setRequest($this->request);
  194. return call_user_func_array(array(\Qii::getInstance()->dispatcher, 'dispatch'), func_get_args());
  195. }
  196. /**
  197. * 获取当前使用的controller
  198. *
  199. * @return string
  200. */
  201. final public function getCurrentController()
  202. {
  203. return get_called_class();
  204. }
  205. /**
  206. * 获取 request 方法
  207. * @return Qii_Request_Http
  208. */
  209. final public function getRequest()
  210. {
  211. return $this->request;
  212. }
  213. /**
  214. * 获取response类
  215. * @return mixed
  216. */
  217. final public function getResponse()
  218. {
  219. return $this->response;
  220. }
  221. /**
  222. * 设置forward
  223. * @param string $controller controller名
  224. * @param string $action action名
  225. */
  226. public function setForward($controller, $action)
  227. {
  228. $this->request->setControllerName($controller);
  229. $this->request->setActionName($action);
  230. $this->request->setForward(true);
  231. }
  232. /**
  233. * afterRun 和 forward 执行
  234. */
  235. public function __destruct()
  236. {
  237. $this->afterRun($this->controller, $this->actionId);
  238. if ($this->request && $this->request->isForward()) {
  239. $this->request->setForward(false);
  240. \Qii::getInstance()->dispatcher->setRequest($this->request);
  241. \Qii::getInstance()->dispatcher->dispatch();
  242. $this->request->setDispatched(true);
  243. }
  244. }
  245. }