/Soul/Application.php

https://gitlab.com/hoanghung.dev/aloads · PHP · 386 lines · 207 code · 46 blank · 133 comment · 14 complexity · cc940ee840edc30a0fc81f963e4fec1c MD5 · raw file

  1. <?php
  2. namespace Soul;
  3. use Soul\Mvc\Controller;
  4. use Soul\Mvc\Request;
  5. use Soul\Mvc\Response;
  6. use Soul\Mvc\Router;
  7. use Soul\Mvc\View;
  8. use Soul\Pattern\Singleton;
  9. class Application extends Singleton
  10. {
  11. /**
  12. * @var View $_view
  13. */
  14. protected $_view = null;
  15. /**
  16. * @var Request $request
  17. */
  18. protected $_request = null;
  19. /**
  20. * @var Router $_router ;
  21. */
  22. protected $_router = null;
  23. /**
  24. * @var Response $_response ;
  25. */
  26. protected $_response = null;
  27. /**
  28. * @var string $_moduleName
  29. */
  30. protected $_moduleName = null;
  31. /**
  32. * @var array $_modules
  33. */
  34. protected $_modules = array();
  35. /**
  36. * @var string $_applicationDir
  37. */
  38. protected $_applicationDir = null;
  39. /**
  40. * @var string $_applicationName
  41. */
  42. protected $_applicationName = 'Application';
  43. /**
  44. * @var string Default Action
  45. */
  46. protected $_defaultAction = 'index';
  47. /**
  48. * @var string Default Controllers
  49. */
  50. protected $_defaultController = 'index';
  51. /**
  52. * @var string Module
  53. */
  54. protected $_defaultModule = null;
  55. /**
  56. *
  57. */
  58. public function __construct()
  59. {
  60. }
  61. /**
  62. * @param $name
  63. * @return mixed
  64. */
  65. public function setApplicationName($name)
  66. {
  67. return $this->_applicationName = $name;
  68. }
  69. /**
  70. * @return string
  71. */
  72. public function getApplicationName()
  73. {
  74. return $this->_applicationName;
  75. }
  76. /**
  77. * @param string $dir
  78. * @return $this
  79. */
  80. public function setApplicationDir($dir)
  81. {
  82. $this->_applicationDir = $dir;
  83. return $this;
  84. }
  85. /**
  86. * @return string Application Dir
  87. */
  88. public function getApplicationDir()
  89. {
  90. return $this->_applicationDir;
  91. }
  92. /**
  93. * @param $module
  94. * @return $this
  95. */
  96. public function setModuleName($module)
  97. {
  98. $this->_moduleName = $module;
  99. return $this;
  100. }
  101. /**
  102. * @param $modules
  103. * @return $this
  104. */
  105. public function setModules($modules)
  106. {
  107. $this->_modules = $modules;
  108. return $this;
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function getModules()
  114. {
  115. return $this->_modules;
  116. }
  117. /**
  118. * @param Router $router
  119. * @return $this
  120. */
  121. public function setRouter(Router $router)
  122. {
  123. $this->_router = $router;
  124. return $this;
  125. }
  126. /**
  127. * @return Router
  128. */
  129. public function getRouter()
  130. {
  131. if (null == $this->_router) {
  132. $this->_router = new Router\Rewrite();
  133. }
  134. return $this->_router;
  135. }
  136. /**
  137. * @param Request $request
  138. * @return $this
  139. */
  140. public function setRequest(Request $request)
  141. {
  142. $this->_request = $request;
  143. return $this;
  144. }
  145. /**
  146. * @return Request
  147. */
  148. public function getRequest()
  149. {
  150. if (null == $this->_request) {
  151. $this->_request = new Request();
  152. }
  153. return $this->_request;
  154. }
  155. /**
  156. * @param Response $response
  157. * @return $this
  158. */
  159. public function setResponse(Response $response)
  160. {
  161. $this->_response = $response;
  162. return $this;
  163. }
  164. /**
  165. * @return Response
  166. */
  167. public function getResponse()
  168. {
  169. if (null === $this->_response) {
  170. $this->_response = new Response();
  171. }
  172. return $this->_response;
  173. }
  174. /**
  175. * @param $unFormatted
  176. * @return string
  177. */
  178. public function formatModuleName($unFormatted)
  179. {
  180. return ucfirst($unFormatted);
  181. }
  182. /**
  183. * @param $unFormatted
  184. * @return string
  185. */
  186. public function formatControllerName($unFormatted)
  187. {
  188. return ucfirst($unFormatted);
  189. }
  190. /**
  191. * @param $unFormatted
  192. * @return string
  193. */
  194. public function formatActionName($unFormatted)
  195. {
  196. return $unFormatted;
  197. }
  198. /**
  199. * @param Controller $controller
  200. * @return $this
  201. */
  202. public function setDefaultController($controller)
  203. {
  204. $this->_defaultController = $controller;
  205. return $this;
  206. }
  207. /**
  208. * @return string
  209. */
  210. public function getDefaultController()
  211. {
  212. return $this->_defaultController;
  213. }
  214. /**
  215. * @param $action
  216. * @return $this
  217. */
  218. public function setDefaultAction($action)
  219. {
  220. $this->_defaultAction = $action;
  221. return $this;
  222. }
  223. /**
  224. * @return string
  225. */
  226. public function getDefaultAction()
  227. {
  228. return $this->_defaultAction;
  229. }
  230. /**
  231. * @param $view
  232. */
  233. public function setView($view)
  234. {
  235. $this->_view = $view;
  236. }
  237. /**
  238. * @return View
  239. */
  240. public function getView()
  241. {
  242. if (null === $this->_view) {
  243. $this->_view = new View($this->getModuleDir() . DIRECTORY_SEPARATOR . 'Views');
  244. }
  245. return $this->_view;
  246. }
  247. /**
  248. * @return string
  249. */
  250. public function getControllerDir()
  251. {
  252. $directory = $this->getModuleDir() . DIRECTORY_SEPARATOR . 'Controllers';
  253. return $directory;
  254. }
  255. /**
  256. * @param Request $request
  257. * @return string action name
  258. */
  259. public function getActionMethod(Request $request)
  260. {
  261. $action = $request->getActionName();
  262. if (empty($action)) {
  263. $action = $this->getDefaultAction();
  264. $request->setActionName($action);
  265. }
  266. return $this->formatActionName($action);
  267. }
  268. /**
  269. * @param Request $request
  270. * @return string
  271. * @throws Exception
  272. */
  273. public function getControllerClass(Request $request)
  274. {
  275. $controller = $request->getControllerName();
  276. if (empty($controller)) {
  277. $controller = $this->getDefaultController();
  278. $request->setControllerName($controller);
  279. }
  280. $module = $this->getModuleName();
  281. if (!empty($module)) {
  282. $module = $module . '\\';
  283. }
  284. $finalClass = $this->getApplicationName() . '\\' . $module . 'Controllers\\' . $this->formatControllerName($controller);
  285. if (!class_exists($finalClass)) {
  286. throw new Exception("Controllers : $finalClass not found!");
  287. }
  288. return $finalClass;
  289. }
  290. /**
  291. * @return string
  292. */
  293. public function getModuleName()
  294. {
  295. $module = $this->getRequest()->getModuleName();
  296. $module = $this->formatModuleName($module);
  297. return $module;
  298. }
  299. public function getModuleDir()
  300. {
  301. $module = $this->getModuleName();
  302. if (empty($module)) {
  303. return $this->getApplicationDir();
  304. }
  305. return $this->getApplicationDir() . DIRECTORY_SEPARATOR . $this->getModuleName();
  306. }
  307. /**
  308. * Dispatch to a controller/action
  309. */
  310. public function dispatch()
  311. {
  312. $request = $this->getRequest();
  313. $module = $this->getModuleName($request);
  314. if (!empty($module)) {
  315. if (!in_array($module, $this->getModules())) {
  316. throw new Exception('Module ' . $module . ' not found');
  317. }
  318. }
  319. if (null === $request->getActionName()) {
  320. $router = $this->getRouter();
  321. $router->route($request);
  322. }
  323. $this->setModuleName($module);
  324. $controllerClass = $this->getControllerClass($request);
  325. /* @var Controller $controller */
  326. $controller = new $controllerClass($this);
  327. $response = $this->getResponse();
  328. $controller->setResponse($response);
  329. $action = $this->getActionMethod($request);
  330. try {
  331. $controller->$action();
  332. } catch (Exception $e) {
  333. throw $e;
  334. }
  335. }
  336. public static function run()
  337. {
  338. self::getInstance()->dispatch();
  339. }
  340. }