From debe5318b42e89fc0609631f3803f6622005c4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Thu, 19 Dec 2013 18:20:44 +0100 Subject: [PATCH] Added a post_hydrate hook --- src/EventDispatcherObjectBuilderModifier.php | 8 ++++++++ tests/EventDispatcherBehaviorTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/EventDispatcherObjectBuilderModifier.php b/src/EventDispatcherObjectBuilderModifier.php index e47c0dd..8546bba 100644 --- a/src/EventDispatcherObjectBuilderModifier.php +++ b/src/EventDispatcherObjectBuilderModifier.php @@ -17,6 +17,7 @@ public function objectAttributes($builder) $events = array(); foreach (array( 'construct', + 'post_hydrate', 'pre_save', 'post_save', 'pre_update', 'post_update', 'pre_insert', 'post_insert', @@ -71,6 +72,13 @@ public function addConstructHook() )) . ' '; } + public function postHydrate() + { + return $this->behavior->renderTemplate('objectHook', array( + 'eventName' => $this->getEventName('post_hydrate'), + )); + } + public function preSave() { return $this->behavior->renderTemplate('objectHook', array( diff --git a/tests/EventDispatcherBehaviorTest.php b/tests/EventDispatcherBehaviorTest.php index 748b2f4..84f949d 100644 --- a/tests/EventDispatcherBehaviorTest.php +++ b/tests/EventDispatcherBehaviorTest.php @@ -60,6 +60,7 @@ public function testObjectMethods() $this->assertTrue(defined('Post::EVENT_PRE_DELETE')); $this->assertTrue(defined('Post::EVENT_POST_DELETE')); $this->assertTrue(defined('Post::EVENT_CONSTRUCT')); + $this->assertTrue(defined('Post::EVENT_POST_HYDRATE')); } public function testGetDispatcher() @@ -76,10 +77,18 @@ public function testFireEvent() $preSaveFired = false; $postSaveFired = false; $postConstructFired = false; + $postHydrateFired = false; $threadConstructFired = false; $that = $this; + Post::getEventDispatcher()->addListener(Post::EVENT_POST_HYDRATE, function (Event $event) use (& $postHydrateFired, $that) { + $postHydrateFired = true; + + $that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); + $that->assertInstanceOf('Post', $event->getSubject()); + }); + Post::getEventDispatcher()->addListener(Post::EVENT_CONSTRUCT, function (Event $event) use (& $postConstructFired, $that) { $postConstructFired = true; @@ -111,12 +120,15 @@ public function testFireEvent() new Thread(); $this->assertTrue($threadConstructFired); + $post = new Post(); $this->assertTrue($postConstructFired); $post->setName('a-name'); $post->save(); + $post->reload(); + $this->assertTrue($postHydrateFired); $this->assertTrue($preSaveFired); $this->assertTrue($postSaveFired); }