Skip to content

Commit

Permalink
ApplicationBooter: Add support for Event Subscribers registration
Browse files Browse the repository at this point in the history
  • Loading branch information
HamedDarragi committed Mar 31, 2021
1 parent 1068b7d commit 1f91195
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions ApplicationBooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Concrete\Core\Application\Application;
use Concrete\Core\Routing\RouteListInterface;
use Concrete\Core\Support\Facade\Route;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

abstract class ApplicationBooter
{
Expand All @@ -17,17 +18,17 @@ private function __construct()
}

/**
* Boot up Application.
*
* @param Application $app
*
* @throws RuntimeException
* @noinspection PhpDocMissingThrowsInspection
*/
final public static function boot(Application $app): void
{
static::_boot($app);

$routeListClasses = static::getRoutesClasses();
if ($routeListClasses !== []) {
if (($routeListClasses = static::getRoutesClasses()) !== []) {
/**
* @var \Concrete\Core\Routing\Router $router
*/
Expand All @@ -37,21 +38,49 @@ final public static function boot(Application $app): void
/** @noinspection PhpUnhandledExceptionInspection */
$router->loadRouteList($app->build($routeListClass));
} else {
throw new RuntimeException(t(static::class . ':getRoutesClass: RoutesClass should be instanceof Concrete\Core\Routing\RouteListInterface'));
self::throwInvalidClassRuntimeException('getRoutesClass', $routeListClass, RouteListInterface::class);
}
}
}

// Register Event Subscribers
if (($evtSubscriberClasses = static::getEventSubscribers()) !== []) {
$director = $app->make(EventDispatcherInterface::class);
foreach ($evtSubscriberClasses as $evtSubscriberClass) {
if (is_subclass_of($evtSubscriberClass, EventSubscriberInterface::class)) {
/** @noinspection PhpUnhandledExceptionInspection */
$director->addSubscriber($app->build($evtSubscriberClass));
} else {
self::throwInvalidClassRuntimeException('getEventSubscribers', $evtSubscriberClass, EventSubscriberInterface::class);
}
}
}
}

private static function throwInvalidClassRuntimeException(string $relatedMethod, $targetClass, string $requiredClass): void
{
throw new \RuntimeException(t('%s:%s - `%s` should be an instance of `%s`', static::class, $relatedMethod, (string) $targetClass, $requiredClass));
}

abstract protected static function _boot(Application $app): void;

/**
* Get Class name for RouteList, must be instance of \Concrete\Core\Routing\RouteListInterface.
*
* @return array
* @return string[]
*/
protected static function getRoutesClasses(): array
{
return [];
}

/**
* Event Subscribers should be instance of \Symfony\Component\EventDispatcher\EventSubscriberInterface.
*
* @return string[]
*/
protected static function getEventSubscribers(): array
{
return [];
}
}

0 comments on commit 1f91195

Please sign in to comment.