Controller.php 7.1 KB

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