Controller.php 6.8 KB

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