Skip to content

Commit

Permalink
Added construct event 'propel.construct'
Browse files Browse the repository at this point in the history
  • Loading branch information
ienzam committed Jul 24, 2013
1 parent a509cd4 commit e3a2286
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
26 changes: 26 additions & 0 deletions src/EventDispatcherObjectBuilderModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -39,6 +40,7 @@ public function objectMethods($builder)
$script = '';
$script .= $this->addGetEventDispatcher($builder);
$script .= $this->addSetEventDispatcher($builder);
$script .= $this->addDummyConstruct();

return $script;
}
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/templates/objectDummyConstruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

// event_dispatcher behavior
public function dummy_construct()
{
parent::__construct();
}
59 changes: 49 additions & 10 deletions tests/EventDispatcherBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class EventDispatcherBehaviorTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Post')) {
$schema = <<<EOF
$tables = array(
'Post' => <<<EOF
<database name="event_dispatcher_behavior" defaultIdMethod="native">
<table name="post">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
Expand All @@ -19,15 +19,31 @@ public function setUp()
<behavior name="event_dispatcher" />
</table>
</database>
EOF;

$builder = new PropelQuickBuilder();
$config = $builder->getConfig();
$config->setBuildProperty('behavior.event_dispatcher.class', '../src/EventDispatcherBehavior');
$builder->setConfig($config);
$builder->setSchema($schema);
EOF
,
'Thread' => <<<EOF
<database name="event_dispatcher_behavior2" defaultIdMethod="native">
<table name="thread">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<column name="text" type="VARCHAR" required="true" />
<column name="allowed" type="boolean" required="true" defaultValue="false" />
$builder->build();
<behavior name="event_dispatcher" />
</table>
</database>
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();
}
}
}

Expand All @@ -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()
Expand All @@ -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;

Expand All @@ -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();

Expand Down

0 comments on commit e3a2286

Please sign in to comment.