From f7fa0eb7eadd6025903d7bd32d89667c1eecb70f Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Wed, 17 Mar 2010 22:56:29 -0400 Subject: [PATCH] - Merged lib/Events.php with lib/ActiveMongo.php --- lib/ActiveMongo.php | 117 ++++++++++++++++++++++++++++++++++-- lib/Validators.php | 16 ++--- samples/blog/Author.php | 6 +- samples/dynamic_references/User.php | 8 +-- samples/references/User.php | 8 +-- 5 files changed, 132 insertions(+), 23 deletions(-) diff --git a/lib/ActiveMongo.php b/lib/ActiveMongo.php index 697753c..beeb25a 100644 --- a/lib/ActiveMongo.php +++ b/lib/ActiveMongo.php @@ -35,8 +35,6 @@ +---------------------------------------------------------------------------------+ */ -require dirname(__FILE__)."/Events.php"; - // Class FilterException {{{ /** * FilterException @@ -45,7 +43,7 @@ require dirname(__FILE__)."/Events.php"; * fails when save() is called. * */ -final class FilterException extends Exception +class ActiveMongo_FilterException extends Exception { } // }}} @@ -77,7 +75,7 @@ function get_object_vars_ex($obj) * @version 1.0 * */ -abstract class ActiveMongo extends ActiveMongo_Events implements Iterator +abstract class ActiveMongo implements Iterator { // properties {{{ @@ -106,6 +104,18 @@ abstract class ActiveMongo extends ActiveMongo_Events implements Iterator */ private static $_db; /** + * List of events handlers + * + * @type array + */ + static private $_events = array(); + /** + * List of global events handlers + * + * @type array + */ + static private $_super_events = array(); + /** * Host name * * @type string @@ -421,6 +431,103 @@ abstract class ActiveMongo extends ActiveMongo_Events implements Iterator // }}} + // EVENT HANDLERS {{{ + + // addEvent($action, $callback) {{{ + /** + * addEvent + * + */ + final static function addEvent($action, $callback) + { + if (!is_callable($callback)) { + throw new Exception("Invalid callback"); + } + + $class = get_called_class(); + if ($class == __CLASS__) { + $events = & self::$_super_events; + } else { + $events = & self::$_events[$class]; + } + if (!isset($events[$action])) { + $events[$action] = array(); + } + $events[$action][] = $callback; + return true; + } + // }}} + + // triggerEvent(string $event, Array $events_params) {{{ + final function triggerEvent($event, Array $events_params = array()) + { + $events = & self::$_events[get_class($this)][$event]; + $sevents = & self::$_super_events[$event]; + + if (!is_array($events_params)) { + return false; + } + + /* Super-Events handler receives the ActiveMongo class name as first param */ + $sevents_params = array_merge(array(get_class($this)), $events_params); + + foreach (array('events', 'sevents') as $event_type) { + if (count($$event_type) > 0) { + $params = "{$event_type}_params"; + foreach ($$event_type as $fnc) { + call_user_func_array($fnc, $$params); + } + } + } + + /* Some natives events are allowed to be called + * as methods, if they exists + */ + switch ($event) { + case 'before_create': + case 'before_update': + case 'before_validate': + case 'before_delete': + case 'after_create': + case 'after_update': + case 'after_validate': + case 'after_delete': + $fnc = array($this, $event); + $params = "events_params"; + if (is_callable($fnc)) { + call_user_func_array($fnc, $$params); + } + break; + } + } + // }}} + + // void runFilter(string $key, mixed &$value, mixed $past_value) {{{ + /** + * *Internal Method* + * + * This method check if the current document property has + * a filter method, if so, call it. + * + * If the filter returns false, throw an Exception. + * + * @return void + */ + protected function runFilter($key, &$value, $past_value) + { + $filter = array($this, "{$key}_filter"); + if (is_callable($filter)) { + $filter = call_user_func_array($filter, array(&$value, $past_value)); + if ($filter===false) { + throw new ActiveMongo_FilterException("{$key} filter failed"); + } + $this->$key = $value; + } + } + // }}} + + // }}} + // void setCursor(MongoCursor $obj) {{{ /** * Set Cursor @@ -1126,6 +1233,8 @@ abstract class ActiveMongo extends ActiveMongo_Events implements Iterator // }}} } +require_once dirname(__FILE__)."/Validators.php"; + /* * Local variables: * tab-width: 4 diff --git a/lib/Validators.php b/lib/Validators.php index 7849e80..33751db 100644 --- a/lib/Validators.php +++ b/lib/Validators.php @@ -36,21 +36,21 @@ */ // Valid Presence Of {{{ -ActiveMongo_Events::addEvent("before_validate_creation", function ($class, $obj) { +ActiveMongo::addEvent("before_validate_creation", function ($class, $obj) { if (isset($class::$validates_presence_of)) { foreach ((Array)$class::$validates_presence_of as $property) { if (!isset($obj[$property])) { - throw new FilterException("Missing required property {$property}"); + throw new ActiveMongo_FilterException("Missing required property {$property}"); } } } }); -ActiveMongo_Events::addEvent("before_validate_update", function ($class, $obj) { +ActiveMongo::addEvent("before_validate_update", function ($class, $obj) { if (isset($class::$validates_presence_of)) { foreach ((Array)$class::$validates_presence_of as $property) { if (isset($obj['$unset'][$property])) { - throw new FilterException("Cannot delete required property {$property}"); + throw new ActiveMongo_FilterException("Cannot delete required property {$property}"); } } } @@ -58,7 +58,7 @@ ActiveMongo_Events::addEvent("before_validate_update", function ($class, $obj) { // }}} // Valid Size Of / Valid Length Of {{{ -ActiveMongo_Events::addEvent("before_validate", function ($class, $obj) { +ActiveMongo::addEvent("before_validate", function ($class, $obj) { $validates = array(); if (isset($class::$validates_size_of)) { @@ -80,13 +80,13 @@ ActiveMongo_Events::addEvent("before_validate", function ($class, $obj) { if (isset($prop)) { if (isset($property['min']) && strlen($prop) < $property['min']) { - throw new FilterException("{$name} length is too short"); + throw new ActiveMongo_FilterException("{$name} length is too short"); } if (isset($property['is']) && strlen($prop) != $property['is']) { - throw new FilterException("{$name} length is different than expected"); + throw new ActiveMongo_FilterException("{$name} length is different than expected"); } if (isset($property['max']) && strlen($prop) > $property['max']) { - throw new FilterException("{$name} length is too large"); + throw new ActiveMongo_FilterException("{$name} length is too large"); } } } diff --git a/samples/blog/Author.php b/samples/blog/Author.php index 4d65a47..2d6e8cf 100644 --- a/samples/blog/Author.php +++ b/samples/blog/Author.php @@ -52,15 +52,15 @@ class AuthorModel extends ActiveMongo function username_filter($value, $old_value) { if ($old_value!=null && $value != $old_value) { - throw new FilterException("The username can't be changed"); + throw new ActiveMongo_FilterException("The username can't be changed"); } if (!preg_match("/[a-z][a-z0-9\-\_]+/", $value)) { - throw new FilterException("The username is not valid"); + throw new ActiveMongo_FilterException("The username is not valid"); } if (strlen($value) < 5) { - throw new FilterException("Username too short"); + throw new ActiveMongo_FilterException("Username too short"); } return true; diff --git a/samples/dynamic_references/User.php b/samples/dynamic_references/User.php index c0c494b..7220c83 100644 --- a/samples/dynamic_references/User.php +++ b/samples/dynamic_references/User.php @@ -10,7 +10,7 @@ class User extends ActiveMongo function password_filter(&$value, $old_value) { if (strlen($value) < 5) { - throw new FilterException("Password is too short"); + throw new ActiveMongo_FilterException("Password is too short"); } $value = sha1($value); } @@ -25,15 +25,15 @@ class User extends ActiveMongo function username_filter($value, $old_value) { if ($old_value!=null && $value != $old_value) { - throw new FilterException("The username can't be changed"); + throw new ActiveMongo_FilterException("The username can't be changed"); } if (!preg_match("/[a-z][a-z0-9\-\_]+/", $value)) { - throw new FilterException("The username is not valid"); + throw new ActiveMongo_FilterException("The username is not valid"); } if (strlen($value) < 5) { - throw new FilterException("Username too short"); + throw new ActiveMongo_FilterException("Username too short"); } return true; } diff --git a/samples/references/User.php b/samples/references/User.php index 8caf305..5738381 100644 --- a/samples/references/User.php +++ b/samples/references/User.php @@ -10,7 +10,7 @@ class User extends ActiveMongo function password_filter(&$value, $old_value) { if (strlen($value) < 5) { - throw new FilterException("Password is too short"); + throw new ActiveMongo_FilterException("Password is too short"); } $value = sha1($value); } @@ -25,15 +25,15 @@ class User extends ActiveMongo function username_filter($value, $old_value) { if ($old_value!=null && $value != $old_value) { - throw new FilterException("The username can't be changed"); + throw new ActiveMongo_FilterException("The username can't be changed"); } if (!preg_match("/[a-z][a-z0-9\-\_]+/", $value)) { - throw new FilterException("The username is not valid"); + throw new ActiveMongo_FilterException("The username is not valid"); } if (strlen($value) < 5) { - throw new FilterException("Username too short"); + throw new ActiveMongo_FilterException("Username too short"); } return true; } -- 2.11.4.GIT