Browse Source

Errors and exceptions handling, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@122 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
master
pzinovkin 15 years ago
parent
commit
5b7ad8e2ea
  1. 2
      Load.php
  2. 5
      app/Action.php
  3. 3
      app/ErrorAction.php
  4. 10
      app/FrontController.php
  5. 5
      app/router/Router.php
  6. 21
      classes/Env.class.php
  7. 7
      exception/ErrorHandler.php
  8. 15
      layout/Error.layout.php
  9. 2
      view/PHPView.php

2
Load.php

@ -53,7 +53,7 @@ class Load
trigger_error('Can\'t create directory: "' . $dir . '"', E_USER_ERROR); 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'); require_once(PATH . '/lib/core/util/AutoloadBuilder.php');

5
app/Action.php

@ -20,15 +20,12 @@ abstract class Action
protected function extractParams() protected function extractParams()
{ {
$params = FrontController::getInstance()->getRouter()->getRoute()->getParams();
if ($params) {
foreach ($params as $name => $value) {
foreach (Env::getParam() as $name => $value) {
if (is_string($name)) { if (is_string($name)) {
$this->$name = $value; $this->$name = $value;
} }
} }
} }
}
abstract protected function execute(); abstract protected function execute();

3
app/ErrorAction.php

@ -20,8 +20,7 @@ class ErrorAction extends ViewAction
public function __construct($exception) public function __construct($exception)
{ {
$this->exception = $exception; $this->exception = $exception;
$this->view = FrontController::getInstance()->getView();
$this->execute();
parent::__construct();
} }
protected function execute() protected function execute()

10
app/FrontController.php

@ -100,12 +100,18 @@ class FrontController
$request = Env::getRequestUri(); $request = Env::getRequestUri();
$route = $this->getRouter()->route($request); $route = $this->getRouter()->route($request);
if (!$route) { if (!$route) {
throw new Error404Exception('Route "' . $request . '" not found');
throw new Error404Exception('Route for "' . $request . '" not found');
} }
$action_class = $route->getAction(); $action_class = $route->getAction();
if (!class_exists($action_class)) {
throw new GeneralException('Action class "' . $action_class . '" not found.');
}
$action = new $action_class(); $action = new $action_class();
$layout_class = $route->getLayout(); $layout_class = $route->getLayout();
if (!class_exists($layout_class)) {
throw new GeneralException('Layout class "' . $layout_class . '" not found.');
}
$layout = new $layout_class(); $layout = new $layout_class();
return $layout->fetch($action); return $layout->fetch($action);
@ -113,7 +119,7 @@ class FrontController
if (DEBUG == true) { if (DEBUG == true) {
return ErrorHandler::showDebug($e); return ErrorHandler::showDebug($e);
} }
$layout = new DefaultLayout();
$layout = new ErrorLayout();
return $layout->fetch(new ErrorAction($e)); return $layout->fetch(new ErrorAction($e));
} }
} }

5
app/router/Router.php

@ -25,7 +25,7 @@ class Router
public function add($name, $route, $action, $params = null, $layout = null) public function add($name, $route, $action, $params = null, $layout = null)
{ {
if (! $layout) {
if (!$layout) {
$layout = $this->default_layout; $layout = $this->default_layout;
} }
$this->routes[$name] = new Route($route, $action, $params, $layout); $this->routes[$name] = new Route($route, $action, $params, $layout);
@ -37,8 +37,9 @@ class Router
foreach ($this->routes as $name => $route) { foreach ($this->routes as $name => $route) {
if ($route->match($req)) { if ($route->match($req)) {
$this->route_name = $route;
$this->route_name = $name;
$this->route = $route; $this->route = $route;
Env::setParams($route->getParams());
return $this->route; return $this->route;
} }
} }

21
classes/Env.class.php

@ -16,6 +16,8 @@ class Env
static protected $request = null; static protected $request = null;
static protected $params = array();
static public function getRequestUri() static public function getRequestUri()
{ {
if (self::$request === null) { if (self::$request === null) {
@ -100,4 +102,23 @@ class Env
$res = isset($_FILES[$name]) ? $_FILES[$name] : $default; $res = isset($_FILES[$name]) ? $_FILES[$name] : $default;
return $param ? $res[$param] : $res; 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;
}
} }

7
exception/ErrorHandler.php

@ -19,6 +19,7 @@ class ErrorHandler
static public function error_handler($errno, $errstr, $errfile, $errline ) static public function error_handler($errno, $errstr, $errfile, $errline )
{ {
ob_clean();
throw new ErrorException($errstr, 0, $errno, $errfile, $errline); throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
return false; return false;
} }
@ -31,9 +32,9 @@ class ErrorHandler
$i++; $i++;
if($i >= $hiline - 10 && $i <= $hiline + 10) { if($i >= $hiline - 10 && $i <= $hiline + 10) {
if ($i == $hiline) { if ($i == $hiline) {
$code[] = '<tr class="error"><th>' . $i . '</th><td><span class="specific">' . $line . '</span></td></tr>';
$code[] = '<tr class="error"><th>' . $i . '</th><td><span class="specific">' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</span></td></tr>';
}else{ }else{
$code[] = '<tr><th>' . $i . '</th><td>' . $line . '</td></tr>';
$code[] = '<tr><th>' . $i . '</th><td>' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</td></tr>';
} }
} }
if($i > $hiline + 10) { if($i > $hiline + 10) {
@ -50,6 +51,7 @@ class ErrorHandler
} }
$text = '<table class="req"><thead><tr><th>Variable</th><th>Value</th></tr></thead><tbody>'; $text = '<table class="req"><thead><tr><th>Variable</th><th>Value</th></tr></thead><tbody>';
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
$value = ($value) ? htmlentities($value, ENT_QUOTES, 'UTF-8') : '&nbsp;';
$text .= '<tr><td>' . $key . '</td><td class="code"><div>' . $value . '</div></td></tr>'; $text .= '<tr><td>' . $key . '</td><td class="code"><div>' . $value . '</div></td></tr>';
} }
$text .= '</tbody></table>'; $text .= '</tbody></table>';
@ -139,7 +141,6 @@ class ErrorHandler
<pre class="exception_value">{$exception->getMessage()}</pre> <pre class="exception_value">{$exception->getMessage()}</pre>
<table class="meta"> <table class="meta">
<tbody> <tbody>
<tbody>
<tr><th>Request Method:</th><td>{$method}</td></tr> <tr><th>Request Method:</th><td>{$method}</td></tr>
<tr><th>Exception Type:</th><td>{$class}</td></tr> <tr><th>Exception Type:</th><td>{$class}</td></tr>
<tr><th>Exception Message:</th><td><pre>{$exception->getMessage()}</pre></td></tr> <tr><th>Exception Message:</th><td><pre>{$exception->getMessage()}</pre></td></tr>

15
layout/Error.layout.php

@ -0,0 +1,15 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Layout
* @since 2010-02-25
* @version SVN: $Id$
* @filesource $URL$
*/
class ErrorLayout extends Layout
{
protected function execute(){}
}

2
view/PHPView.php

@ -20,7 +20,7 @@ class PHPView implements iView
public function __construct($config) public function __construct($config)
{ {
if (!isset($config['path'])) { if (!isset($config['path'])) {
throw new Exception('Configuration must have a "host".');
throw new Exception('Configuration must have a "path".');
} }
$this->setPath($config['path']); $this->setPath($config['path']);
} }

Loading…
Cancel
Save