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.

144 lines
3.2 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
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 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. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class FrontController
  12. {
  13. /**
  14. * @var Router
  15. */
  16. protected $router;
  17. /**
  18. * @var string
  19. */
  20. protected $view = 'PHPView';
  21. protected $base_url = '';
  22. /**
  23. * @var FrontController
  24. */
  25. protected static $instance;
  26. private function __construct()
  27. {
  28. ErrorHandler::init();
  29. $this->router = new Router();
  30. }
  31. /**
  32. * Refuse cloning
  33. */
  34. private function __clone()
  35. {
  36. }
  37. /**
  38. * @return FrontController
  39. */
  40. static public function getInstance()
  41. {
  42. if (!isset(self::$instance)) {
  43. self::$instance = new self();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param string $view
  49. * @return FrontController
  50. */
  51. public function setView($view)
  52. {
  53. $this->view = $view;
  54. return $this;
  55. }
  56. /**
  57. *
  58. * @return iView
  59. */
  60. public function getView($view = null)
  61. {
  62. $view = ($view) ? $view : $this->view;
  63. return new $view(Config::get($view));
  64. }
  65. /**
  66. * @param string $url
  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. $layout = new $layout_class();
  102. $html = $layout->fetch($action);
  103. if (Config::get('PROFILER')) {
  104. if (is_subclass_of($action, 'AjaxAction')) {
  105. Profiler::getInstance()->getJson();
  106. } else {
  107. $html = Profiler::getInstance()->end($html);
  108. }
  109. }
  110. return $html;
  111. } catch (Exception $e) {
  112. if (Config::get('DEBUG')) {
  113. if (!headers_sent()) {
  114. header('HTTP/1.0 500 Internal Server Error');
  115. }
  116. return ErrorHandler::showDebug($e);
  117. }
  118. $layout_class = $this->getRouter()->getErrorLayout();
  119. /**
  120. * @var ErrorLayout $layout
  121. */
  122. $layout = new $layout_class();
  123. $layout->setException($e);
  124. return $layout->fetch(new ErrorAction($e));
  125. }
  126. }
  127. }