improve error messages in the event dispatcher
This commit is contained in:
parent
bca4761f37
commit
0b381dbe1d
|
@ -54,15 +54,13 @@ class EventDispatcher implements EventDispatcherInterface
|
|||
|
||||
if (\is_object($event)) {
|
||||
$eventName = $eventName ?? \get_class($event);
|
||||
} else {
|
||||
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
|
||||
} elseif (\is_string($event) && (null === $eventName || $eventName instanceof Event)) {
|
||||
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
|
||||
$swap = $event;
|
||||
$event = $eventName ?? new Event();
|
||||
$eventName = $swap;
|
||||
|
||||
if (!$event instanceof Event) {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
|
||||
}
|
||||
} else {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', EventDispatcherInterface::class, \is_object($event) ? \get_class($event) : \gettype($event)));
|
||||
}
|
||||
|
||||
if (null !== $this->optimized && null !== $eventName) {
|
||||
|
|
|
@ -16,7 +16,7 @@ use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
|
|||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch().
|
||||
* A helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch().
|
||||
*
|
||||
* This class should be deprecated in Symfony 5.1
|
||||
*
|
||||
|
@ -57,15 +57,13 @@ final class LegacyEventDispatcherProxy implements EventDispatcherInterface
|
|||
|
||||
if (\is_object($event)) {
|
||||
$eventName = $eventName ?? \get_class($event);
|
||||
} else {
|
||||
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', ContractsEventDispatcherInterface::class), E_USER_DEPRECATED);
|
||||
} elseif (\is_string($event) && (null === $eventName || $eventName instanceof Event)) {
|
||||
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.', ContractsEventDispatcherInterface::class), E_USER_DEPRECATED);
|
||||
$swap = $event;
|
||||
$event = $eventName ?? new Event();
|
||||
$eventName = $swap;
|
||||
|
||||
if (!$event instanceof Event) {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', ContractsEventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
|
||||
}
|
||||
} else {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', ContractsEventDispatcherInterface::class, \is_object($event) ? \get_class($event) : \gettype($event)));
|
||||
}
|
||||
|
||||
$listeners = $this->getListeners($eventName);
|
||||
|
|
|
@ -412,6 +412,33 @@ class EventDispatcherTest extends TestCase
|
|||
|
||||
$this->assertTrue($testLoaded);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
|
||||
*/
|
||||
public function testLegacySignatureWithoutEvent()
|
||||
{
|
||||
$this->dispatcher->dispatch('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
|
||||
*/
|
||||
public function testLegacySignatureWithEvent()
|
||||
{
|
||||
$this->dispatcher->dispatch('foo', new Event());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \TypeError
|
||||
* @expectedExceptionMessage Argument 1 passed to "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" must be an object, string given.
|
||||
*/
|
||||
public function testLegacySignatureWithNewEventObject()
|
||||
{
|
||||
$this->dispatcher->dispatch('foo', new ContractsEvent());
|
||||
}
|
||||
}
|
||||
|
||||
class CallableClass
|
||||
|
|
|
@ -14,12 +14,42 @@ namespace Symfony\Component\EventDispatcher\Tests;
|
|||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
|
||||
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class LegacyEventDispatcherTest extends EventDispatcherTest
|
||||
{
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation The signature of the "Symfony\Component\EventDispatcher\Tests\TestLegacyEventDispatcher::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.
|
||||
* @expectedDeprecation Calling the "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
|
||||
*/
|
||||
public function testLegacySignatureWithoutEvent()
|
||||
{
|
||||
$this->createEventDispatcher()->dispatch('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation The signature of the "Symfony\Component\EventDispatcher\Tests\TestLegacyEventDispatcher::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.
|
||||
* @expectedDeprecation Calling the "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
|
||||
*/
|
||||
public function testLegacySignatureWithEvent()
|
||||
{
|
||||
$this->createEventDispatcher()->dispatch('foo', new Event());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \TypeError
|
||||
* @expectedExceptionMessage Argument 1 passed to "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" must be an object, string given.
|
||||
*/
|
||||
public function testLegacySignatureWithNewEventObject()
|
||||
{
|
||||
$this->createEventDispatcher()->dispatch('foo', new ContractsEvent());
|
||||
}
|
||||
|
||||
protected function createEventDispatcher()
|
||||
{
|
||||
return LegacyEventDispatcherProxy::decorate(new TestLegacyEventDispatcher());
|
||||
|
|
Reference in New Issue