-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationBooter.php
81 lines (71 loc) · 2.57 KB
/
ApplicationBooter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Xanweb\C5\Foundation;
use Concrete\Core\Application\Application;
use Concrete\Core\Routing\RouteListInterface;
use Concrete\Core\Support\Facade\Route;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
abstract class ApplicationBooter
{
/**
* Class to be used Statically.
*/
private function __construct()
{
}
/**
* Boot up Application.
*
* @param Application $app
*/
final public static function boot(Application $app): void
{
static::_boot($app);
if (($routeListClasses = static::getRoutesClasses()) !== []) {
/**
* @var \Concrete\Core\Routing\Router $router
*/
$router = Route::getFacadeRoot();
foreach ($routeListClasses as $routeListClass) {
if (is_subclass_of($routeListClass, RouteListInterface::class)) {
$router->loadRouteList($app->make($routeListClass));
} else {
self::throwInvalidClassRuntimeException('getRoutesClass', $routeListClass, RouteListInterface::class);
}
}
}
// Register Event Subscribers
if (($evtSubscriberClasses = static::getEventSubscribers()) !== []) {
$director = $app->make('director');
foreach ($evtSubscriberClasses as $evtSubscriberClass) {
if (is_subclass_of($evtSubscriberClass, EventSubscriberInterface::class)) {
$director->addSubscriber($app->make($evtSubscriberClass));
} else {
self::throwInvalidClassRuntimeException('getEventSubscribers', $evtSubscriberClass, EventSubscriberInterface::class);
}
}
}
}
abstract protected static function _boot(Application $app): void;
/**
* Get Class name for RouteList, must be an instance of \Concrete\Core\Routing\RouteListInterface.
*
* @return string[]
*/
protected static function getRoutesClasses(): array
{
return [];
}
/**
* Event Subscribers should be an instance of \Symfony\Component\EventDispatcher\EventSubscriberInterface.
*
* @return string[]
*/
protected static function getEventSubscribers(): array
{
return [];
}
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));
}
}