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
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 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. * @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. if (DEBUG == true) {
  30. Profiler::getInstance()->start();
  31. }
  32. $this->router = new Router();
  33. }
  34. /**
  35. * Refuse cloning
  36. */
  37. private function __clone()
  38. {
  39. }
  40. /**
  41. * @return FrontController
  42. */
  43. static public function getInstance()
  44. {
  45. if (!isset(self::$instance)) {
  46. self::$instance = new self();
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * @param string $view
  52. * @return FrontController
  53. */
  54. public function setView($view)
  55. {
  56. $this->view = $view;
  57. return $this;
  58. }
  59. /**
  60. *
  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. */
  71. public function setBaseUrl($url)
  72. {
  73. $this->base_url = rtrim($url, '/');
  74. return $this;
  75. }
  76. public function getBaseUrl()
  77. {
  78. return $this->base_url;
  79. }
  80. /**
  81. * @return Router
  82. */
  83. public function getRouter()
  84. {
  85. return $this->router;
  86. }
  87. public function execute()
  88. {
  89. try {
  90. $request = Env::getRequestUri(true);
  91. $route = $this->getRouter()->route($request);
  92. if (!$route) {
  93. throw new Error404Exception('Route for "' . $request . '" not found');
  94. }
  95. $action_class = $route->getAction();
  96. if (!class_exists($action_class)) {
  97. throw new GeneralException('Action class "' . $action_class . '" not found.');
  98. }
  99. $action = new $action_class();
  100. $layout_class = $route->getLayout();
  101. if (!class_exists($layout_class)) {
  102. throw new GeneralException('Layout class "' . $layout_class . '" not found.');
  103. }
  104. $layout = new $layout_class();
  105. $html = $layout->fetch($action);
  106. if (DEBUG) {
  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 (DEBUG == true) {
  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. }