Permalink
Browse files

precondition()s should return true/false, throws PreconditionExceptio…

…n if not met
  • Loading branch information...
1 parent 059b6d0 commit 831fcfd9fb77fe97617246cc140f5c32d7eea8f8 @noodlehaus committed Feb 15, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 dispatch.php
View
@@ -6,6 +6,9 @@
// throw this when pass() is called
class PassException extends Exception {}
+// for failed preconditions
+class PreconditionException extends Exception {}
+
function config($key, $value = null) {
static $_config = null;
@@ -232,7 +235,11 @@ function precondition() {
if (count($args) && is_callable($args[0])) {
$cb_map[$name] = $args[0];
} else {
- isset($cb_map[$name]) && is_callable($cb_map[$name]) && call_user_func_array($cb_map[$name], $args);
+ if (isset($cb_map[$name]) && is_callable($cb_map[$name])) {
+ if (!call_user_func_array($cb_map[$name], $args)) {
+ throw new PreconditionException('Precondition not met');
+ }
+ }
}
}
@@ -317,6 +324,9 @@ function route($method, $pattern, $callback = null) {
call_user_func_array($obj['callback'], $params);
}
break;
+ } catch (PreconditionException $e) {
+ error(403, 'Precondition not met');
+ break;
} catch (PassException $e) {
continue;
}

0 comments on commit 831fcfd

Please sign in to comment.