Front.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ dV48TyHotkxfH/69Qj9xzd7RdkGKdkqq8gzk841Pho8=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.6
  11. */
  12. /**
  13. * front controller implementation
  14. */
  15. namespace Cube\Controller;
  16. use Cube\Application,
  17. Cube\ModuleManager,
  18. Cube\Controller\Request,
  19. Cube\Controller\Request\AbstractRequest,
  20. Cube\Controller\Plugin\AbstractPlugin,
  21. Cube\Controller\Response\ResponseInterface,
  22. Cube\Http\Response,
  23. Cube\Controller\Router,
  24. Cube\Controller\Dispatcher,
  25. Cube\Controller\Dispatcher\DispatcherInterface,
  26. Cube\Debug,
  27. Cube\Exception\Dispatcher as DispatcherException;
  28. class Front
  29. {
  30. const ERROR_CONTROLLER = 'error';
  31. const ERROR_ACTION = 'not-found';
  32. /**
  33. *
  34. * will hold an array of front controller plugin objects
  35. *
  36. * @var array
  37. */
  38. protected $_plugins;
  39. /**
  40. *
  41. * request object
  42. *
  43. * @var \Cube\Controller\Request\AbstractRequest
  44. */
  45. protected $_request;
  46. /**
  47. *
  48. * response object
  49. *
  50. * @var \Cube\Http\Response
  51. */
  52. protected $_response;
  53. /**
  54. *
  55. * router object
  56. *
  57. * @var \Cube\Controller\Router
  58. */
  59. protected $_router;
  60. /**
  61. *
  62. * dispatcher object
  63. *
  64. * @var \Cube\Controller\Dispatcher
  65. */
  66. protected $_dispatcher;
  67. /**
  68. *
  69. * holds an instance of the object
  70. *
  71. * @var \Cube\Controller\Front
  72. */
  73. protected static $_instance;
  74. /**
  75. *
  76. * application config options
  77. *
  78. * @var array
  79. */
  80. protected $_options;
  81. /**
  82. *
  83. * bootstrap object, in order to be available in the application
  84. *
  85. * @var \Cube\Application\Bootstrap
  86. */
  87. protected $_bootstrap;
  88. /**
  89. *
  90. * class constructor
  91. */
  92. protected function __construct()
  93. {
  94. $this->_plugins = new Plugin\Broker();
  95. }
  96. /**
  97. *
  98. * returns an instance of the object and creates it if it wasnt instantiated yet
  99. *
  100. * @return \Cube\Controller\Front
  101. */
  102. public static function getInstance()
  103. {
  104. if (!self::$_instance instanceof self) {
  105. self::$_instance = new self();
  106. }
  107. return self::$_instance;
  108. }
  109. /**
  110. *
  111. * register plugin
  112. *
  113. * @param \Cube\Controller\Plugin\AbstractPlugin $plugin
  114. *
  115. * @return \Cube\Controller\Front
  116. */
  117. public function registerPlugin(AbstractPlugin $plugin)
  118. {
  119. $this->_plugins->registerPlugin($plugin);
  120. return $this;
  121. }
  122. /**
  123. *
  124. * check if the plugin has been registered
  125. *
  126. * @param string|AbstractPlugin $plugin
  127. *
  128. * @return bool
  129. */
  130. public function isRegisteredPlugin($plugin)
  131. {
  132. return $this->_plugins->isRegisteredPlugin($plugin);
  133. }
  134. /**
  135. *
  136. * get the request object, if not set create a new object of type \Cube\Controller\Request
  137. *
  138. * @return \Cube\Controller\Request\AbstractRequest
  139. */
  140. public function getRequest()
  141. {
  142. if (!$this->_request instanceof AbstractRequest) {
  143. // initialize a routed request
  144. $this->setRequest(
  145. new Request());
  146. }
  147. return $this->_request;
  148. }
  149. /**
  150. *
  151. * set the request object
  152. *
  153. * @param \Cube\Controller\Request\AbstractRequest $request
  154. *
  155. * @return \Cube\Controller\Front
  156. */
  157. public function setRequest(AbstractRequest $request)
  158. {
  159. $this->_request = $request;
  160. return $this;
  161. }
  162. /**
  163. *
  164. * get the response object, if not set create a new object of type \Cube\Controller\Response
  165. *
  166. * @return \Cube\Controller\Response\ResponseInterface
  167. */
  168. public function getResponse()
  169. {
  170. if (!($this->_response instanceof ResponseInterface)) {
  171. $this->setResponse(
  172. new Response());
  173. }
  174. return $this->_response;
  175. }
  176. /**
  177. *
  178. * set response object
  179. *
  180. * @param \Cube\Controller\Response\ResponseInterface $response
  181. *
  182. * @return \Cube\Controller\Front
  183. */
  184. public function setResponse(ResponseInterface $response)
  185. {
  186. $this->_response = $response;
  187. return $this;
  188. }
  189. /**
  190. *
  191. * get router object, if not set create a new object of type \Cube\Controller\Router
  192. *
  193. * @return \Cube\Controller\Router
  194. */
  195. public function getRouter()
  196. {
  197. if (!($this->_router instanceof Router\AbstractRouter)) {
  198. // add routes from module manager singleton
  199. $routes = ModuleManager::getInstance()->getRoutes();
  200. $this->setRouter(
  201. new Router($routes));
  202. }
  203. return $this->_router;
  204. }
  205. /**
  206. *
  207. * set router object
  208. *
  209. * @param \Cube\Controller\Router\AbstractRouter $router
  210. *
  211. * @return \Cube\Controller\Front
  212. */
  213. public function setRouter(Router\AbstractRouter $router)
  214. {
  215. $this->_router = $router;
  216. return $this;
  217. }
  218. /**
  219. *
  220. * get dispatcher object, if not set create a new object of type \Cube\Controller\Dispatcher
  221. *
  222. * @return \Cube\Controller\Dispatcher\DispatcherInterface
  223. */
  224. public function getDispatcher()
  225. {
  226. if (!($this->_dispatcher instanceof DispatcherInterface)) {
  227. $this->setDispatcher(
  228. new Dispatcher());
  229. }
  230. return $this->_dispatcher;
  231. }
  232. /**
  233. *
  234. * set dispatcher object
  235. *
  236. * @param \Cube\Controller\Dispatcher\DispatcherInterface $dispatcher
  237. *
  238. * @return \Cube\Controller\Front
  239. */
  240. public function setDispatcher(DispatcherInterface $dispatcher)
  241. {
  242. $this->_dispatcher = $dispatcher;
  243. return $this;
  244. }
  245. /**
  246. *
  247. * get options array
  248. *
  249. * @return array
  250. */
  251. public function getOptions()
  252. {
  253. return $this->_options;
  254. }
  255. /**
  256. *
  257. * set options array
  258. *
  259. * @param array $options
  260. *
  261. * @return \Cube\Controller\Front
  262. */
  263. public function setOptions(array $options)
  264. {
  265. if (is_array($this->_options)) {
  266. $this->_options = array_merge($this->_options, $options);
  267. }
  268. else {
  269. $this->_options = $options;
  270. }
  271. return $this;
  272. }
  273. /**
  274. *
  275. * get a key from the options array
  276. *
  277. * @param string $key
  278. *
  279. * @return mixed|null
  280. */
  281. public function getOption($key)
  282. {
  283. if (isset($this->_options[$key])) {
  284. return $this->_options[$key];
  285. }
  286. return null;
  287. }
  288. /**
  289. *
  290. * set or unset a key in the options array
  291. *
  292. * @param string $key
  293. * @param mixed|null $value
  294. *
  295. * @return \Cube\Controller\Front
  296. */
  297. public function setOption($key, $value = null)
  298. {
  299. if ($value === null && isset($this->_options[$key])) {
  300. unset($this->_options[$key]);
  301. }
  302. else {
  303. $this->_options[$key] = $value;
  304. }
  305. return $this;
  306. }
  307. /**
  308. *
  309. * get the bootstrap object
  310. *
  311. * @return \Cube\Application\Bootstrap
  312. */
  313. public function getBootstrap()
  314. {
  315. return Application::getInstance()->getBootstrap();
  316. }
  317. /**
  318. *
  319. * dispatch request
  320. *
  321. * @param \Cube\Controller\Request\AbstractRequest $request
  322. * @param \Cube\Controller\Response\ResponseInterface $response
  323. */
  324. public function dispatch(AbstractRequest $request = null, ResponseInterface $response = null)
  325. {
  326. if ($request instanceof AbstractRequest) {
  327. $this->setRequest($request);
  328. }
  329. if ($response instanceof ResponseInterface) {
  330. $this->setResponse($response);
  331. }
  332. $request = $this->getRequest();
  333. $response = $this->getResponse();
  334. // set request
  335. $this->_plugins->setRequest($request)
  336. ->setResponse($response);
  337. $router = $this->getRouter();
  338. // run pre route plugins
  339. $this->_plugins->preRoute();
  340. // route request
  341. $this->_plugins->setRequest(
  342. $router->route($request));
  343. // run post route plugins
  344. $this->_plugins->postRoute();
  345. // run pre dispatcher plugins
  346. $this->_plugins->preDispatcher();
  347. do {
  348. // set dispatched flag to true
  349. $request->setDispatched(true);
  350. // run pre dispatch plugins
  351. $this->_plugins->preDispatch();
  352. try {
  353. $response = $this->getDispatcher()->dispatch($request, $response);
  354. } catch (DispatcherException $e) {
  355. $request->setController(self::ERROR_CONTROLLER)
  356. ->setAction(self::ERROR_ACTION);
  357. $request->setDispatched(false);
  358. }
  359. // dispatch request
  360. $this->setResponse(
  361. $response);
  362. // run post dispatch plugins
  363. $this->_plugins->postDispatch();
  364. } while ($request->isDispatched() !== true);
  365. // run post dispatcher plugins
  366. $this->_plugins->postDispatcher();
  367. $this->setRequest($request)
  368. ->setResponse($response);
  369. Debug::setMemoryEnd();
  370. Debug::setTimeEnd();
  371. Debug::setCpuUsageEnd();
  372. $this->getResponse()->send();
  373. }
  374. }