From e3a2286ac92b5c1841cef357c84927ea9edbba61 Mon Sep 17 00:00:00 2001 From: "Md. Enzam Hossain" Date: Wed, 24 Jul 2013 12:57:35 +0600 Subject: [PATCH] Added construct event 'propel.construct' --- src/EventDispatcherObjectBuilderModifier.php | 26 +++++++++ src/templates/objectDummyConstruct.php | 6 ++ tests/EventDispatcherBehaviorTest.php | 59 ++++++++++++++++---- 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 src/templates/objectDummyConstruct.php diff --git a/src/EventDispatcherObjectBuilderModifier.php b/src/EventDispatcherObjectBuilderModifier.php index 071e19f..805ad6a 100644 --- a/src/EventDispatcherObjectBuilderModifier.php +++ b/src/EventDispatcherObjectBuilderModifier.php @@ -16,6 +16,7 @@ public function objectAttributes($builder) { $events = array(); foreach (array( + 'construct', 'pre_save', 'post_save', 'pre_update', 'post_update', 'pre_insert', 'post_insert', @@ -39,6 +40,7 @@ public function objectMethods($builder) $script = ''; $script .= $this->addGetEventDispatcher($builder); $script .= $this->addSetEventDispatcher($builder); + $script .= $this->addDummyConstruct(); return $script; } @@ -57,6 +59,18 @@ public function addSetEventDispatcher($builder) return $this->behavior->renderTemplate('objectSetEventDispatcher'); } + public function addDummyConstruct() + { + return $this->behavior->renderTemplate('objectDummyConstruct'); + } + + public function addConstructHook() + { + return ' ' . $this->behavior->renderTemplate('objectHook', array( + 'eventName' => $this->getEventName('construct'), + )) . ' '; + } + public function preSave() { return $this->behavior->renderTemplate('objectHook', array( @@ -116,6 +130,18 @@ public function postDelete() public function objectFilter(&$script) { $script = preg_replace('#(implements Persistent)#', '$1, EventDispatcherAwareModelInterface', $script); + + // rename the dummy_construct to __construct if __construct does not exists + if(strpos($script, 'function __construct') === false) { + $script = str_replace('function dummy_construct', 'function __construct', $script); + } + + $parser = new PropelPHPParser($script, true); + $parser->removeMethod('dummy_construct'); + $oldCode = $parser->findMethod('__construct'); + $newCode = substr_replace($oldCode, $this->addConstructHook() . '}', strrpos($oldCode, '}')); + $parser->replaceMethod('__construct', $newCode); + $script = $parser->getCode(); } protected function getEventName($eventName) diff --git a/src/templates/objectDummyConstruct.php b/src/templates/objectDummyConstruct.php new file mode 100644 index 0000000..b9a6a0d --- /dev/null +++ b/src/templates/objectDummyConstruct.php @@ -0,0 +1,6 @@ + +// event_dispatcher behavior +public function dummy_construct() +{ + parent::__construct(); +} diff --git a/tests/EventDispatcherBehaviorTest.php b/tests/EventDispatcherBehaviorTest.php index b94e592..c6c9683 100644 --- a/tests/EventDispatcherBehaviorTest.php +++ b/tests/EventDispatcherBehaviorTest.php @@ -9,8 +9,8 @@ class EventDispatcherBehaviorTest extends \PHPUnit_Framework_TestCase { public function setUp() { - if (!class_exists('Post')) { - $schema = << << @@ -19,15 +19,31 @@ public function setUp()
-EOF; - - $builder = new PropelQuickBuilder(); - $config = $builder->getConfig(); - $config->setBuildProperty('behavior.event_dispatcher.class', '../src/EventDispatcherBehavior'); - $builder->setConfig($config); - $builder->setSchema($schema); +EOF + , + 'Thread' => << + + + + - $builder->build(); + +
+ +EOF + ); + + foreach($tables as $className => $schema) { + if (!class_exists($className)) { + $builder = new PropelQuickBuilder(); + $config = $builder->getConfig(); + $config->setBuildProperty('behavior.event_dispatcher.class', '../src/EventDispatcherBehavior'); + $builder->setConfig($config); + $builder->setSchema($schema); + + $builder->build(); + } } } @@ -43,6 +59,7 @@ public function testObjectMethods() $this->assertTrue(defined('Post::EVENT_POST_INSERT')); $this->assertTrue(defined('Post::EVENT_PRE_DELETE')); $this->assertTrue(defined('Post::EVENT_POST_DELETE')); + $this->assertTrue(defined('Post::EVENT_CONSTRUCT')); } public function testGetDispatcher() @@ -58,8 +75,25 @@ public function testFireEvent() { $preSaveFired = false; $postSaveFired = false; + $postConstructFired = false; + $threadConstructFired = false; $that = $this; + + Post::getEventDispatcher()->addListener(Post::EVENT_CONSTRUCT, function (Event $event) use (& $postConstructFired, $that) { + $postConstructFired = true; + + $that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); + $that->assertInstanceOf('Post', $event->getSubject()); + }); + + Thread::getEventDispatcher()->addListener(Thread::EVENT_CONSTRUCT, function (Event $event) use (& $threadConstructFired, $that) { + $threadConstructFired = true; + + $that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); + $that->assertInstanceOf('Thread', $event->getSubject()); + }); + Post::getEventDispatcher()->addListener(Post::EVENT_PRE_SAVE, function (Event $event) use (& $preSaveFired, $that) { $preSaveFired = true; @@ -74,7 +108,12 @@ public function testFireEvent() $that->assertInstanceOf('Post', $event->getSubject()); }); + new Thread(); + $this->assertTrue($threadConstructFired); + $post = new Post(); + $this->assertTrue($postConstructFired); + $post->setName('a-name'); $post->save();