Skip to content

Commit c808dfb

Browse files
committed
[EventDispatcher] Add an interface for the ContainerAwareEventDispatcher
1 parent 784df79 commit c808dfb

File tree

4 files changed

+104
-16
lines changed

4 files changed

+104
-16
lines changed

src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author Bernhard Schussek <bschussek@gmail.com>
2222
* @author Jordan Alliot <jordan.alliot@gmail.com>
2323
*/
24-
class ContainerAwareEventDispatcher extends EventDispatcher
24+
class ContainerAwareEventDispatcher extends EventDispatcher implements ContainerAwareEventDispatcherInterface
2525
{
2626
/**
2727
* The container from where services are loaded
@@ -52,16 +52,7 @@ public function __construct(ContainerInterface $container)
5252
}
5353

5454
/**
55-
* Adds a service as event listener
56-
*
57-
* @param string $eventName Event for which the listener is added
58-
* @param array $callback The service ID of the listener service & the method
59-
* name that has to be called
60-
* @param int $priority The higher this value, the earlier an event listener
61-
* will be triggered in the chain.
62-
* Defaults to 0.
63-
*
64-
* @throws \InvalidArgumentException
55+
* @see ContainerAwareEventDispatcherInterface::addListenerService
6556
*/
6657
public function addListenerService($eventName, $callback, $priority = 0)
6758
{
@@ -132,10 +123,7 @@ public function getListeners($eventName = null)
132123
}
133124

134125
/**
135-
* Adds a service as event subscriber
136-
*
137-
* @param string $serviceId The service ID of the subscriber service
138-
* @param string $class The service's class name (which must implement EventSubscriberInterface)
126+
* @see ContainerAwareEventDispatcherInterface::addSubscriberService
139127
*/
140128
public function addSubscriberService($serviceId, $class)
141129
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher;
13+
14+
/**
15+
* A ContainerAwareEventDispatcher is able to load the listeners
16+
* and subscribers from the dependency injection container.
17+
*
18+
* @author Tristan Darricau <tristan@darricau.eu>
19+
*
20+
* @api
21+
*/
22+
interface ContainerAwareEventDispatcherInterface extends EventDispatcherInterface
23+
{
24+
/**
25+
* Adds a service as event listener
26+
*
27+
* @param string $eventName Event for which the listener is added
28+
* @param array $callback The service ID of the listener service & the method
29+
* name that has to be called
30+
* @param int $priority The higher this value, the earlier an event listener
31+
* will be triggered in the chain.
32+
* Defaults to 0.
33+
*
34+
* @throws \InvalidArgumentException
35+
*/
36+
public function addListenerService($eventName, $callback, $priority = 0);
37+
38+
/**
39+
* Adds a service as event subscriber
40+
*
41+
* @param string $serviceId The service ID of the subscriber service
42+
* @param string $class The service's class name (which must implement EventSubscriberInterface)
43+
*/
44+
public function addSubscriberService($serviceId, $class);
45+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher\Debug;
13+
14+
use Symfony\Component\Stopwatch\Stopwatch;
15+
use Psr\Log\LoggerInterface;
16+
17+
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcherInterface;
18+
19+
/**
20+
* Collects some data about event listeners.
21+
*
22+
* This event dispatcher delegates the dispatching to another one.
23+
*
24+
* @author Tristan Darricau <tristan@darricau.eu>
25+
*/
26+
class TraceableContainerAwareEventDispatcher extends TraceableEventDispatcher implements ContainerAwareEventDispatcherInterface
27+
{
28+
/**
29+
* Constructor.
30+
*
31+
* @param ContainerAwareEventDispatcherInterface $dispatcher An EventDispatcherInterface instance
32+
* @param Stopwatch $stopwatch A Stopwatch instance
33+
* @param LoggerInterface $logger A LoggerInterface instance
34+
*/
35+
public function __construct(ContainerAwareEventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
36+
{
37+
parent::__construct($dispatcher, $stopwatch, $logger);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function addListenerService($eventName, $callback, $priority = 0)
44+
{
45+
$this->dispatcher->addListenerService($eventName, $callback, $priority);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function addSubscriberService($serviceId, $class)
52+
{
53+
$this->dispatcher->addSubscriberService($serviceId, $class);
54+
}
55+
}

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
2828
{
2929
protected $logger;
3030
protected $stopwatch;
31+
protected $dispatcher;
3132

3233
private $called;
33-
private $dispatcher;
3434

3535
/**
3636
* Constructor.

0 commit comments

Comments
 (0)