Skip to content

Commit

Permalink
Implementing \core\ErrorHandler::apply().
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Jan 6, 2011
1 parent 63ff4d8 commit 775bf9b
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions libraries/lithium/core/ErrorHandler.php
Expand Up @@ -8,7 +8,7 @@

namespace lithium\core;

use \Exception;
use Exception;
use lithium\util\Collection;
use lithium\core\Environment;

Expand Down Expand Up @@ -122,13 +122,17 @@ public static function handlers($handlers = array()) {
/**
* Configure the `ErrorHandler`.
*
* @param array $config Configuration directives.
* @var array $config Configuration directives.
* @return Current configuration set.
*/
public static function config(array $config = array()) {
public static function config($config = array()) {
return (static::$_config = array_merge($config, static::$_config));
}

public static function handler($name, $info) {
return static::$_handlers[$name]($info);
}

/**
* Register error and exception handlers.
*
Expand Down Expand Up @@ -241,7 +245,7 @@ public static function handle($info, $scope = array()) {
/**
* Determine frame from the stack trace where the error/exception was first generated.
*
* @param array $stack Stacktrace from error/exception that was produced.
* @var array $stack Stack trace from error/exception that was produced.
* @return string Class where error/exception was generated.
*/
protected static function _origin(array $stack) {
Expand All @@ -252,10 +256,54 @@ protected static function _origin(array $stack) {
}
}

public static function apply($class, $method, array $conditions, $handler) {
$_self = get_called_class();

$filter = function($self, $params, $chain) use ($_self, $conditions, $handler) {
try {
return $chain->next($self, $params, $chain);
} catch (Exception $e) {
if (!$_self::matches($e, $conditions)) {
throw $e;
}
return $handler($e, $params);
}
};

if (is_string($class)) {
$class::applyFilter($method, $filter);
} else {
$class->applyFilter($method, $filter);
}
}

public static function matches($info, $conditions) {
$checks = static::$_checks;
$handler = static::$_exceptionHandler;
$info = is_object($info) ? $handler($info, true) : $info;

foreach (array_keys($conditions) as $key) {
if ($key == 'conditions' || $key == 'scope' || $key == 'handler') {
continue;
}
if (!isset($info[$key]) || !isset($checks[$key])) {
return false;
}
if (($check = $checks[$key]) && !$check($conditions, $info)) {
return false;
}
}
if ((isset($config['conditions']) && $call = $config['conditions']) && !$call($info)) {
return false;
}
return true;
}

/**
* Trim down a typical stack trace to class & method calls.
*
* @param array $stack A debug_backtrace() compatible stacktrace output.
* @var array $stack A `debug_backtrace()`-compatible stack trace output.
* @return array Returns a flat stack array containing class and method references.
*/
protected static function _trace(array $stack) {
$result = array();
Expand Down

0 comments on commit 775bf9b

Please sign in to comment.