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.

133 lines
2.7 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. /**
  22. * @var string
  23. */
  24. protected $view_path;
  25. protected $base_url = '';
  26. /**
  27. * @var FrontController
  28. */
  29. protected static $instance;
  30. private function __construct()
  31. {
  32. $this->router = new Router();
  33. }
  34. private function __clone(){}
  35. /**
  36. * @return FrontController
  37. */
  38. static public function getInstance()
  39. {
  40. if (! isset(self::$instance)) {
  41. self::$instance = new self();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param string $path
  47. * @return FrontController
  48. */
  49. public function setViewPath($path)
  50. {
  51. $this->view_path = $path;
  52. return $this;
  53. }
  54. /**
  55. * @param string $view
  56. * @return FrontController
  57. */
  58. public function setView($view)
  59. {
  60. $this->view = $view;
  61. return $this;
  62. }
  63. /**
  64. *
  65. * @return iView
  66. */
  67. public function getView($view = null)
  68. {
  69. $view = ($view) ? $view : $this->view;
  70. return new $view($this->view_path);
  71. }
  72. /**
  73. * @param string $url
  74. */
  75. public function setBaseUrl($url)
  76. {
  77. $this->base_url = rtrim($url, '/');
  78. return $this;
  79. }
  80. public function getBaseUrl()
  81. {
  82. return $this->base_url;
  83. }
  84. /**
  85. * @return Router
  86. */
  87. public function getRouter()
  88. {
  89. return $this->router;
  90. }
  91. public function execute()
  92. {
  93. try {
  94. try {
  95. $request = Env::getRequestUri();
  96. $route = $this->getRouter()->route($request);
  97. if (!$route) {
  98. throw new Exception('Route "' . $request . '" not found');
  99. }
  100. $action_class = $route->getAction();
  101. $action = new $action_class();
  102. $layout_class = $route->getLayout();
  103. $layout = new $layout_class();
  104. return $layout->fetch($action);
  105. } catch (GeneralException $e) {
  106. throw $e;
  107. } catch (Exception $e) {
  108. throw new GeneralException($e->getMessage(), $e->getCode());
  109. }
  110. } catch (Exception $e) {
  111. if (DEBUG == true) {
  112. return $e->toHtml();
  113. }
  114. }
  115. }
  116. }