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.

147 lines
3.3 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
13 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. */
  32. private function __clone()
  33. {
  34. }
  35. /**
  36. * @return FrontController
  37. */
  38. static public function getInstance()
  39. {
  40. if (!isset(self::$instance)) {
  41. self::$instance = new self();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param string $view
  47. * @return FrontController
  48. */
  49. public function setView($view)
  50. {
  51. $this->view = $view;
  52. return $this;
  53. }
  54. /**
  55. *
  56. * @param null $view
  57. * @return iView
  58. */
  59. public function getView($view = null)
  60. {
  61. $view = ($view) ? $view : $this->view;
  62. return new $view(Config::get($view));
  63. }
  64. /**
  65. * @param string $url
  66. * @return FrontController
  67. */
  68. public function setBaseUrl($url)
  69. {
  70. $this->base_url = rtrim($url, '/');
  71. return $this;
  72. }
  73. public function getBaseUrl()
  74. {
  75. return $this->base_url;
  76. }
  77. /**
  78. * @return Router
  79. */
  80. public function getRouter()
  81. {
  82. return $this->router;
  83. }
  84. public function execute()
  85. {
  86. try {
  87. $request = Env::getRequestUri(true);
  88. $route = $this->getRouter()->route($request);
  89. if (!$route) {
  90. throw new Error404Exception('Route for "' . $request . '" not found');
  91. }
  92. $action_class = $route->getAction();
  93. if (!class_exists($action_class)) {
  94. throw new GeneralException('Action class "' . $action_class . '" not found.');
  95. }
  96. $action = new $action_class();
  97. $layout_class = $route->getLayout();
  98. if (!class_exists($layout_class)) {
  99. throw new GeneralException('Layout class "' . $layout_class . '" not found.');
  100. }
  101. /**
  102. * @var Layout $layout
  103. */
  104. $layout = new $layout_class();
  105. $html = $layout->fetch($action);
  106. if (Config::get('PROFILER')) {
  107. if (is_subclass_of($action, 'AjaxAction')) {
  108. Profiler::getInstance()->getJson();
  109. } else {
  110. $html = Profiler::getInstance()->end($html);
  111. }
  112. }
  113. return $html;
  114. } catch (Exception $e) {
  115. if (Config::get('DEBUG')) {
  116. if (!headers_sent()) {
  117. header('HTTP/1.0 500 Internal Server Error');
  118. }
  119. return ErrorHandler::showDebug($e);
  120. }
  121. $layout_class = $this->getRouter()->getErrorLayout();
  122. /**
  123. * @var ErrorLayout $layout
  124. */
  125. $layout = new $layout_class();
  126. $layout->setException($e);
  127. return $layout->fetch(new ErrorAction($e));
  128. }
  129. }
  130. }