Controller.php 7.1 KB

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