Files
majestic/App/FrontController.php

163 lines
4.1 KiB
PHP
Raw Normal View History

2014-06-02 18:58:49 +04:00
<?php namespace Majestic\App;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-24
*/
class FrontController
{
/**
2014-06-02 18:58:49 +04:00
* @var Router\Router
*/
protected $router;
2011-10-13 14:55:06 +04:00
/**
* @var string
*/
2014-06-02 18:58:49 +04:00
protected $view = '\Majestic\View\PHPView';
2011-10-13 14:55:06 +04:00
protected $base_url = '';
2011-10-13 14:55:06 +04:00
/**
* @var FrontController
*/
protected static $instance;
2011-10-13 14:55:06 +04:00
private function __construct()
{
// \Majestic\Exception\ErrorHandler::init();
2014-06-02 18:58:49 +04:00
$this->router = new Router\Router();
}
2011-10-13 14:55:06 +04:00
/**
* Refuse cloning
* @codeCoverageIgnoreStart
*/
private function __clone()
{
}
/**
* @codeCoverageIgnoreEnd
*/
/**
* @return FrontController
*/
static public function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
2011-10-13 14:55:06 +04:00
/**
* @param string $view
* @return FrontController
*/
public function setView($view)
{
$this->view = $view;
return $this;
}
2011-10-13 14:55:06 +04:00
/**
2011-10-13 14:55:06 +04:00
*
2012-11-10 15:55:11 +04:00
* @param null $view
2014-06-02 18:58:49 +04:00
* @return ..\View\PHPView
*/
public function getView($view = null)
{
$view = ($view) ? $view : $this->view;
2014-06-02 18:58:49 +04:00
return new $view(\Majestic\Config::get($view));
}
2011-10-13 14:55:06 +04:00
/**
* @param string $url
2012-11-10 15:55:11 +04:00
* @return FrontController
*/
public function setBaseUrl($url)
{
$this->base_url = rtrim($url, '/');
return $this;
}
2011-10-13 14:55:06 +04:00
public function getBaseUrl()
{
return $this->base_url;
}
2011-10-13 14:55:06 +04:00
/**
2014-06-02 18:58:49 +04:00
* @return Router\Router
*/
public function getRouter()
{
return $this->router;
}
2011-10-13 14:55:06 +04:00
public function execute()
{
try {
2014-06-02 18:58:49 +04:00
$request = \Majestic\Env::getRequestUri(true);
$route = $this->getRouter()->route($request);
if (!$route) {
2014-06-02 18:58:49 +04:00
throw new \Majestic\Exception\Error404Exception('Route for "' . $request . '" not found');
}
2011-10-13 14:55:06 +04:00
$action_class = $route->getAction();
if (!class_exists($action_class)) {
2014-06-02 18:58:49 +04:00
throw new \Majestic\Exception\GeneralException('Action class "' . $action_class . '" not found.');
}
2011-10-13 14:55:06 +04:00
$action = new $action_class();
$layout_class = $route->getLayout();
if (!class_exists($layout_class)) {
2014-06-02 18:58:49 +04:00
throw new \Majestic\Exception\GeneralException('Layout class "' . $layout_class . '" not found.');
}
2011-10-13 14:55:06 +04:00
2012-11-10 15:55:11 +04:00
/**
2014-06-02 18:58:49 +04:00
* @var \Majestic\Layout\Layout $layout
2012-11-10 15:55:11 +04:00
*/
$layout = new $layout_class();
$html = $layout->fetch($action);
2014-06-02 18:58:49 +04:00
if (\Majestic\Config::get('PROFILER')) {
2011-07-29 13:05:23 +04:00
if (is_subclass_of($action, 'AjaxAction')) {
2014-06-02 18:58:49 +04:00
\Majestic\Util\Profiler\Profiler::getInstance()->getJson();
2011-07-29 13:05:23 +04:00
} else {
2014-06-02 18:58:49 +04:00
$html = \Majestic\Util\Profiler\Profiler::getInstance()->end($html);
2011-07-29 13:05:23 +04:00
}
}
return $html;
}
2014-06-02 18:58:49 +04:00
catch (\Exception $e) {
if (\Majestic\Config::get('DEBUG')) {
2011-06-01 22:25:06 +04:00
if (!headers_sent()) {
2014-06-02 18:58:49 +04:00
if ($e instanceof \Majestic\Exception\ErrorHTTPException) {
header($e->getHTTPHeader());
} else {
header('HTTP/1.0 500 Internal Server Error');
}
2011-06-01 22:25:06 +04:00
}
2014-06-02 18:58:49 +04:00
\Majestic\Exception\ErrorHandler::logError($e);
return \Majestic\Exception\ErrorHandler::showDebug($e);
}
$layout_class = $this->getRouter()->getErrorLayout();
/**
2014-06-02 18:58:49 +04:00
* @var \Majestic\Layout\ErrorLayout $layout
*/
$layout = new $layout_class();
$layout->setException($e);
$error_action = new ErrorAction($e);
if (isset($action_class) && is_subclass_of($action_class, 'AjaxAction')) {
$error_action->setAjaxError();
}
return $layout->fetch($error_action);
}
}
}