Skip to content

Commit

Permalink
Made it compatible to Propel2 alpha 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc J. Schmidt committed Nov 14, 2014
1 parent 2b64c84 commit 7cd46fb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 43 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ EventDispatcherBehavior

[![Build Status](https://secure.travis-ci.org/willdurand/EventDispatcherBehavior.png?branch=master)](http://travis-ci.org/willdurand/EventDispatcherBehavior)

Integrate the Symfony2 [EventDispatcher](https://github.com/symfony/EventDispatcher) component in your Model classes.
Integrate the Symfony2 [EventDispatcher](https://github.com/symfony/EventDispatcher) component in your Model classes for Propel 2 alpha 3.
Final Propel 2 release will have this integrated into core. This is only for guys using Propel 2 already till alpha 3 and need those behavior.


### Installation ###
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "willdurand/propel-eventdispatcher-behavior",
"description": "Integrates the Symfony2 EventDispatcher component in your Model classes. ",
"keywords": [ "propel", "behavior", "event dispatcher", "event" ],
"name": "marcj/propel-eventdispatcher-behavior",
"description": "Integrates the Symfony2 EventDispatcher component in your Model classes for Propel2 alpha 3. ",
"keywords": [ "propel2", "behavior", "event dispatcher", "event" ],
"license": "MIT",
"require": {
"symfony/event-dispatcher": "~2.1",
"propel/propel1": "~1.6"
"symfony/event-dispatcher": "~2.1"
},
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/EventDispatcherBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @author William Durand <[email protected]>
*/
class EventDispatcherBehavior extends Behavior
class EventDispatcherBehavior extends \Propel\Generator\Model\Behavior
{
/**
* @var EventDispatcherObjectBuilderModifier
Expand Down
14 changes: 3 additions & 11 deletions src/EventDispatcherObjectBuilderModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EventDispatcherObjectBuilderModifier
{
private $behavior;

public function __construct(Behavior $behavior)
public function __construct(\Propel\Generator\Model\Behavior $behavior)
{
$this->behavior = $behavior;
}
Expand Down Expand Up @@ -73,14 +73,6 @@ public function addConstructHook()
)) . ' ';
}

public function postHydrate()
{
return $this->behavior->renderTemplate('objectHook', array(
'eventName' => $this->getEventName('post_hydrate'),
'withConnection' => false,
));
}

public function preSave()
{
return $this->behavior->renderTemplate('objectHook', array(
Expand Down Expand Up @@ -147,14 +139,14 @@ public function postDelete()

public function objectFilter(&$script)
{
$script = preg_replace('#(implements Persistent)#', '$1, EventDispatcherAwareModelInterface', $script);
$script = preg_replace('#(implements ActiveRecordInterface)#', '$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 = new \Propel\Generator\Util\PhpParser($script, true);
$parser->removeMethod('dummy_construct');
$oldCode = $parser->findMethod('__construct');
$newCode = substr_replace($oldCode, $this->addConstructHook() . '}', strrpos($oldCode, '}'));
Expand Down
22 changes: 5 additions & 17 deletions tests/EventDispatcherBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function setUp()
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<column name="name" type="VARCHAR" required="true" />
<behavior name="event_dispatcher" />
<behavior name="\EventDispatcherBehavior" />
</table>
</database>
EOF
Expand All @@ -28,17 +28,16 @@ public function setUp()
<column name="text" type="VARCHAR" required="true" />
<column name="allowed" type="boolean" required="true" defaultValue="false" />
<behavior name="event_dispatcher" />
<behavior name="\EventDispatcherBehavior" />
</table>
</database>
EOF
);

foreach ($tables as $className => $schema) {
if (!class_exists($className)) {
$builder = new PropelQuickBuilder();
$builder = new \Propel\Generator\Util\QuickBuilder();
$config = $builder->getConfig();
$config->setBuildProperty('behavior.event_dispatcher.class', '../src/EventDispatcherBehavior');
$builder->setConfig($config);
$builder->setSchema($schema);

Expand Down Expand Up @@ -77,19 +76,10 @@ 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());
$that->assertFalse($event->hasArgument('connection'));
});

Post::getEventDispatcher()->addListener(Post::EVENT_CONSTRUCT, function (Event $event) use (& $postConstructFired, $that) {
$postConstructFired = true;

Expand All @@ -111,29 +101,27 @@ public function testFireEvent()

$that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
$that->assertInstanceOf('Post', $event->getSubject());
$that->assertInstanceOf('PropelPDO', $event->getArgument('connection'));
$that->assertInstanceOf('Propel\Runtime\Connection\ConnectionInterface', $event->getArgument('connection'));
});

Post::getEventDispatcher()->addListener(Post::EVENT_POST_SAVE, function (Event $event) use (& $postSaveFired, $that) {
$postSaveFired = true;

$that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
$that->assertInstanceOf('Post', $event->getSubject());
$that->assertInstanceOf('PropelPDO', $event->getArgument('connection'));
$that->assertInstanceOf('Propel\Runtime\Connection\ConnectionInterface', $event->getArgument('connection'));
});

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);
}
Expand Down
9 changes: 4 additions & 5 deletions tests/EventDispatcherBehaviorWithNamespacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public function setUp()
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<column name="name" type="VARCHAR" required="true" />
<behavior name="event_dispatcher" />
<behavior name="\EventDispatcherBehavior" />
</table>
</database>
EOF;

$builder = new PropelQuickBuilder();
$builder = new \Propel\Generator\Util\QuickBuilder();
$config = $builder->getConfig();
$config->setBuildProperty('behavior.event_dispatcher.class', '../src/EventDispatcherBehavior');
$builder->setConfig($config);
$builder->setSchema($schema);

Expand Down Expand Up @@ -66,15 +65,15 @@ public function testFireEvent()

$that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
$that->assertInstanceOf('My\Post', $event->getSubject());
$that->assertInstanceOf('PropelPDO', $event->getArgument('connection'));
$that->assertInstanceOf('Propel\Runtime\Connection\ConnectionInterface', $event->getArgument('connection'));
});

Post::getEventDispatcher()->addListener(Post::EVENT_POST_SAVE, function (Event $event) use (& $postSaveFired, $that) {
$postSaveFired = true;

$that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
$that->assertInstanceOf('My\Post', $event->getSubject());
$that->assertInstanceOf('PropelPDO', $event->getArgument('connection'));
$that->assertInstanceOf('Propel\Runtime\Connection\ConnectionInterface', $event->getArgument('connection'));
});

$post = new Post();
Expand Down
3 changes: 0 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
set_include_path(__DIR__ . '/../vendor/phing/phing/classes' . PATH_SEPARATOR . get_include_path());

require_once __DIR__ . '/../vendor/propel/propel1/generator/lib/util/PropelQuickBuilder.php';

0 comments on commit 7cd46fb

Please sign in to comment.