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.

139 lines
3.2 KiB

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