Errors and exceptions handling, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@121 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
pzinovkin
2010-03-07 17:26:44 +00:00
parent 2d7f5391f8
commit 4f7e5b00bc
7 changed files with 305 additions and 82 deletions

59
app/ErrorAction.php Normal file
View File

@ -0,0 +1,59 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-25
* @version SVN: $Id$
* @filesource $URL$
*/
class ErrorAction extends ViewAction
{
/**
* @var Exception
*/
protected $exception;
public function __construct($exception)
{
$this->exception = $exception;
$this->view = FrontController::getInstance()->getView();
$this->execute();
}
protected function execute()
{
$this->template = 500;
if ($this->exception instanceof Error404Exception) {
$this->template = 404;
}
}
protected function getTemplate()
{
return '/static/' . $this->template;
}
protected function sendHttpCode($code)
{
if (headers_sent()) {
return;
}
switch ($code) {
case 404:
header('HTTP/1.0 404 Not Found');
break;
default:
header('HTTP/1.0 500 Internal Server Error');
}
}
public function fetch()
{
$this->sendHTTPCode($this->template);
return $this->view->fetch($this->getTemplate());
}
}

View File

@ -21,11 +21,6 @@ class FrontController
*/
protected $view = 'PHPView';
/**
* @var string
*/
protected $view_path;
protected $base_url = '';
/**
@ -36,9 +31,13 @@ class FrontController
private function __construct()
{
ErrorHandler::init();
$this->router = new Router();
}
/**
* Refuse cloning
*/
private function __clone(){}
/**
@ -46,23 +45,13 @@ class FrontController
*/
static public function getInstance()
{
if (! isset(self::$instance)) {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param string $path
* @return FrontController
*/
public function setViewPath($path)
{
$this->view_path = $path;
return $this;
}
/**
* @param string $view
* @return FrontController
*/
@ -79,7 +68,7 @@ class FrontController
public function getView($view = null)
{
$view = ($view) ? $view : $this->view;
return new $view($this->view_path);
return new $view(Config::get($view));
}
/**
@ -106,29 +95,26 @@ class FrontController
public function execute()
{
try {
try {
$request = Env::getRequestUri();
$route = $this->getRouter()->route($request);
if (!$route) {
throw new Exception('Route "' . $request . '" not found');
}
$action_class = $route->getAction();
$action = new $action_class();
$layout_class = $route->getLayout();
$layout = new $layout_class();
return $layout->fetch($action);
} catch (GeneralException $e) {
throw $e;
} catch (Exception $e) {
throw new GeneralException($e->getMessage(), $e->getCode());
$request = Env::getRequestUri();
$route = $this->getRouter()->route($request);
if (!$route) {
throw new Error404Exception('Route "' . $request . '" not found');
}
} catch (Exception $e) {
$action_class = $route->getAction();
$action = new $action_class();
$layout_class = $route->getLayout();
$layout = new $layout_class();
return $layout->fetch($action);
} catch(Exception $e) {
if (DEBUG == true) {
return $e->toHtml();
return ErrorHandler::showDebug($e);
}
$layout = new DefaultLayout();
return $layout->fetch(new ErrorAction($e));
}
}
}