willdurand/propel-eventdispatcher-behavior
Propel behavior that integrates Symfony’s EventDispatcher into your model classes. Add it to your Propel config and schema to auto-generate an EventDispatcher per ActiveRecord class via getEventDispatcher(), enabling event-driven hooks in models.
Install the Package
Add to composer.json:
"require": {
"willdurand/propel-eventdispatcher-behavior": "dev-master"
}
Run composer update.
Configure Propel
Add to propel.ini or build.properties:
propel.behavior.eventdispatcher.class = vendor.willdurand.propel-eventdispatcher-behavior.src.EventDispatcherBehavior
Apply to a Model
In schema.xml, add the behavior to a <table>:
<behavior name="event_dispatcher" />
First Use Case Dispatch an event in a model method:
$this->getEventDispatcher()->dispatch('user.created', new UserEvent($this));
Event-Driven Model Logic
Use getEventDispatcher() to trigger events in model methods (e.g., save(), delete()):
public function save() {
$this->getEventDispatcher()->dispatch('user.pre_save', $this);
parent::save();
$this->getEventDispatcher()->dispatch('user.post_save', $this);
}
Listener Registration Register listeners in a service provider or bootstrap file:
$dispatcher = $this->getEventDispatcher();
$dispatcher->addListener('user.created', function ($event) {
// Handle user creation
});
Event Objects
Create custom event classes (e.g., UserEvent) extending Event:
class UserEvent extends Event {
public function __construct(private UserModel $user) {}
public function getUser() { return $this->user; }
}
Global vs. Instance Dispatchers
Propel Lifecycle Hooks
Combine with Propel’s preInsert, postUpdate, etc., for granular control:
<table name="user">
<behavior name="event_dispatcher" />
<column name="name" type="VARCHAR" behavior="[preInsert, postUpdate]"/>
</table>
Singleton Dispatcher
Propel Schema Updates
Circular Dependencies
user.updated listener modifying the user again.Performance
Listener Not Triggering?
Dispatcher Not Available?
propel.ini.<behavior name="event_dispatcher" />.Propel Caching
php propel:build --no-confirm) if changes aren’t reflected.Custom Event Dispatcher Override the default dispatcher by extending the behavior:
class CustomEventDispatcherBehavior extends EventDispatcherBehavior {
protected function getDispatcher() {
return new CustomDispatcher(); // Your implementation
}
}
Update propel.ini to point to your class.
Dynamic Event Names Use model properties to construct event names:
$eventName = 'user.'.$this->getType().'.created';
$this->getEventDispatcher()->dispatch($eventName, $this);
Integration with Laravel
$this->app->bind('propel.dispatcher', function () {
return PropelModel::getEventDispatcher();
});
class PropelEvent {
public static function dispatch($event, $subject) {
$model = PropelModel::getModel($subject);
$model->getEventDispatcher()->dispatch($event, $subject);
}
}
Usage: PropelEvent::dispatch('user.created', $user);Testing
$dispatcher = Mockery::mock('Symfony\Component\EventDispatcher\EventDispatcher');
$this->getEventDispatcher()->shouldReceive('dispatch')->once();
How can I help you explore Laravel packages today?