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.

132 lines
3.0 KiB

  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. return (!DEBUG) ? $html : Profiler::getInstance()->end($html);
  105. } catch(Exception $e) {
  106. if (DEBUG == true) {
  107. if (!headers_sent()) {
  108. header('HTTP/1.0 500 Internal Server Error');
  109. }
  110. return ErrorHandler::showDebug($e);
  111. }
  112. $layout = new ErrorLayout();
  113. return $layout->fetch(new ErrorAction($e));
  114. }
  115. }
  116. }