From 5b7ad8e2ea248a65510a4ac26b4c830517b8684d Mon Sep 17 00:00:00 2001 From: pzinovkin Date: Sun, 7 Mar 2010 19:54:09 +0000 Subject: [PATCH] Errors and exceptions handling, #16 git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@122 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- Load.php | 2 +- app/Action.php | 9 +++------ app/ErrorAction.php | 3 +-- app/FrontController.php | 10 ++++++++-- app/router/Router.php | 5 +++-- classes/Env.class.php | 21 +++++++++++++++++++++ exception/ErrorHandler.php | 7 ++++--- layout/Error.layout.php | 15 +++++++++++++++ view/PHPView.php | 2 +- 9 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 layout/Error.layout.php diff --git a/Load.php b/Load.php index c82b6c3..432cd2c 100644 --- a/Load.php +++ b/Load.php @@ -53,7 +53,7 @@ class Load trigger_error('Can\'t create directory: "' . $dir . '"', E_USER_ERROR); } - $scan = array(PATH . '/lib', PATH . '/' . APP . '/src'); + $scan = array(PATH . '/' . APP . '/src', PATH . '/lib'); require_once(PATH . '/lib/core/util/AutoloadBuilder.php'); diff --git a/app/Action.php b/app/Action.php index 1e734d9..457f9bf 100644 --- a/app/Action.php +++ b/app/Action.php @@ -20,12 +20,9 @@ abstract class Action protected function extractParams() { - $params = FrontController::getInstance()->getRouter()->getRoute()->getParams(); - if ($params) { - foreach ($params as $name => $value) { - if (is_string($name)) { - $this->$name = $value; - } + foreach (Env::getParam() as $name => $value) { + if (is_string($name)) { + $this->$name = $value; } } } diff --git a/app/ErrorAction.php b/app/ErrorAction.php index 0ffc9ee..d6370b0 100644 --- a/app/ErrorAction.php +++ b/app/ErrorAction.php @@ -20,8 +20,7 @@ class ErrorAction extends ViewAction public function __construct($exception) { $this->exception = $exception; - $this->view = FrontController::getInstance()->getView(); - $this->execute(); + parent::__construct(); } protected function execute() diff --git a/app/FrontController.php b/app/FrontController.php index 67d683e..2ea3154 100644 --- a/app/FrontController.php +++ b/app/FrontController.php @@ -100,12 +100,18 @@ class FrontController $request = Env::getRequestUri(); $route = $this->getRouter()->route($request); if (!$route) { - throw new Error404Exception('Route "' . $request . '" not found'); + throw new Error404Exception('Route for "' . $request . '" not found'); } $action_class = $route->getAction(); + if (!class_exists($action_class)) { + throw new GeneralException('Action class "' . $action_class . '" not found.'); + } $action = new $action_class(); $layout_class = $route->getLayout(); + if (!class_exists($layout_class)) { + throw new GeneralException('Layout class "' . $layout_class . '" not found.'); + } $layout = new $layout_class(); return $layout->fetch($action); @@ -113,7 +119,7 @@ class FrontController if (DEBUG == true) { return ErrorHandler::showDebug($e); } - $layout = new DefaultLayout(); + $layout = new ErrorLayout(); return $layout->fetch(new ErrorAction($e)); } } diff --git a/app/router/Router.php b/app/router/Router.php index 44ce240..4ad8915 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -25,7 +25,7 @@ class Router public function add($name, $route, $action, $params = null, $layout = null) { - if (! $layout) { + if (!$layout) { $layout = $this->default_layout; } $this->routes[$name] = new Route($route, $action, $params, $layout); @@ -37,8 +37,9 @@ class Router foreach ($this->routes as $name => $route) { if ($route->match($req)) { - $this->route_name = $route; + $this->route_name = $name; $this->route = $route; + Env::setParams($route->getParams()); return $this->route; } } diff --git a/classes/Env.class.php b/classes/Env.class.php index eafb9fe..bca691d 100644 --- a/classes/Env.class.php +++ b/classes/Env.class.php @@ -16,6 +16,8 @@ class Env static protected $request = null; + static protected $params = array(); + static public function getRequestUri() { if (self::$request === null) { @@ -100,4 +102,23 @@ class Env $res = isset($_FILES[$name]) ? $_FILES[$name] : $default; return $param ? $res[$param] : $res; } + + static public function getParam($key = null, $default = false) + { + if ($key === null) { + return self::$params; + } + return (isset(self::$params[$key])) ? self::$params[$key] : $default; + } + + static public function setParam($key, $value) + { + self::$params[$key] = $value; + } + + static public function setParams($params = array()) + { + self::$params = self::$params + $params; + } + } \ No newline at end of file diff --git a/exception/ErrorHandler.php b/exception/ErrorHandler.php index 18dd2f6..1bd7fc7 100644 --- a/exception/ErrorHandler.php +++ b/exception/ErrorHandler.php @@ -19,6 +19,7 @@ class ErrorHandler static public function error_handler($errno, $errstr, $errfile, $errline ) { + ob_clean(); throw new ErrorException($errstr, 0, $errno, $errfile, $errline); return false; } @@ -31,9 +32,9 @@ class ErrorHandler $i++; if($i >= $hiline - 10 && $i <= $hiline + 10) { if ($i == $hiline) { - $code[] = '' . $i . '' . $line . ''; + $code[] = '' . $i . '' . htmlentities($line, ENT_QUOTES, 'UTF-8') . ''; }else{ - $code[] = '' . $i . '' . $line . ''; + $code[] = '' . $i . '' . htmlentities($line, ENT_QUOTES, 'UTF-8') . ''; } } if($i > $hiline + 10) { @@ -50,6 +51,7 @@ class ErrorHandler } $text = ''; foreach ($array as $key => $value) { + $value = ($value) ? htmlentities($value, ENT_QUOTES, 'UTF-8') : ' '; $text .= ''; } $text .= '
VariableValue
' . $key . '
' . $value . '
'; @@ -139,7 +141,6 @@ class ErrorHandler
{$exception->getMessage()}
- diff --git a/layout/Error.layout.php b/layout/Error.layout.php new file mode 100644 index 0000000..d8fe312 --- /dev/null +++ b/layout/Error.layout.php @@ -0,0 +1,15 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Layout + * @since 2010-02-25 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class ErrorLayout extends Layout +{ + protected function execute(){} +} \ No newline at end of file diff --git a/view/PHPView.php b/view/PHPView.php index cdb7f9e..dc77d9d 100644 --- a/view/PHPView.php +++ b/view/PHPView.php @@ -20,7 +20,7 @@ class PHPView implements iView public function __construct($config) { if (!isset($config['path'])) { - throw new Exception('Configuration must have a "host".'); + throw new Exception('Configuration must have a "path".'); } $this->setPath($config['path']); }
Request Method:{$method}
Exception Type:{$class}
Exception Message:
{$exception->getMessage()}