-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
269 lines (239 loc) · 7.41 KB
/
Module.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace Xanweb\Module;
use Concrete\Core\Application\Application;
use Concrete\Core\Entity\Package;
use Concrete\Core\Foundation\ClassAliasList;
use Concrete\Core\Foundation\Service\ProviderList;
use Concrete\Core\Package\Package as PackageController;
use Concrete\Core\Package\PackageService;
use Concrete\Core\Routing\RouteListInterface;
use Concrete\Core\Support\Facade\Application as FacadeApp;
use Concrete\Core\Support\Facade\Route;
use Illuminate\Support\Str;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Xanweb\Module\Asset\Provider;
/**
* @method static string getPackagePath() @see \Concrete\Core\Package\Package::getPackagePath()
* @method static string getRelativePath() @see \Concrete\Core\Package\Package::getRelativePath()
*/
abstract class Module implements ModuleInterface
{
private static Application $app;
/**
* The resolved controller instances.
*
* @var array
*/
private static array $resolvedPackController = [];
/**
* Class to be used Statically.
*/
private function __construct()
{
}
/**
* Handle dynamic, static calls to the controller.
*
* @param string $method
* @param array $args
*
* @return mixed
*/
public static function __callStatic($method, $args)
{
return self::controller()->$method(...$args);
}
/**
* {@inheritdoc}
*
* @see ModuleInterface::pkg()
*/
public static function pkg(): Package
{
return self::controller()->getPackageEntity();
}
/**
* {@inheritdoc}
*
* @see ModuleInterface::boot()
*/
public static function boot(): void
{
// Register Class Aliases
if (($aliases = static::getClassAliases()) !== []) {
ClassAliasList::getInstance()->registerMultiple($aliases);
}
$app = self::app();
// Register Service Providers
if (($providers = static::getServiceProviders()) !== []) {
$app->make(ProviderList::class)->registerProviders($providers);
}
// Register Route Lists
if (($routeListClasses = static::getRoutesClasses()) !== []) {
$router = Route::getFacadeRoot();
foreach ($routeListClasses as $routeListClass) {
if (is_subclass_of($routeListClass, RouteListInterface::class)) {
$router->loadRouteList($app->make($routeListClass));
} else {
self::throwInvalidClassRuntimeException('getRoutesClasses', $routeListClass, RouteListInterface::class);
}
}
}
// Register Asset Providers
$assetProviders = static::getAssetProviders();
foreach ($assetProviders as $assetProviderClass) {
if (is_subclass_of($assetProviderClass, Provider::class)) {
$app->make($assetProviderClass, ['package' => static::pkg()])->register();
} else {
self::throwInvalidClassRuntimeException('getAssetProviders', $assetProviderClass, Provider::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);
}
}
}
//register console commands
if (($consoleCmdClasses = static::getConsoleCmds()) !== []) {
foreach ($consoleCmdClasses as $cmdClass) {
if ($app->has("console")) {
$app->make("console")->add($app->make($cmdClass));
}
}
}
}
public static function isInstalled(): bool
{
try {
return static::pkg()->isPackageInstalled();
} catch (\Throwable $e) {
return false;
}
}
/**
* Get Database Config.
*
* @param string|null $key
* @param mixed $default
*
* @return \Concrete\Core\Config\Repository\Liaison|mixed
*/
final public static function getConfig(?string $key = null, $default = null)
{
$config = self::controller()->getDatabaseConfig();
if ($key !== null) {
return $config->get($key, $default);
}
return $config;
}
/**
* Get File Config.
*
* @param string|null $key
* @param mixed $default
*
* @return \Concrete\Core\Config\Repository\Liaison|mixed
*/
final public static function getFileConfig(?string $key = null, $default = null)
{
$config = self::controller()->getFileConfig();
if ($key !== null) {
return $config->get($key, $default);
}
return $config;
}
/**
* Classes to be registered as aliases in \Concrete\Core\Foundation\ClassAliasList.
*
* @return array
*/
protected static function getClassAliases(): array
{
return [
static::getPackageAlias() => static::class,
];
}
/**
* Get Package Alias.
*
* @return string
*/
protected static function getPackageAlias(): string
{
return Str::studly(static::pkgHandle());
}
/**
* Get Service Providers Class Names.
*
* @return string[]
*/
protected static function getServiceProviders(): array
{
return [];
}
/**
* Get Classes names for RouteList, must be an instance of \Concrete\Core\Routing\RouteListInterface.
*
* @return string[]
*/
protected static function getRoutesClasses(): array
{
return [];
}
/**
* AssetProviders should be an instance of \Xanweb\Module\Asset\Provider.
*
* @return string[]
*/
protected static function getAssetProviders(): array
{
return [];
}
/**
* Event Subscribers should be an instance of \Symfony\Component\EventDispatcher\EventSubscriberInterface.
*
* @return string[]
*/
protected static function getEventSubscribers(): array
{
return [];
}
/**
* @param string $make [optional]
*
* @return Application|object
*/
protected static function app($make = null)
{
if (!isset(self::$app)) {
self::$app = FacadeApp::getFacadeApplication();
}
if ($make !== null) {
return self::$app->make($make);
}
return self::$app;
}
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));
}
private static function controller(): PackageController
{
$pkgHandle = static::pkgHandle();
return self::$resolvedPackController[$pkgHandle] ??= self::app(PackageService::class)->getClass($pkgHandle);
}
/**
* return list of CLI command class list
* @return string[]
*/
protected static function getConsoleCmds(): array
{
return [];
}
}