feature #22796 [EventDispatcher] remove deprecated features (xabbuh)
This PR was merged into the 4.0-dev branch.
Discussion
----------
[EventDispatcher] remove deprecated features
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | no
| BC breaks? | yes
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
Commits
-------
598bec4
[EventDispatcher] remove deprecated features
This commit is contained in:
commit
c0a2d02ad4
@ -32,8 +32,6 @@ use Symfony\Component\DependencyInjection\Exception\LogicException;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
|
||||
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
@ -98,13 +96,6 @@ class FrameworkExtension extends Extension
|
||||
$loader->load('web.xml');
|
||||
$loader->load('services.xml');
|
||||
|
||||
// forward compatibility with Symfony 4.0 where the ContainerAwareEventDispatcher class is removed
|
||||
if (!class_exists(ContainerAwareEventDispatcher::class)) {
|
||||
$definition = $container->getDefinition('event_dispatcher');
|
||||
$definition->setClass(EventDispatcher::class);
|
||||
$definition->setArguments(array());
|
||||
}
|
||||
|
||||
if (PHP_VERSION_ID < 70000) {
|
||||
$definition = $container->getDefinition('kernel.class_cache.cache_warmer');
|
||||
$definition->addTag('kernel.cache_warmer');
|
||||
@ -309,7 +300,6 @@ class FrameworkExtension extends Extension
|
||||
'Symfony\\Component\\DependencyInjection\\Container',
|
||||
|
||||
'Symfony\\Component\\EventDispatcher\\Event',
|
||||
'Symfony\\Component\\EventDispatcher\\ContainerAwareEventDispatcher',
|
||||
|
||||
'Symfony\\Component\\HttpKernel\\EventListener\\ResponseListener',
|
||||
'Symfony\\Component\\HttpKernel\\EventListener\\RouterListener',
|
||||
|
@ -7,9 +7,7 @@
|
||||
<services>
|
||||
<defaults public="false" />
|
||||
|
||||
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher" public="true">
|
||||
<argument type="service" id="service_container" />
|
||||
</service>
|
||||
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\EventDispatcher" public="true" />
|
||||
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" />
|
||||
|
||||
<service id="http_kernel" class="Symfony\Component\HttpKernel\HttpKernel" public="true">
|
||||
|
@ -17,7 +17,6 @@ use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\Templating\EngineInterface as ComponentEngineInterface;
|
||||
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
|
||||
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;
|
||||
|
||||
class AutowiringTypesTest extends WebTestCase
|
||||
@ -56,12 +55,7 @@ class AutowiringTypesTest extends WebTestCase
|
||||
$container = static::$kernel->getContainer();
|
||||
|
||||
$autowiredServices = $container->get('test.autowiring_types.autowired_services');
|
||||
|
||||
if (class_exists(ContainerAwareEventDispatcher::class)) {
|
||||
$this->assertInstanceOf(ContainerAwareEventDispatcher::class, $autowiredServices->getDispatcher(), 'The event_dispatcher service should be injected if the debug is not enabled');
|
||||
} else {
|
||||
$this->assertInstanceOf(EventDispatcher::class, $autowiredServices->getDispatcher(), 'The event_dispatcher service should be injected if the debug is not enabled');
|
||||
}
|
||||
$this->assertInstanceOf(EventDispatcher::class, $autowiredServices->getDispatcher(), 'The event_dispatcher service should be injected if the debug is not enabled');
|
||||
|
||||
static::bootKernel(array('debug' => true));
|
||||
$container = static::$kernel->getContainer();
|
||||
|
@ -39,7 +39,6 @@ use Symfony\Component\Config\Resource\ResourceInterface;
|
||||
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
|
||||
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
|
||||
use Symfony\Component\ExpressionLanguage\Expression;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
|
||||
@ -1076,11 +1075,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
|
||||
$r = new \ReflectionClass($class = $parameterBag->resolveValue($definition->getClass()));
|
||||
|
||||
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
|
||||
// don't trigger deprecations for internal uses
|
||||
// @deprecated since version 3.3, to be removed in 4.0 along with the deprecated class
|
||||
$deprecationWhitelist = array('event_dispatcher' => ContainerAwareEventDispatcher::class);
|
||||
|
||||
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ") && (!isset($deprecationWhitelist[$id]) || $deprecationWhitelist[$id] !== $class)) {
|
||||
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
|
||||
@trigger_error(sprintf('The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name), E_USER_DEPRECATED);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
4.0.0
|
||||
-----
|
||||
|
||||
* removed the `ContainerAwareEventDispatcher` class
|
||||
|
||||
3.3.0
|
||||
-----
|
||||
|
||||
|
@ -1,211 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Lazily loads listeners and subscribers from the dependency injection
|
||||
* container.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Jordan Alliot <jordan.alliot@gmail.com>
|
||||
*
|
||||
* @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure-proxy injection instead.
|
||||
*/
|
||||
class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
{
|
||||
/**
|
||||
* The container from where services are loaded.
|
||||
*
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* The service IDs of the event listeners and subscribers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $listenerIds = array();
|
||||
|
||||
/**
|
||||
* The services registered as listeners.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $listeners = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param ContainerInterface $container A ContainerInterface instance
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
$class = get_class($this);
|
||||
if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
|
||||
$class = get_parent_class($class);
|
||||
}
|
||||
if (__CLASS__ !== $class) {
|
||||
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use EventDispatcher with closure-proxy injection instead.', __CLASS__), E_USER_DEPRECATED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a service as event listener.
|
||||
*
|
||||
* @param string $eventName Event for which the listener is added
|
||||
* @param array $callback The service ID of the listener service & the method
|
||||
* name that has to be called
|
||||
* @param int $priority The higher this value, the earlier an event listener
|
||||
* will be triggered in the chain.
|
||||
* Defaults to 0.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function addListenerService($eventName, $callback, $priority = 0)
|
||||
{
|
||||
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use EventDispatcher with closure-proxy injection instead.', __CLASS__), E_USER_DEPRECATED);
|
||||
|
||||
if (!is_array($callback) || 2 !== count($callback)) {
|
||||
throw new \InvalidArgumentException('Expected an array("service", "method") argument');
|
||||
}
|
||||
|
||||
$this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
|
||||
}
|
||||
|
||||
public function removeListener($eventName, $listener)
|
||||
{
|
||||
$this->lazyLoad($eventName);
|
||||
|
||||
if (isset($this->listenerIds[$eventName])) {
|
||||
foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method, $priority)) {
|
||||
$key = $serviceId.'.'.$method;
|
||||
if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
|
||||
unset($this->listeners[$eventName][$key]);
|
||||
if (empty($this->listeners[$eventName])) {
|
||||
unset($this->listeners[$eventName]);
|
||||
}
|
||||
unset($this->listenerIds[$eventName][$i]);
|
||||
if (empty($this->listenerIds[$eventName])) {
|
||||
unset($this->listenerIds[$eventName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parent::removeListener($eventName, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasListeners($eventName = null)
|
||||
{
|
||||
if (null === $eventName) {
|
||||
return (bool) count($this->listenerIds) || (bool) count($this->listeners);
|
||||
}
|
||||
|
||||
if (isset($this->listenerIds[$eventName])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return parent::hasListeners($eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListeners($eventName = null)
|
||||
{
|
||||
if (null === $eventName) {
|
||||
foreach ($this->listenerIds as $serviceEventName => $args) {
|
||||
$this->lazyLoad($serviceEventName);
|
||||
}
|
||||
} else {
|
||||
$this->lazyLoad($eventName);
|
||||
}
|
||||
|
||||
return parent::getListeners($eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListenerPriority($eventName, $listener)
|
||||
{
|
||||
$this->lazyLoad($eventName);
|
||||
|
||||
return parent::getListenerPriority($eventName, $listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a service as event subscriber.
|
||||
*
|
||||
* @param string $serviceId The service ID of the subscriber service
|
||||
* @param string $class The service's class name (which must implement EventSubscriberInterface)
|
||||
*/
|
||||
public function addSubscriberService($serviceId, $class)
|
||||
{
|
||||
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use EventDispatcher with closure-proxy injection instead.', __CLASS__), E_USER_DEPRECATED);
|
||||
|
||||
foreach ($class::getSubscribedEvents() as $eventName => $params) {
|
||||
if (is_string($params)) {
|
||||
$this->listenerIds[$eventName][] = array($serviceId, $params, 0);
|
||||
} elseif (is_string($params[0])) {
|
||||
$this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
|
||||
} else {
|
||||
foreach ($params as $listener) {
|
||||
$this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getContainer()
|
||||
{
|
||||
@trigger_error('The '.__METHOD__.'() method is deprecated since version 3.3 as its class will be removed in 4.0. Inject the container or the services you need in your listeners/subscribers instead.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily loads listeners for this event from the dependency injection
|
||||
* container.
|
||||
*
|
||||
* @param string $eventName The name of the event to dispatch. The name of
|
||||
* the event is the name of the method that is
|
||||
* invoked on listeners.
|
||||
*/
|
||||
protected function lazyLoad($eventName)
|
||||
{
|
||||
if (isset($this->listenerIds[$eventName])) {
|
||||
foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) {
|
||||
$listener = $this->container->get($serviceId);
|
||||
|
||||
$key = $serviceId.'.'.$method;
|
||||
if (!isset($this->listeners[$eventName][$key])) {
|
||||
$this->addListener($eventName, array($listener, $method), $priority);
|
||||
} elseif ($listener !== $this->listeners[$eventName][$key]) {
|
||||
parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
|
||||
$this->addListener($eventName, array($listener, $method), $priority);
|
||||
}
|
||||
|
||||
$this->listeners[$eventName][$key] = $listener;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,210 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\Tests;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
|
||||
{
|
||||
protected function createEventDispatcher()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
return new ContainerAwareEventDispatcher($container);
|
||||
}
|
||||
|
||||
public function testAddAListenerService()
|
||||
{
|
||||
$event = new Event();
|
||||
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
|
||||
|
||||
$service
|
||||
->expects($this->once())
|
||||
->method('onEvent')
|
||||
->with($event)
|
||||
;
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.listener', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
|
||||
|
||||
$dispatcher->dispatch('onEvent', $event);
|
||||
}
|
||||
|
||||
public function testAddASubscriberService()
|
||||
{
|
||||
$event = new Event();
|
||||
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
|
||||
|
||||
$service
|
||||
->expects($this->once())
|
||||
->method('onEvent')
|
||||
->with($event)
|
||||
;
|
||||
|
||||
$service
|
||||
->expects($this->once())
|
||||
->method('onEventWithPriority')
|
||||
->with($event)
|
||||
;
|
||||
|
||||
$service
|
||||
->expects($this->once())
|
||||
->method('onEventNested')
|
||||
->with($event)
|
||||
;
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.subscriber', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
|
||||
|
||||
$dispatcher->dispatch('onEvent', $event);
|
||||
$dispatcher->dispatch('onEventWithPriority', $event);
|
||||
$dispatcher->dispatch('onEventNested', $event);
|
||||
}
|
||||
|
||||
public function testPreventDuplicateListenerService()
|
||||
{
|
||||
$event = new Event();
|
||||
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
|
||||
|
||||
$service
|
||||
->expects($this->once())
|
||||
->method('onEvent')
|
||||
->with($event)
|
||||
;
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.listener', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
|
||||
|
||||
$dispatcher->dispatch('onEvent', $event);
|
||||
}
|
||||
|
||||
public function testHasListenersOnLazyLoad()
|
||||
{
|
||||
$event = new Event();
|
||||
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.listener', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
|
||||
|
||||
$service
|
||||
->expects($this->once())
|
||||
->method('onEvent')
|
||||
->with($event)
|
||||
;
|
||||
|
||||
$this->assertTrue($dispatcher->hasListeners());
|
||||
|
||||
if ($dispatcher->hasListeners('onEvent')) {
|
||||
$dispatcher->dispatch('onEvent');
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetListenersOnLazyLoad()
|
||||
{
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.listener', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
|
||||
|
||||
$listeners = $dispatcher->getListeners();
|
||||
|
||||
$this->assertTrue(isset($listeners['onEvent']));
|
||||
|
||||
$this->assertCount(1, $dispatcher->getListeners('onEvent'));
|
||||
}
|
||||
|
||||
public function testRemoveAfterDispatch()
|
||||
{
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.listener', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
|
||||
|
||||
$dispatcher->dispatch('onEvent', new Event());
|
||||
$dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
|
||||
$this->assertFalse($dispatcher->hasListeners('onEvent'));
|
||||
}
|
||||
|
||||
public function testRemoveBeforeDispatch()
|
||||
{
|
||||
$service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
|
||||
|
||||
$container = new Container();
|
||||
$container->set('service.listener', $service);
|
||||
|
||||
$dispatcher = new ContainerAwareEventDispatcher($container);
|
||||
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
|
||||
|
||||
$dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
|
||||
$this->assertFalse($dispatcher->hasListeners('onEvent'));
|
||||
}
|
||||
}
|
||||
|
||||
class Service
|
||||
{
|
||||
public function onEvent(Event $e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class SubscriberService implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'onEvent' => 'onEvent',
|
||||
'onEventWithPriority' => array('onEventWithPriority', 10),
|
||||
'onEventNested' => array(array('onEventNested')),
|
||||
);
|
||||
}
|
||||
|
||||
public function onEvent(Event $e)
|
||||
{
|
||||
}
|
||||
|
||||
public function onEventWithPriority(Event $e)
|
||||
{
|
||||
}
|
||||
|
||||
public function onEventNested(Event $e)
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user