Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Dec 12, 2024
1 parent 950ed93 commit 274eb3f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
8 changes: 7 additions & 1 deletion _config/events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ SilverStripe\Core\Injector\Injector:
ArchiPro\EventDispatcher\ListenerProvider:
class: ArchiPro\EventDispatcher\ListenerProvider

# Define the error handler
ArchiPro\Silverstripe\EventDispatcher\ErrorHandling\ErrorHandler:
class: ArchiPro\Silverstripe\EventDispatcher\ErrorHandling\SilverstripeErrorHandler
constructor:
logger: '%$Psr\Log\LoggerInterface.errorhandler'

# Default event dispatcher
ArchiPro\EventDispatcher\AsyncEventDispatcher:
class: ArchiPro\EventDispatcher\AsyncEventDispatcher
constructor:
listenerProvider: '%$ArchiPro\EventDispatcher\ListenerProvider'
logger: '%$Psr\Log\LoggerInterface.errorhandler'
errorhandler: '%$ArchiPro\Silverstripe\EventDispatcher\ErrorHandling\ErrorHandler'
Psr\EventDispatcher\EventDispatcherInterface:
alias: '%$ArchiPro\EventDispatcher\AsyncEventDispatcher'

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.10",
"colinodell/psr-testlogger": "^1.3"
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions src/ErrorHandling/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\ErrorHandling;

use Throwable;

/**
* Interface for error handlers.
*/
interface ErrorHandler
{
/**
* Handle an error.
*/
public function handle(Throwable $error): void;
}
19 changes: 19 additions & 0 deletions src/ErrorHandling/SilverstripeErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\ErrorHandling;

use Psr\Log\LoggerInterface;
use Throwable;

class SilverstripeErrorHandler
{
public function __construct(
private readonly LoggerInterface $logger
) {
}

public function __invoke(Throwable $error): void
{
$this->logger->error($error->getMessage(), ['exception' => $error]);
}
}
56 changes: 39 additions & 17 deletions src/Service/TestEventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use ArchiPro\EventDispatcher\AsyncEventDispatcher;
use ArchiPro\EventDispatcher\ListenerProvider;
use Closure;
use ColinODell\PsrTestLogger\TestLogger;
use SilverStripe\Core\Injector\Injector;
use Throwable;

/**
* Extension of the AsyncEventDispatcher for testing purposes.
Expand All @@ -14,17 +16,18 @@
*/
class TestEventService extends EventService
{
private ?TestLogger $logger = null;

/**
* @var array<Throwable>
*/
private array $errors = [];

public function __construct()
{
$listenerProvider = Injector::inst()->get(ListenerProvider::class);

// The test logger is useful but we don't want to force people to install it in production.
if (class_exists(TestLogger::class)) {
$this->logger = new TestLogger();
}
$dispatcher = new AsyncEventDispatcher($listenerProvider, $this->logger, AsyncEventDispatcher::THROW_ON_ERROR);
$dispatcher = new AsyncEventDispatcher(
$listenerProvider,
Closure::fromCallable([$this, 'recordError'])
);
parent::__construct($dispatcher, $listenerProvider);
}

Expand All @@ -39,16 +42,35 @@ public static function bootstrap(): self
}

/**
* Return a logger that can be used to see if an array errors were thrown by the event loop.
* Catch errors and store them for later inspection.
*/
private function recordError(Throwable $message): void
{
$this->errors[] = $message;
}

/**
* Check if any errors were recorded.
*/
public function hasErrors(): bool
{
return !empty($this->errors);
}

/**
* Get all errors that were recorded.
* @return array<Throwable>
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* Clear all errors.
*/
public function getLogger(): TestLogger
public function clearErrors(): void
{
if (!$this->logger) {
throw new \RuntimeException(
'To use the EventService\'s test logger, you must install colinodell/psr-testlogger. ' .
'`composer require --dev colinodell/psr-testlogger`'
);
}
return $this->logger;
$this->errors = [];
}
}

0 comments on commit 274eb3f

Please sign in to comment.