Controller.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * 控制器基类
  4. */
  5. namespace Qii\Base;
  6. use \Qii\Autoloader\Psr4;
  7. use \Qii\Config\Register;
  8. use \Qii\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. $this->language = \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader');
  76. $this->response = \Qii\Autoloader\Factory::getInstance('\Qii\Base\Response');;
  77. //载入model
  78. if ($this->enableDB) {
  79. $this->enableDB();
  80. }
  81. //载入view
  82. if ($this->enableView) {
  83. $this->view = $this->setView();
  84. }
  85. if (!$this->beforeRun()) {
  86. exit();
  87. }
  88. }
  89. /**
  90. * 启用view后调用初始化View方法
  91. */
  92. protected function initView()
  93. {
  94. }
  95. /**
  96. * 设置view
  97. *
  98. * @param string $engine
  99. * @param array $policy
  100. * @return mixed
  101. */
  102. public function setView($engine = 'smarty', $policy = array())
  103. {
  104. $viewConfigure = \Qii::appConfigure('view');
  105. //如果之前实例化过相同的就不再实例化
  106. if (!$engine) $engine = $viewConfigure['engine'];
  107. $policy = (array)$policy;
  108. if (!$policy) {
  109. $policy = array_merge($policy, $viewConfigure[$engine]);
  110. }
  111. $viewEngine = Psr4::getInstance()->loadClass('\Qii\View\Loader');
  112. $viewEngine->setView($engine, $policy);
  113. $this->view = $viewEngine->getView();
  114. if(method_exists($this, 'initView'))
  115. {
  116. $this->initView();
  117. }
  118. return $this->view ;
  119. }
  120. /**
  121. * 设置缓存
  122. *
  123. * @param string $engine 缓存方法
  124. * @param array $policy 缓存策略
  125. */
  126. public function setCache($engine = '', $policy = array())
  127. {
  128. $engine = $engine == '' ? \Qii::appConfigure('cache') : $engine;
  129. $basicPolicy = array(
  130. 'servers' => $this->getCachePolicy($engine),
  131. );
  132. if ($basicPolicy['servers']) {
  133. $policy = array_merge($basicPolicy, $policy);
  134. }
  135. $loader = new \Qii\Cache\Loader($engine);
  136. return $this->cache = $loader->initialization($policy);
  137. }
  138. /**
  139. * 获取缓存的策略
  140. * @param String $cache 缓存的内容
  141. * @return multitype:multitype:Ambigous <>
  142. */
  143. final public function getCachePolicy($cache)
  144. {
  145. $data = array();
  146. if (!$cache) return $data;
  147. $cacheInfo = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE), $cache);
  148. if (!$cacheInfo) return $data;
  149. $servers = explode(";", $cacheInfo['servers']);
  150. $ports = explode(";", $cacheInfo['ports']);
  151. for ($i = 0; $i < count($servers); $i++) {
  152. $data[] = array('host' => $servers[$i], 'port' => $ports[$i]);
  153. }
  154. return $data;
  155. }
  156. /**
  157. * 开启数据库操作
  158. */
  159. final public function enableDB()
  160. {
  161. return $this->db = Psr4::getInstance()->loadClass('\Qii\Driver\Model');
  162. }
  163. /**
  164. * 获取view
  165. *
  166. * @return mixed
  167. */
  168. public function getView()
  169. {
  170. return $this->view;
  171. }
  172. /**
  173. * 设置 response
  174. * @param $request
  175. */
  176. public function setResponse(\Qii\Base\Response $response)
  177. {
  178. return $this->response = $response;
  179. }
  180. /**
  181. * 设置request
  182. * @param $request
  183. */
  184. public function setRequest(\Qii\Base\Request $request)
  185. {
  186. return $this->request = $request;
  187. }
  188. /**
  189. * 只要继承的方法调用parent::__construct()就开始执行
  190. * 此方法如果返回false,将不再往下继续执行
  191. */
  192. protected function beforeRun()
  193. {
  194. return true;
  195. }
  196. /**
  197. * 执行完dispatch后调用
  198. */
  199. protected function afterRun()
  200. {
  201. if(!$this->response || !is_object($this->response))
  202. {
  203. return;
  204. }
  205. if($this->response instanceof \Qii\Base\Response)
  206. {
  207. $this->response->response();
  208. }
  209. }
  210. /**
  211. * 转发
  212. * @param String $controller
  213. * @param String $action
  214. * @return mixed
  215. */
  216. final public function dispatch($controller, $action)
  217. {
  218. $this->request->setControllerName($controller);
  219. $this->request->setActionName($action);
  220. \Qii::getInstance()->dispatcher->setRequest($this->request);
  221. return call_user_func_array(array(\Qii::getInstance()->dispatcher, 'dispatch'), func_get_args());
  222. }
  223. /**
  224. * 获取当前使用的controller
  225. *
  226. * @return string
  227. */
  228. final public function getCurrentController()
  229. {
  230. return get_called_class();
  231. }
  232. /**
  233. * 获取 request 方法
  234. * @return Qii_Request_Http
  235. */
  236. final public function getRequest()
  237. {
  238. return $this->request;
  239. }
  240. /**
  241. * 获取response类
  242. * @return mixed
  243. */
  244. final public function getResponse()
  245. {
  246. return $this->response;
  247. }
  248. /**
  249. * 设置forward
  250. * @param string $controller controller名
  251. * @param string $action action名
  252. */
  253. public function setForward($controller, $action)
  254. {
  255. $this->request->setControllerName($controller);
  256. $this->request->setActionName($action);
  257. $this->request->setForward(true);
  258. }
  259. /**
  260. * afterRun 和 forward 执行
  261. */
  262. public function __destruct()
  263. {
  264. $this->afterRun($this->controllerId, $this->actionId);
  265. if ($this->request && $this->request->isForward()) {
  266. $this->request->setForward(false);
  267. \Qii::getInstance()->dispatcher->setRequest($this->request);
  268. \Qii::getInstance()->dispatcher->dispatch();
  269. $this->request->setDispatched(true);
  270. }
  271. }
  272. }