Skip to content

Commit

Permalink
added two interfaces: EventInterface and EventDispatcherInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 25, 2011
1 parent b325487 commit 0e66e38
Show file tree
Hide file tree
Showing 31 changed files with 347 additions and 168 deletions.
Expand Up @@ -16,8 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventInterface;

/**
* Converts \ReflectionParameters for Controller actions into Objects if the \ReflectionParameter have a class
Expand All @@ -44,14 +43,14 @@ public function __construct(ConverterManager $manager)
}

/**
* @param Event $event
* @param mixed $controller
* @param EventInterface $event
* @param mixed $controller
*
* @return mixed
*
* @throws NotFoundHttpException
*/
public function filterController(Event $event, $controller)
public function filterController(EventInterface $event, $controller)
{
if (!is_array($controller)) {
return $controller;
Expand Down
11 changes: 5 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Debug/EventDispatcher.php
Expand Up @@ -12,8 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Debug;

use Symfony\Bundle\FrameworkBundle\EventDispatcher as BaseEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Debug\EventDispatcherTraceableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -45,7 +44,7 @@ public function __construct(ContainerInterface $container, LoggerInterface $logg
/**
* {@inheritDoc}
*/
public function notify(Event $event)
public function notify(EventInterface $event)
{
foreach ($this->getListeners($event->getName()) as $listener) {
if (is_array($listener) && is_string($listener[0])) {
Expand All @@ -63,7 +62,7 @@ public function notify(Event $event)
/**
* {@inheritDoc}
*/
public function notifyUntil(Event $event)
public function notifyUntil(EventInterface $event)
{
foreach ($this->getListeners($event->getName()) as $i => $listener) {
if (is_array($listener) && is_string($listener[0])) {
Expand Down Expand Up @@ -93,7 +92,7 @@ public function notifyUntil(Event $event)
/**
* {@inheritDoc}
*/
public function filter(Event $event, $value)
public function filter(EventInterface $event, $value)
{
foreach ($this->getListeners($event->getName()) as $listener) {
if (is_array($listener) && is_string($listener[0])) {
Expand Down Expand Up @@ -160,7 +159,7 @@ protected function listenerToString($listener)
}
}

protected function addCall(Event $event, $listener, $type)
protected function addCall(EventInterface $event, $listener, $type)
{
$listener = $this->listenerToString($listener);

Expand Down
Expand Up @@ -181,7 +181,9 @@ protected function doConfigLoad(array $config, ContainerBuilder $container)
'Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerResolver',
'Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller',

'Symfony\\Component\\EventDispatcher\\EventInterface',
'Symfony\\Component\\EventDispatcher\\Event',
'Symfony\\Component\\EventDispatcher\\EventDispatcherInterface',
'Symfony\\Component\\EventDispatcher\\EventDispatcher',
'Symfony\\Bundle\\FrameworkBundle\\EventDispatcher',

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/EventDispatcher.php
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\EventDispatcher\EventDispatcher as BaseEventDispatcher;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventInterface;

/**
* This EventDispatcher automatically gets the kernel listeners injected
Expand Down Expand Up @@ -42,7 +42,7 @@ public function registerKernelListeners(array $listeners)
/**
* {@inheritdoc}
*/
public function notify(Event $event)
public function notify(EventInterface $event)
{
foreach ($this->getListeners($event->getName()) as $listener) {
if (is_array($listener) && is_string($listener[0])) {
Expand All @@ -57,7 +57,7 @@ public function notify(Event $event)
/**
* {@inheritdoc}
*/
public function notifyUntil(Event $event)
public function notifyUntil(EventInterface $event)
{
foreach ($this->getListeners($event->getName()) as $listener) {
if (is_array($listener) && is_string($listener[0])) {
Expand All @@ -75,7 +75,7 @@ public function notifyUntil(Event $event)
/**
* {@inheritdoc}
*/
public function filter(Event $event, $value)
public function filter(EventInterface $event, $value)
{
foreach ($this->getListeners($event->getName()) as $listener) {
if (is_array($listener) && is_string($listener[0])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/HttpKernel.php
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\HttpKernel as BaseHttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher as BaseEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* This HttpKernel is used to manage scope changes of the DI container.
Expand All @@ -25,7 +25,7 @@ public function __construct(ContainerInterface $container, ControllerResolverInt
$this->resolver = $controllerResolver;
}

public function setEventDispatcher(BaseEventDispatcher $dispatcher)
public function setEventDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/RequestListener.php
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -38,7 +38,7 @@ public function __construct(ContainerInterface $container, RouterInterface $rout
$this->logger = $logger;
}

public function handle(Event $event)
public function handle(EventInterface $event)
{
$request = $event->get('request');
$master = HttpKernelInterface::MASTER_REQUEST === $event->get('request_type');
Expand Down
Expand Up @@ -11,8 +11,7 @@

namespace Symfony\Bundle\WebProfilerBundle;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\HttpKernel;
Expand All @@ -36,7 +35,7 @@ public function __construct(HttpKernel $kernel, $interceptRedirects = false)
$this->interceptRedirects = $interceptRedirects;
}

public function handle(Event $event, Response $response)
public function handle(EventInterface $event, Response $response)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) {
return $response;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/EventDispatcher/Event.php
Expand Up @@ -16,7 +16,7 @@
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Event
class Event implements EventInterface
{
protected $value = null;
protected $processed = false;
Expand Down
28 changes: 13 additions & 15 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Expand Up @@ -14,11 +14,9 @@
/**
* EventDispatcher implements a dispatcher object.
*
* @see http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/index.html Apple's Cocoa framework
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class EventDispatcher
class EventDispatcher implements EventDispatcherInterface
{
protected $listeners = array();

Expand Down Expand Up @@ -46,8 +44,8 @@ public function connect($name, $listener, $priority = 0)
/**
* Disconnects one, or all listeners for the given event name.
*
* @param string $name An event name
* @param mixed|null $listener the listener to remove, or null to remove all
* @param string $name An event name
* @param mixed|null $listener The listener to remove, or null to remove all
*
* @return void
*/
Expand All @@ -74,11 +72,11 @@ public function disconnect($name, $listener = null)
/**
* Notifies all listeners of a given event.
*
* @param Event $event An Event instance
* @param EventInterface $event An EventInterface instance
*
* @return Event The Event instance
* @return EventInterface The EventInterface instance
*/
public function notify(Event $event)
public function notify(EventInterface $event)
{
foreach ($this->getListeners($event->getName()) as $listener) {
call_user_func($listener, $event);
Expand All @@ -90,11 +88,11 @@ public function notify(Event $event)
/**
* Notifies all listeners of a given event until one returns a non null value.
*
* @param Event $event An Event instance
* @param EventInterface $event An EventInterface instance
*
* @return Event The Event instance
* @return EventInterface The EventInterface instance
*/
public function notifyUntil(Event $event)
public function notifyUntil(EventInterface $event)
{
foreach ($this->getListeners($event->getName()) as $listener) {
if (call_user_func($listener, $event)) {
Expand All @@ -109,12 +107,12 @@ public function notifyUntil(Event $event)
/**
* Filters a value by calling all listeners of a given event.
*
* @param Event $event An Event instance
* @param mixed $value The value to be filtered
* @param EventInterface $event An EventInterface instance
* @param mixed $value The value to be filtered
*
* @return Event The Event instance
* @return EventInterface The EventInterface instance
*/
public function filter(Event $event, $value)
public function filter(EventInterface $event, $value)
{
foreach ($this->getListeners($event->getName()) as $listener) {
$value = call_user_func($listener, $event, $value);
Expand Down
89 changes: 89 additions & 0 deletions src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher;

/**
* EventDispatcherInterface describes an event dispatcher class.
*
* @see http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/index.html Apple's Cocoa framework
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
interface EventDispatcherInterface
{
/**
* Connects a listener to a given event name.
*
* Listeners with a higher priority are executed first.
*
* @param string $name An event name
* @param mixed $listener A PHP callable
* @param integer $priority The priority (between -10 and 10 -- defaults to 0)
*/
function connect($name, $listener, $priority = 0);

/**
* Disconnects one, or all listeners for the given event name.
*
* @param string $name An event name
* @param mixed|null $listener The listener to remove, or null to remove all
*
* @return void
*/
function disconnect($name, $listener = null);

/**
* Notifies all listeners of a given event.
*
* @param EventInterface $event An EventInterface instance
*
* @return EventInterface The EventInterface instance
*/
function notify(EventInterface $event);

/**
* Notifies all listeners of a given event until one returns a non null value.
*
* @param EventInterface $event An EventInterface instance
*
* @return EventInterface The EventInterface instance
*/
function notifyUntil(EventInterface $event);

/**
* Filters a value by calling all listeners of a given event.
*
* @param EventInterface $event An EventInterface instance
* @param mixed $value The value to be filtered
*
* @return EventInterface The EventInterface instance
*/
function filter(EventInterface $event, $value);

/**
* Returns true if the given event name has some listeners.
*
* @param string $name The event name
*
* @return Boolean true if some listeners are connected, false otherwise
*/
function hasListeners($name);

/**
* Returns all listeners associated with a given event name.
*
* @param string $name The event name
*
* @return array An array of listeners
*/
function getListeners($name);
}

0 comments on commit 0e66e38

Please sign in to comment.