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.

162 lines
4.1 KiB

11 years ago
11 years ago
13 years ago
11 years ago
13 years ago
13 years ago
13 years ago
11 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
11 years ago
11 years ago
13 years ago
12 years ago
13 years ago
13 years ago
11 years ago
13 years ago
11 years ago
11 years ago
13 years ago
11 years ago
13 years ago
11 years ago
13 years ago
12 years ago
11 years ago
12 years ago
11 years ago
13 years ago
11 years ago
13 years ago
11 years ago
13 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php namespace Majestic\App;
  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\Router
  13. */
  14. protected $router;
  15. /**
  16. * @var string
  17. */
  18. protected $view = '\Majestic\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\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 ..\View\PHPView
  62. */
  63. public function getView($view = null)
  64. {
  65. $view = ($view) ? $view : $this->view;
  66. return new $view(\Majestic\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\Router
  83. */
  84. public function getRouter()
  85. {
  86. return $this->router;
  87. }
  88. public function execute()
  89. {
  90. try {
  91. $request = \Majestic\Env::getRequestUri(true);
  92. $route = $this->getRouter()->route($request);
  93. if (!$route) {
  94. throw new \Majestic\Exception\Error404Exception('Route for "' . $request . '" not found');
  95. }
  96. $action_class = $route->getAction();
  97. if (!class_exists($action_class)) {
  98. throw new \Majestic\Exception\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 \Majestic\Exception\GeneralException('Layout class "' . $layout_class . '" not found.');
  104. }
  105. /**
  106. * @var \Majestic\Layout\Layout $layout
  107. */
  108. $layout = new $layout_class();
  109. $html = $layout->fetch($action);
  110. if (\Majestic\Config::get('PROFILER')) {
  111. if (is_subclass_of($action, 'AjaxAction')) {
  112. \Majestic\Util\Profiler\Profiler::getInstance()->getJson();
  113. } else {
  114. $html = \Majestic\Util\Profiler\Profiler::getInstance()->end($html);
  115. }
  116. }
  117. return $html;
  118. }
  119. catch (\Exception $e) {
  120. if (\Majestic\Config::get('DEBUG')) {
  121. if (!headers_sent()) {
  122. if ($e instanceof \Majestic\Exception\ErrorHTTPException) {
  123. header($e->getHTTPHeader());
  124. } else {
  125. header('HTTP/1.0 500 Internal Server Error');
  126. }
  127. }
  128. \Majestic\Exception\ErrorHandler::logError($e);
  129. return \Majestic\Exception\ErrorHandler::showDebug($e);
  130. }
  131. $layout_class = $this->getRouter()->getErrorLayout();
  132. /**
  133. * @var \Majestic\Layout\ErrorLayout $layout
  134. */
  135. $layout = new $layout_class();
  136. $layout->setException($e);
  137. $error_action = new ErrorAction($e);
  138. if (isset($action_class) && is_subclass_of($action_class, 'AjaxAction')) {
  139. $error_action->setAjaxError();
  140. }
  141. return $layout->fetch($error_action);
  142. }
  143. }
  144. }