Controller.php 6.8 KB

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