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.

135 lines
3.0 KiB

10 years ago
10 years ago
13 years ago
10 years ago
13 years ago
13 years ago
13 years ago
10 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
10 years ago
10 years ago
13 years ago
12 years ago
13 years ago
13 years ago
10 years ago
13 years ago
13 years ago
13 years ago
  1. <?php namespace Majestic\App;
  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\Router
  13. */
  14. protected $router;
  15. /**
  16. * @var string
  17. */
  18. protected $view = '\Majestic\View\PHPView';
  19. protected $base_url = '';
  20. /**
  21. * @var FrontController
  22. */
  23. protected static $instance;
  24. private function __construct()
  25. {
  26. // \Majestic\Exception\ErrorHandler::init();
  27. $this->router = new Router\Router();
  28. }
  29. /**
  30. * Refuse cloning
  31. * @codeCoverageIgnoreStart
  32. */
  33. private function __clone()
  34. {
  35. }
  36. /**
  37. * @codeCoverageIgnoreEnd
  38. */
  39. /**
  40. * @return FrontController
  41. */
  42. static public function getInstance()
  43. {
  44. if (!isset(self::$instance)) {
  45. self::$instance = new self();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * @param string $view
  51. * @return FrontController
  52. */
  53. public function setView($view)
  54. {
  55. $this->view = $view;
  56. return $this;
  57. }
  58. /**
  59. *
  60. * @param null $view
  61. * @return ..\View\PHPView
  62. */
  63. public function getView($view = null)
  64. {
  65. $view = ($view) ? $view : $this->view;
  66. return new $view(\Majestic\Config::get($view));
  67. }
  68. /**
  69. * @param string $url
  70. * @return FrontController
  71. */
  72. public function setBaseUrl($url)
  73. {
  74. $this->base_url = rtrim($url, '/');
  75. return $this;
  76. }
  77. public function getBaseUrl()
  78. {
  79. return $this->base_url;
  80. }
  81. /**
  82. * @return Router\Router
  83. */
  84. public function getRouter()
  85. {
  86. return $this->router;
  87. }
  88. public function execute()
  89. {
  90. $request = \Majestic\Env::getRequestUri(true);
  91. $route = $this->getRouter()->route($request);
  92. if (!$route) {
  93. throw new \Majestic\Exception\Error404Exception('Route for "' . $request . '" not found');
  94. }
  95. $action_class = $route->getAction();
  96. if (!class_exists($action_class)) {
  97. throw new \Majestic\Exception\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 \Majestic\Exception\GeneralException('Layout class "' . $layout_class . '" not found.');
  103. }
  104. /**
  105. * @var \Majestic\Layout\Layout $layout
  106. */
  107. $layout = new $layout_class();
  108. $html = $layout->fetch($action);
  109. if (\Majestic\Config::get('PROFILER')) {
  110. if (is_subclass_of($action, 'AjaxAction')) {
  111. \Majestic\Util\Profiler\Profiler::getInstance()->getJson();
  112. } else {
  113. $html = \Majestic\Util\Profiler\Profiler::getInstance()->end($html);
  114. }
  115. }
  116. return $html;
  117. }
  118. }