You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.7 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
14 years ago
  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-24
  8. */
  9. class FrontController
  10. {
  11. /**
  12. * @var Router
  13. */
  14. protected $router;
  15. /**
  16. * @var string
  17. */
  18. protected $view = 'PHPView';
  19. protected $base_url = '';
  20. /**
  21. * @var FrontController
  22. */
  23. protected static $instance;
  24. private function __construct()
  25. {
  26. ErrorHandler::init();
  27. $this->router = new Router();
  28. }
  29. /**
  30. * Refuse cloning
  31. * @codeCoverageIgnoreStart
  32. */
  33. private function __clone()
  34. {
  35. }
  36. /**
  37. * @codeCoverageIgnoreEnd
  38. */
  39. /**
  40. * @return FrontController
  41. */
  42. static public function getInstance()
  43. {
  44. if (!isset(self::$instance)) {
  45. self::$instance = new self();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * @param string $view
  51. * @return FrontController
  52. */
  53. public function setView($view)
  54. {
  55. $this->view = $view;
  56. return $this;
  57. }
  58. /**
  59. *
  60. * @param null $view
  61. * @return iView
  62. */
  63. public function getView($view = null)
  64. {
  65. $view = ($view) ? $view : $this->view;
  66. return new $view(Config::get($view));
  67. }
  68. /**
  69. * @param string $url
  70. * @return FrontController
  71. */
  72. public function setBaseUrl($url)
  73. {
  74. $this->base_url = rtrim($url, '/');
  75. return $this;
  76. }
  77. public function getBaseUrl()
  78. {
  79. return $this->base_url;
  80. }
  81. /**
  82. * @return Router
  83. */
  84. public function getRouter()
  85. {
  86. return $this->router;
  87. }
  88. public function execute()
  89. {
  90. try {
  91. $request = Env::getRequestUri(true);
  92. $route = $this->getRouter()->route($request);
  93. if (!$route) {
  94. throw new Error404Exception('Route for "' . $request . '" not found');
  95. }
  96. $action_class = $route->getAction();
  97. if (!class_exists($action_class)) {
  98. throw new GeneralException('Action class "' . $action_class . '" not found.');
  99. }
  100. $action = new $action_class();
  101. $layout_class = $route->getLayout();
  102. if (!class_exists($layout_class)) {
  103. throw new GeneralException('Layout class "' . $layout_class . '" not found.');
  104. }
  105. /**
  106. * @var Layout $layout
  107. */
  108. $layout = new $layout_class();
  109. $html = $layout->fetch($action);
  110. if (Config::get('PROFILER')) {
  111. if (is_subclass_of($action, 'AjaxAction')) {
  112. Profiler::getInstance()->getJson();
  113. } else {
  114. $html = Profiler::getInstance()->end($html);
  115. }
  116. }
  117. return $html;
  118. }
  119. catch (Exception $e) {
  120. if (Config::get('DEBUG')) {
  121. if (!headers_sent()) {
  122. if ($e instanceof ErrorHTTPException) {
  123. header($e->getHTTPHeader());
  124. } else {
  125. header('HTTP/1.0 500 Internal Server Error');
  126. }
  127. }
  128. return ErrorHandler::showDebug($e);
  129. }
  130. $layout_class = $this->getRouter()->getErrorLayout();
  131. /**
  132. * @var ErrorLayout $layout
  133. */
  134. $layout = new $layout_class();
  135. $layout->setException($e);
  136. $error_action = new ErrorAction($e);
  137. if (isset($action_class) && is_subclass_of($action_class, 'AjaxAction')) {
  138. $error_action->setAjaxError();
  139. }
  140. return $layout->fetch($error_action);
  141. }
  142. }
  143. }