Skip to content

Instantly share code, notes, and snippets.

@faizalpribadi
Last active December 17, 2015 21:38
Show Gist options
  • Save faizalpribadi/5675772 to your computer and use it in GitHub Desktop.
Save faizalpribadi/5675772 to your computer and use it in GitHub Desktop.
Mozart Event Component Couple With Mozart Dependency Injection Component !
<?php
/**
* Build couple the component of Mozart PHP
* Create the custom event and dispatch With Dependency Injection
* Simple And Very Interesting
*/
/**
* Build Use Mozart Component
*/
use Mozart\Library\Event\Event;
use Mozart\Library\Event\EventDispatcher;
use Mozart\Library\DI\Container;
/**
* Example Class Repository Event Extends From Event ("Mozart\Library\Event")
*/
class RepositoryEvent extends Event
{
const REPOSITORY_EVENT = 'repository.event';
protected $event;
public function __construct()
{
$this->setEvent(RepositoryEvent::REPOSITORY_EVENT);
}
public function getEvent()
{
parent::getEvent();
}
}
class Dispatch
{
protected $dispatcher;
public function dispatcher(EventDispatcher $dispatcher = null)
{
if (null === $dispatcher) {
$dispatcher = new EventDispatcher();
}
$event = new RepositoryEvent();
$dispatcher->addListener(RepositoryEvent::REPOSITORY_EVENT, array($event, 'getEvent'));
$dispatcher->dispatch(RepositoryEvent::REPOSITORY_EVENT);
$this->dispatcher = $dispatcher;
}
}
/**
* Dispatch With Dependency Injection
*/
$container = new Container();
$container->set('repository', function() {
$dispatch = new Dispatch();
$dispatch->dispatcher();
return $dispatch;
});
$container->get('repository');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment