From 2035024a4d563adb0dbf6ba42652843536fdce16 Mon Sep 17 00:00:00 2001 From: nawarian Date: Mon, 11 May 2015 21:13:49 -0300 Subject: [PATCH 1/9] EventedMapper structure --- library/Respect/Event/EventedMapper.php | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 library/Respect/Event/EventedMapper.php diff --git a/library/Respect/Event/EventedMapper.php b/library/Respect/Event/EventedMapper.php new file mode 100644 index 0000000..f6a3922 --- /dev/null +++ b/library/Respect/Event/EventedMapper.php @@ -0,0 +1,49 @@ +mapper = $mapper; + } + + public function __get($name) + { + return $this->mapper->__get($name); + } + + public function __set($alias, $collection) + { + return $this->mapper->__set($alias, $collection); + } + + public function __call($name, $children) + { + return $this->mapper->__call($name, $children); + } + + public function __isset($alias) + { + return $this->mapper->__isset($alias); + } + + public function flush() + { + $this->notifyPreFlush(); + $this->mapper->flush(); + $this->notifyPostFlush(); + } + + public function notifyPreFlush() + {} + + public function notifyPostFlush() + {} +} From 0aaa4e36969d5146247401bcad7b6fa046bb8a40 Mon Sep 17 00:00:00 2001 From: Rafael-BP Date: Mon, 11 May 2015 21:31:28 -0300 Subject: [PATCH 2/9] Creating EventManager structure --- library/Respect/Event/EventManager.php | 139 ++++++++++++++++++ .../Respect/Event/Interfaces/EventManager.php | 42 ++++++ library/Respect/Event/Interfaces/Listener.php | 16 ++ 3 files changed, 197 insertions(+) create mode 100644 library/Respect/Event/EventManager.php create mode 100644 library/Respect/Event/Interfaces/EventManager.php create mode 100644 library/Respect/Event/Interfaces/Listener.php diff --git a/library/Respect/Event/EventManager.php b/library/Respect/Event/EventManager.php new file mode 100644 index 0000000..ac8fa8d --- /dev/null +++ b/library/Respect/Event/EventManager.php @@ -0,0 +1,139 @@ + array( + * 0 => callable, + * 1 => concreteListener + * ... + * ) + * ... + * ) + */ + private $events; + + /** + * Construct + */ + public function __construct() + { + $this->events = array(); + } + + /** + * Add a callable or listener to a new or existent event + * @param string $eventName The event to attach the listener + * @param \Respect\Event\Interfaces\Listener|callable $action The event listener callback + * @return void + */ + public function on($eventName, $action) + { + switch (true) { + case ( ($action instanceof callable) || ($action instanceof Listener) ): + + // The block below you can use for control or remove it + $events = $this->getEvents(); + if (array_key_exists("attach", $events)) { + $this->dispatch("attach", array($eventName, $action)); + } + + $this->events[$eventName][] = $action; + break; + default: + $message = 'Invalid type of action provided for event manager'; + throw new \InvalidArgumentException($message, 400); + } + } + + /** + * Dispatch event + * + * If your event have more than one action, unless all the actions doesn't have + * any args, the args array need to be a array of arrays, each index of the + * args array needs to correspond to the index of the actions in the event + * array to work properly. + * + * @param string $eventName The event to be dispatched + * @param array $args Optional args for event actions + * + * @return void + */ + public function dispatch($eventName, $args = array()) + { + $events = $this->getEvents(); + if (array_key_exists($eventName, $events)) { + $argsIndex = 0; + foreach ($events[$eventName] as $action) { + switch (true) { + case $action instanceof callable: + if ( (count($events[$eventName]) > 1) && !empty($args) ) { + call_user_func_array($action, $args[$argsIndex]); + } else { + call_user_func_array($action, $args); + } + break; + case $action instanceof Listener: + if ( (count($events[$eventName]) > 1) && !empty($args) ) { + $action->update($args[$argsIndex]); + } else { + $action->update($args); + } + break; + default: + $message = 'Invalid type of action provided on event manager'; + throw new \InvalidArgumentException($message, 400); + } + $argsIndex++; + } + } + + } + + /** + * Return events array + * @return array + */ + public function getEvents() + { + return $this->events; + } + + /** + * Search and return event from the events array + * @param string $eventName the event name + * @return array + */ + public function getEvent($eventName) + { + $events = $this->getEvents(); + if (array_key_exists($eventName, $events)) { + return $events[$eventName]; + } + return null; + } + + /** + * Remove event from events array + * @param string $eventName The event to be removed + * @return void + */ + public function removeEvent($eventName) + { + $events = $this->getEvents(); + if (array_key_exists($eventName, $events)) { + $this->events = array_splice($events, $eventName, 1); + } + } + +} + diff --git a/library/Respect/Event/Interfaces/EventManager.php b/library/Respect/Event/Interfaces/EventManager.php new file mode 100644 index 0000000..56c2f37 --- /dev/null +++ b/library/Respect/Event/Interfaces/EventManager.php @@ -0,0 +1,42 @@ + Date: Mon, 11 May 2015 21:47:02 -0300 Subject: [PATCH 3/9] Updating namespaces --- .../Respect/Data/Event/.EventedMapper.php.swp | Bin 0 -> 12288 bytes library/Respect/Data/Event/EventManager.php | 139 ++++++++++++++++++ library/Respect/Data/Event/EventedMapper.php | 51 +++++++ .../Data/Event/Interfaces/EventManager.php | 42 ++++++ .../Data/Event/Interfaces/Listener.php | 16 ++ 5 files changed, 248 insertions(+) create mode 100644 library/Respect/Data/Event/.EventedMapper.php.swp create mode 100644 library/Respect/Data/Event/EventManager.php create mode 100644 library/Respect/Data/Event/EventedMapper.php create mode 100644 library/Respect/Data/Event/Interfaces/EventManager.php create mode 100644 library/Respect/Data/Event/Interfaces/Listener.php diff --git a/library/Respect/Data/Event/.EventedMapper.php.swp b/library/Respect/Data/Event/.EventedMapper.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..2759b08065443d1d0d016ba6f8ca16be22e97343 GIT binary patch literal 12288 zcmeI2&ui2`6vtnC)v73ZlxP>b6}PEYP^eY}6+{ZQS`SiKW|LVrl1;+QL@C8zJyp*h zLyu86z-Z4222eNQCYk8TqktRW{=K!U#7EYD%cNBHX=TFJ45X$@bEp6iFi0 zNYY5Ut$@eaz&1CKntJ8*0F9kEK5ADwc=(XBZ|}@DUoylLI&RZT}|ulYR@hUHQun(~-a@)|+t zD;aH4=>PZhTCu$zTQLqp^n2skZc=ljyLzp9%UZO{+@wxBt~2wRcfmR9l(M&?+q15s zwLQ{CrJgCDui-#=CTmPD?#^Fe^u-%!kbZG}oM@iBP-yM6+S-eTcA`{EC-0J array( + * 0 => callable, + * 1 => concreteListener + * ... + * ) + * ... + * ) + */ + private $events; + + /** + * Construct + */ + public function __construct() + { + $this->events = array(); + } + + /** + * Add a callable or listener to a new or existent event + * @param string $eventName The event to attach the listener + * @param \Respect\Event\Interfaces\Listener|callable $action The event listener callback + * @return void + */ + public function on($eventName, $action) + { + switch (true) { + case ( ($action instanceof \callable) || ($action instanceof Listener) ): + + // The block below you can use for control or remove it + $events = $this->getEvents(); + if (array_key_exists("attach", $events)) { + $this->dispatch("attach", array($eventName, $action)); + } + + $this->events[$eventName][] = $action; + break; + default: + $message = 'Invalid type of action provided for event manager'; + throw new \InvalidArgumentException($message, 400); + } + } + + /** + * Dispatch event + * + * If your event have more than one action, unless all the actions doesn't have + * any args, the args array need to be a array of arrays, each index of the + * args array needs to correspond to the index of the actions in the event + * array to work properly. + * + * @param string $eventName The event to be dispatched + * @param array $args Optional args for event actions + * + * @return void + */ + public function dispatch($eventName, $args = array()) + { + $events = $this->getEvents(); + if (array_key_exists($eventName, $events)) { + $argsIndex = 0; + foreach ($events[$eventName] as $action) { + switch (true) { + case $action instanceof callable: + if ( (count($events[$eventName]) > 1) && !empty($args) ) { + call_user_func_array($action, $args[$argsIndex]); + } else { + call_user_func_array($action, $args); + } + break; + case $action instanceof Listener: + if ( (count($events[$eventName]) > 1) && !empty($args) ) { + $action->update($args[$argsIndex]); + } else { + $action->update($args); + } + break; + default: + $message = 'Invalid type of action provided on event manager'; + throw new \InvalidArgumentException($message, 400); + } + $argsIndex++; + } + } + + } + + /** + * Return events array + * @return array + */ + public function getEvents() + { + return $this->events; + } + + /** + * Search and return event from the events array + * @param string $eventName the event name + * @return array + */ + public function getEvent($eventName) + { + $events = $this->getEvents(); + if (array_key_exists($eventName, $events)) { + return $events[$eventName]; + } + return null; + } + + /** + * Remove event from events array + * @param string $eventName The event to be removed + * @return void + */ + public function removeEvent($eventName) + { + $events = $this->getEvents(); + if (array_key_exists($eventName, $events)) { + $this->events = array_splice($events, $eventName, 1); + } + } + +} + diff --git a/library/Respect/Data/Event/EventedMapper.php b/library/Respect/Data/Event/EventedMapper.php new file mode 100644 index 0000000..9818ade --- /dev/null +++ b/library/Respect/Data/Event/EventedMapper.php @@ -0,0 +1,51 @@ +mapper = $mapper; + } + + public function __get($name) + { + return $this->mapper->__get($name); + } + + public function __set($alias, $collection) + { + return $this->mapper->__set($alias, $collection); + } + + public function __call($name, $children) + { + return $this->mapper->__call($name, $children); + } + + public function __isset($alias) + { + return $this->mapper->__isset($alias); + } + + public function flush() + { + $this->notifyPreFlush(); + $this->mapper->flush(); + $this->notifyPostFlush(); + } + + public function notifyPreFlush() + {} + + public function notifyPostFlush() + { + die('hey'); + } +} diff --git a/library/Respect/Data/Event/Interfaces/EventManager.php b/library/Respect/Data/Event/Interfaces/EventManager.php new file mode 100644 index 0000000..93e9121 --- /dev/null +++ b/library/Respect/Data/Event/Interfaces/EventManager.php @@ -0,0 +1,42 @@ + Date: Mon, 11 May 2015 21:48:45 -0300 Subject: [PATCH 4/9] Fixing some missing files --- .../Respect/Data/Event/.EventedMapper.php.swp | Bin 12288 -> 0 bytes library/Respect/Data/Event/EventedMapper.php | 4 +- library/Respect/Event/EventManager.php | 139 ------------------ library/Respect/Event/EventedMapper.php | 49 ------ .../Respect/Event/Interfaces/EventManager.php | 42 ------ library/Respect/Event/Interfaces/Listener.php | 16 -- 6 files changed, 1 insertion(+), 249 deletions(-) delete mode 100644 library/Respect/Data/Event/.EventedMapper.php.swp delete mode 100644 library/Respect/Event/EventManager.php delete mode 100644 library/Respect/Event/EventedMapper.php delete mode 100644 library/Respect/Event/Interfaces/EventManager.php delete mode 100644 library/Respect/Event/Interfaces/Listener.php diff --git a/library/Respect/Data/Event/.EventedMapper.php.swp b/library/Respect/Data/Event/.EventedMapper.php.swp deleted file mode 100644 index 2759b08065443d1d0d016ba6f8ca16be22e97343..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&ui2`6vtnC)v73ZlxP>b6}PEYP^eY}6+{ZQS`SiKW|LVrl1;+QL@C8zJyp*h zLyu86z-Z4222eNQCYk8TqktRW{=K!U#7EYD%cNBHX=TFJ45X$@bEp6iFi0 zNYY5Ut$@eaz&1CKntJ8*0F9kEK5ADwc=(XBZ|}@DUoylLI&RZT}|ulYR@hUHQun(~-a@)|+t zD;aH4=>PZhTCu$zTQLqp^n2skZc=ljyLzp9%UZO{+@wxBt~2wRcfmR9l(M&?+q15s zwLQ{CrJgCDui-#=CTmPD?#^Fe^u-%!kbZG}oM@iBP-yM6+S-eTcA`{EC-0J array( - * 0 => callable, - * 1 => concreteListener - * ... - * ) - * ... - * ) - */ - private $events; - - /** - * Construct - */ - public function __construct() - { - $this->events = array(); - } - - /** - * Add a callable or listener to a new or existent event - * @param string $eventName The event to attach the listener - * @param \Respect\Event\Interfaces\Listener|callable $action The event listener callback - * @return void - */ - public function on($eventName, $action) - { - switch (true) { - case ( ($action instanceof callable) || ($action instanceof Listener) ): - - // The block below you can use for control or remove it - $events = $this->getEvents(); - if (array_key_exists("attach", $events)) { - $this->dispatch("attach", array($eventName, $action)); - } - - $this->events[$eventName][] = $action; - break; - default: - $message = 'Invalid type of action provided for event manager'; - throw new \InvalidArgumentException($message, 400); - } - } - - /** - * Dispatch event - * - * If your event have more than one action, unless all the actions doesn't have - * any args, the args array need to be a array of arrays, each index of the - * args array needs to correspond to the index of the actions in the event - * array to work properly. - * - * @param string $eventName The event to be dispatched - * @param array $args Optional args for event actions - * - * @return void - */ - public function dispatch($eventName, $args = array()) - { - $events = $this->getEvents(); - if (array_key_exists($eventName, $events)) { - $argsIndex = 0; - foreach ($events[$eventName] as $action) { - switch (true) { - case $action instanceof callable: - if ( (count($events[$eventName]) > 1) && !empty($args) ) { - call_user_func_array($action, $args[$argsIndex]); - } else { - call_user_func_array($action, $args); - } - break; - case $action instanceof Listener: - if ( (count($events[$eventName]) > 1) && !empty($args) ) { - $action->update($args[$argsIndex]); - } else { - $action->update($args); - } - break; - default: - $message = 'Invalid type of action provided on event manager'; - throw new \InvalidArgumentException($message, 400); - } - $argsIndex++; - } - } - - } - - /** - * Return events array - * @return array - */ - public function getEvents() - { - return $this->events; - } - - /** - * Search and return event from the events array - * @param string $eventName the event name - * @return array - */ - public function getEvent($eventName) - { - $events = $this->getEvents(); - if (array_key_exists($eventName, $events)) { - return $events[$eventName]; - } - return null; - } - - /** - * Remove event from events array - * @param string $eventName The event to be removed - * @return void - */ - public function removeEvent($eventName) - { - $events = $this->getEvents(); - if (array_key_exists($eventName, $events)) { - $this->events = array_splice($events, $eventName, 1); - } - } - -} - diff --git a/library/Respect/Event/EventedMapper.php b/library/Respect/Event/EventedMapper.php deleted file mode 100644 index f6a3922..0000000 --- a/library/Respect/Event/EventedMapper.php +++ /dev/null @@ -1,49 +0,0 @@ -mapper = $mapper; - } - - public function __get($name) - { - return $this->mapper->__get($name); - } - - public function __set($alias, $collection) - { - return $this->mapper->__set($alias, $collection); - } - - public function __call($name, $children) - { - return $this->mapper->__call($name, $children); - } - - public function __isset($alias) - { - return $this->mapper->__isset($alias); - } - - public function flush() - { - $this->notifyPreFlush(); - $this->mapper->flush(); - $this->notifyPostFlush(); - } - - public function notifyPreFlush() - {} - - public function notifyPostFlush() - {} -} diff --git a/library/Respect/Event/Interfaces/EventManager.php b/library/Respect/Event/Interfaces/EventManager.php deleted file mode 100644 index 56c2f37..0000000 --- a/library/Respect/Event/Interfaces/EventManager.php +++ /dev/null @@ -1,42 +0,0 @@ - Date: Mon, 11 May 2015 21:56:02 -0300 Subject: [PATCH 5/9] Fixing some syntax on EventManager --- library/Respect/Data/Event/EventManager.php | 8 ++++---- library/Respect/Data/Event/Interfaces/EventManager.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/Respect/Data/Event/EventManager.php b/library/Respect/Data/Event/EventManager.php index 40df4af..1c437cc 100644 --- a/library/Respect/Data/Event/EventManager.php +++ b/library/Respect/Data/Event/EventManager.php @@ -1,6 +1,6 @@ getEvents(); @@ -75,7 +75,7 @@ public function dispatch($eventName, $args = array()) $argsIndex = 0; foreach ($events[$eventName] as $action) { switch (true) { - case $action instanceof callable: + case is_callable($action): if ( (count($events[$eventName]) > 1) && !empty($args) ) { call_user_func_array($action, $args[$argsIndex]); } else { diff --git a/library/Respect/Data/Event/Interfaces/EventManager.php b/library/Respect/Data/Event/Interfaces/EventManager.php index 93e9121..cb0ac80 100644 --- a/library/Respect/Data/Event/Interfaces/EventManager.php +++ b/library/Respect/Data/Event/Interfaces/EventManager.php @@ -16,7 +16,7 @@ public function dispatch($eventName, $args = array()); /** * Add action (Listener or callable) for a event * @param string $eventName - * @param \Respect\Event\Interfaces\Listener|callable $action + * @param \Respect\Data\Event\Interfaces\Listener|\callable $action */ public function on($eventName, $action); From 0fa906f3fa18b554f2f8aff9709a49b605050f9f Mon Sep 17 00:00:00 2001 From: nawarian Date: Mon, 11 May 2015 22:54:29 -0300 Subject: [PATCH 6/9] Finishing event dispatching under EventedMapper --- library/Respect/Data/Event/EventedMapper.php | 68 ++++++++++++++++++-- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/library/Respect/Data/Event/EventedMapper.php b/library/Respect/Data/Event/EventedMapper.php index 4f26b28..7d2dc1c 100644 --- a/library/Respect/Data/Event/EventedMapper.php +++ b/library/Respect/Data/Event/EventedMapper.php @@ -9,9 +9,12 @@ class EventedMapper protected $mapper; + protected $eventManager; + public function __construct(AbstractMapper $mapper) { $this->mapper = $mapper; + $this->eventManager = new EventManager(); } public function __get($name) @@ -34,16 +37,69 @@ public function __isset($alias) return $this->mapper->__isset($alias); } + public function on($event, $callback) + { + return $this->eventManager->on($event, $callback); + } + public function flush() { - $this->notifyPreFlush(); + $trackedQueue = $this->getTrackedQueue(); + $trackedEntities = $this->getTrackedEntities(); + + $this->processFlushQueue($trackedQueue, $trackedEntities, 'pre'); $this->mapper->flush(); - $this->notifyPostFlush(); + $this->processFlushQueue($trackedQueue, $trackedEntities, 'post'); + } + + protected function processFlushQueue($queue, $trackedEntities, $eventSuffix) + { + $em = $this->eventManager; + + foreach ($queue as $eventType => $objects) { + foreach ($objects as $entity) { + $collection = $trackedEntities[$entity]; + + $em->dispatch( + "{$collection->getName()}:{$eventType}:{$eventSuffix}", + array($entity, $collection) + ); + } + } } - public function notifyPreFlush() - {} + protected function getTrackedQueue() + { + $m = $this->mapper; + $inserts = $this->getObjectPropertyFromReflection($m, 'new'); + $updates = $this->getObjectPropertyFromReflection($m, 'changed'); + $deletes = $this->getObjectPropertyFromReflection($m, 'removed'); - public function notifyPostFlush() - {} + return array( + 'insert' => $inserts, + 'update' => $updates, + 'delete' => $deletes + ); + } + + protected function getTrackedEntities() + { + return $this->getObjectPropertyFromReflection( + $this->mapper, + 'tracked' + ); + } + + private function getObjectPropertyFromReflection( + $object, + $property + ) { + $ref = new \ReflectionObject($object); + $refProp = $ref->getProperty($property); + if ($refProp->isPrivate() || $refProp->isProtected()) { + $refProp->setAccessible(true); + } + + return $refProp->getValue($object); + } } From 202e6cec511677698845d3efb52d224c4412cb5f Mon Sep 17 00:00:00 2001 From: nawarian Date: Mon, 11 May 2015 22:57:16 -0300 Subject: [PATCH 7/9] Making sure that none Mapper method has changed under this implementation --- library/Respect/Data/Event/EventedMapper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/Respect/Data/Event/EventedMapper.php b/library/Respect/Data/Event/EventedMapper.php index 7d2dc1c..d0cc6ae 100644 --- a/library/Respect/Data/Event/EventedMapper.php +++ b/library/Respect/Data/Event/EventedMapper.php @@ -48,8 +48,10 @@ public function flush() $trackedEntities = $this->getTrackedEntities(); $this->processFlushQueue($trackedQueue, $trackedEntities, 'pre'); - $this->mapper->flush(); + $flushResult = $this->mapper->flush(); $this->processFlushQueue($trackedQueue, $trackedEntities, 'post'); + + return $flushResult; } protected function processFlushQueue($queue, $trackedEntities, $eventSuffix) From 444406ae3eb6b6905321d9dbd87bcbc57de28a35 Mon Sep 17 00:00:00 2001 From: nawarian Date: Mon, 11 May 2015 23:05:56 -0300 Subject: [PATCH 8/9] Adding removeEvent from EventManager inside EventedMapper --- library/Respect/Data/Event/EventedMapper.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/Respect/Data/Event/EventedMapper.php b/library/Respect/Data/Event/EventedMapper.php index d0cc6ae..8d60973 100644 --- a/library/Respect/Data/Event/EventedMapper.php +++ b/library/Respect/Data/Event/EventedMapper.php @@ -42,6 +42,11 @@ public function on($event, $callback) return $this->eventManager->on($event, $callback); } + public function removeEvent($eventName) + { + return $this->eventManager->removeEvent($eventName); + } + public function flush() { $trackedQueue = $this->getTrackedQueue(); From da3cde8fccebc5dde34e9ee2e45a4f692e7d73f6 Mon Sep 17 00:00:00 2001 From: Rafael BP Date: Thu, 21 May 2015 14:16:24 -0300 Subject: [PATCH 9/9] Changing on function to 'if' --- library/Respect/Data/Event/EventManager.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/library/Respect/Data/Event/EventManager.php b/library/Respect/Data/Event/EventManager.php index 1c437cc..c9feaec 100644 --- a/library/Respect/Data/Event/EventManager.php +++ b/library/Respect/Data/Event/EventManager.php @@ -38,21 +38,18 @@ public function __construct() */ public function on($eventName, $action) { - switch (true) { - case ( (is_callable($action)) || ($action instanceof Listener) ): - - // The block below you can use for control or remove it - $events = $this->getEvents(); - if (array_key_exists("attach", $events)) { - $this->dispatch("attach", array($eventName, $action)); - } - - $this->events[$eventName][] = $action; - break; - default: + if (!(is_callable($action)) || !($action instanceof Listener)) { $message = 'Invalid type of action provided for event manager'; throw new \InvalidArgumentException($message, 400); } + + // The block below you can use for control or remove it + $events = $this->getEvents(); + if (array_key_exists("attach", $events)) { + $this->dispatch("attach", array($eventName, $action)); + } + + $this->events[$eventName][] = $action; } /**