Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Joomla\Event\EventInterface;
use Joomla\Event\EventImmutable;
use Joomla\Event\Priority;
use Joomla\Event\Tests\Stubs\ErrorResistibleEvent;
use Joomla\Event\Tests\Stubs\FirstListener;
use Joomla\Event\Tests\Stubs\SecondListener;
use Joomla\Event\Tests\Stubs\SomethingListener;
Expand Down Expand Up @@ -717,4 +718,51 @@ public function testRemoveSubscriber()
$this->assertFalse($this->instance->hasListener([$listener, 'onSomething']));
$this->assertFalse($this->instance->hasListener([$listener, 'onAfterSomething']));
}

/**
* @testdox An error resistible event works as expected the dispatcher
*
* @covers \Joomla\Event\Dispatcher
* @uses \Joomla\Event\ErrorResistibleEventInterface
*/
public function testErrorResistibleEvent()
{
$this->instance->addListener('onErroredEventTest', function () {
throw new \Exception('Event error 1');
});
$this->instance->addListener('onErroredEventTest', function () {
// No error
});
$this->instance->addListener('onErroredEventTest', function () {
throw new \Exception('Event error 2');
});

$errors = [];
$event = new ErrorResistibleEvent('onErroredEventTest', [], function (\Throwable $e) use (&$errors) {
$errors[] = $e->getMessage();
});

$this->instance->dispatch('onErroredEventTest', $event);

$this->assertEquals(2, count($errors), 'The error handler of the event should collect correct amount of errors.');
}

/**
* @testdox An error resistible event throws an TypeError when error handler not set
*
* @covers \Joomla\Event\Dispatcher
* @uses \Joomla\Event\ErrorResistibleEventInterface
*/
public function testErrorResistibleEventIncorectlyImplemented()
{
$this->instance->addListener('onErroredEventTest', function () {
throw new \Exception('Event error 1');
});

$event = new ErrorResistibleEvent('onErroredEventTest');

$this->expectException(\TypeError::class);

$this->instance->dispatch('onErroredEventTest', $event);
}
}
37 changes: 37 additions & 0 deletions Tests/Stubs/ErrorResistibleEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @copyright Copyright (C) 2005 - 2023 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Event\Tests\Stubs;

use Joomla\Event\ErrorResistibleEventInterface;
use Joomla\Event\ErrorResistibleTrait;
use Joomla\Event\Event;

/**
* An error resistible event
*
* @since __DEPLOY_VERSION__
*/
class ErrorResistibleEvent extends Event implements ErrorResistibleEventInterface
{
use ErrorResistibleTrait;

/**
* Constructor.
*
* @param string $name The event name.
* @param array $arguments The event arguments.
* @param ?callable $errorHandler The event arguments.
*
* @since @since __DEPLOY_VERSION__
*/
public function __construct($name, array $arguments = [], callable $errorHandler = null)
{
parent::__construct($name, $arguments);

$this->errorHandler = $errorHandler;
}
}
18 changes: 17 additions & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,23 @@ public function dispatch(string $name, ?EventInterface $event = null): EventInte
$event = $this->getDefaultEvent($name);
}

if (isset($this->listeners[$event->getName()])) {
if (!isset($this->listeners[$event->getName()])) {
return $event;
}

if ($event instanceof ErrorResistibleEventInterface) {
foreach ($this->listeners[$event->getName()] as $listener) {
if ($event->isStopped()) {
return $event;
}

try {
$listener($event);
} catch (\Throwable $e) {
$event->handleError($e);
}
}
} else {
foreach ($this->listeners[$event->getName()] as $listener) {
if ($event->isStopped()) {
return $event;
Expand Down
37 changes: 37 additions & 0 deletions src/ErrorResistibleEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Part of the Joomla Framework Event Package
*
* @copyright Copyright (C) 2005 - 2023 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Event;

/**
* Interface for error resistible events.
* Event implementing this interface allows to handle errors of the event listener.
*
* @since __DEPLOY_VERSION__
*/
interface ErrorResistibleEventInterface
{
/**
* Retrieve error handler for the event.
*
* @return callable
*
* @since __DEPLOY_VERSION__
*/
public function getErrorHandler(): callable;

/**
* Handle the error.
*
* @param \Throwable $error The error instance
*
* @since __DEPLOY_VERSION__
*/
public function handleError(\Throwable $error): void;
}
52 changes: 52 additions & 0 deletions src/ErrorResistibleTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Part of the Joomla Framework Event Package
*
* @copyright Copyright (C) 2005 - 2023 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Event;

/**
* Trait with base method for ErrorResistibleEventInterface
*
* @since __DEPLOY_VERSION__
*/
trait ErrorResistibleTrait
{
/**
* Error handler.
*
* @return callable
*
* @since __DEPLOY_VERSION__
*/
private $errorHandler;

/**
* Retrieve error handler for the event.
*
* @return callable
*
* @since __DEPLOY_VERSION__
*/
public function getErrorHandler(): callable
{
return $this->errorHandler;
}

/**
* Handle the error.
*
* @param \Throwable $error The error instance
*
* @since __DEPLOY_VERSION__
*/
public function handleError(\Throwable $error): void
{
$handler = $this->getErrorHandler();
$handler($error);
}
}