-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
SprinkleManager.php
341 lines (296 loc) · 9.71 KB
/
SprinkleManager.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/*
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @copyright Copyright (c) 2019 Alexander Weissman
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\System\Sprinkle;
use Illuminate\Support\Str;
use Psr\Container\ContainerInterface;
use UserFrosting\Support\Exception\FileNotFoundException;
use UserFrosting\Support\Exception\JsonException;
/**
* Sprinkle manager class.
*
* Manages a collection of loaded Sprinkles for the application.
* Handles Sprinkle class creation, event subscription, services registration, and resource stream registration.
*
* @author Alex Weissman (https://alexanderweissman.com)
*/
class SprinkleManager
{
/**
* @var ContainerInterface The global container object, which holds all your services.
*/
protected $ci;
/**
* @var (Sprinkle|null)[] An array of sprinkles : array<name, instance|null>.
*/
protected $sprinkles = [];
/**
* @var string Path to the sprinkles directory. Will be used to register the location with the ResourceLocator
*/
protected $sprinklesPath = \UserFrosting\SPRINKLES_DIR . \UserFrosting\DS;
/**
* Create a new SprinkleManager object.
*
* @param ContainerInterface $ci The global container object, which holds all your services.
*/
public function __construct(ContainerInterface $ci)
{
$this->ci = $ci;
}
/**
* Register resource streams for all base sprinkles.
* For each sprinkle, register its resources and then run its initializer.
*/
public function addResources(): void
{
foreach ($this->sprinkles as $sprinkleName => $sprinkle) {
$this->addSprinkleResources($sprinkleName);
}
}
/**
* Register a sprinkle as a locator location.
*
* @param string $sprinkleName
*/
public function addSprinkleResources(string $sprinkleName): void
{
/** @var \UserFrosting\UniformResourceLocator\ResourceLocator $locator */
$locator = $this->ci->locator;
$locator->registerLocation($sprinkleName, $this->getSprinklePath($sprinkleName));
}
/**
* Returns sprinkle base path from name.
*
* @param string $sprinkleName
*
* @return string
*/
public function getSprinklePath(string $sprinkleName): string
{
// Get Sprinkle and make sure it exist
$sprinkle = $this->getSprinkle($sprinkleName);
if (!$sprinkle) {
throw new FileNotFoundException("Sprinkle `$sprinkleName` doesn't exist.");
}
// Get path and make sure it exist
$path = $this->getSprinklesPath() . $sprinkle;
if (!file_exists($path)) {
throw new FileNotFoundException("Sprinkle `$sprinkleName` should be found at `$path`, but that directory doesn't exist.");
}
return $path;
}
/**
* Returns the sprinkle class.
*
* @param string $sprinkleName
*
* @return string
*/
protected function getSprinkleClass(string $sprinkleName): string
{
$className = Str::studly($sprinkleName);
return $this->getSprinkleClassNamespace($sprinkleName) . "\\$className";
}
/**
* Returns the claculated sprinkle namespace.
*
* @param string $sprinkleName
*
* @return string The Sprinkle Namespace
*/
public function getSprinkleClassNamespace(string $sprinkleName): string
{
$className = Str::studly($sprinkleName);
return "UserFrosting\\Sprinkle\\$className";
}
/**
* Returns the sprinkle service provider class.
*
* @param string $sprinkleName
*
* @return string
*/
protected function getSprinkleDefaultServiceProvider(string $sprinkleName): string
{
return $this->getSprinkleClassNamespace($sprinkleName) . '\\ServicesProvider\\ServicesProvider';
}
/**
* Takes the name of a Sprinkle, and creates an instance of the initializer object (if defined).
*
* Creates an object of a subclass of UserFrosting\System\Sprinkle\Sprinkle if defined for the sprinkle (converting to StudlyCase).
* Otherwise, returns null.
*
* @param string $sprinkleName The name of the Sprinkle to initialize.
*
* @return Sprinkle|null Sprinkle class instance or null if no such class exist
*/
public function bootSprinkle(string $sprinkleName): ?Sprinkle
{
$fullClassName = $this->getSprinkleClass($sprinkleName);
// Check that class exists. If not, set to null
if (class_exists($fullClassName)) {
$sprinkle = new $fullClassName($this->ci);
if (!$sprinkle instanceof Sprinkle) {
throw new SprinkleClassException("$fullClassName must be an instance of " . Sprinkle::class);
}
return $sprinkle;
} else {
return null;
}
}
/**
* Returns a list of available sprinkle names.
*
* @return string[]
*/
public function getSprinkleNames(): array
{
return array_keys($this->sprinkles);
}
/**
* Returns a list of available sprinkles.
*
* @return (Sprinkle|null)[]
*/
public function getSprinkles(): array
{
return $this->sprinkles;
}
/**
* Initialize a list of Sprinkles, instantiating their boot classes (if they exist),
* and subscribing them to the event dispatcher.
*
* @param string[] $sprinkleNames
*/
public function init(array $sprinkleNames): void
{
foreach ($sprinkleNames as $sprinkleName) {
$sprinkle = $this->bootSprinkle($sprinkleName);
if ($sprinkle) {
// Subscribe the sprinkle to the event dispatcher
$this->ci->eventDispatcher->addSubscriber($sprinkle);
}
$this->sprinkles[$sprinkleName] = $sprinkle;
}
}
/**
* Initialize all base sprinkles in a specified Sprinkles schema file (e.g. 'sprinkles.json').
*
* @param string $schemaPath
*/
public function initFromSchema(string $schemaPath): void
{
$baseSprinkleNames = $this->loadSchema($schemaPath);
$this->init($baseSprinkleNames);
}
/**
* Return if a Sprinkle is available
* Can be used by other Sprinkles to test if their dependencies are met.
*
* @param string $sprinkleName The name of the Sprinkle
*
* @return bool
*/
public function isAvailable(string $sprinkleName): bool
{
return (bool) $this->getSprinkle($sprinkleName);
}
/**
* Find sprinkle value from the sprinkles.json.
*
* @param string $sprinkleName
*
* @return string|false Return sprinkle name or false if sprinkle not found
*/
public function getSprinkle(string $sprinkleName)
{
$mathches = preg_grep("/^$sprinkleName$/i", $this->getSprinkleNames());
if (count($mathches) <= 0) {
return false;
}
return array_values($mathches)[0];
}
/**
* Interate through the list of loaded Sprinkles, and invoke their ServiceProvider classes.
*/
public function registerAllServices(): void
{
foreach ($this->getSprinkleNames() as $sprinkleName) {
$this->registerServices($sprinkleName);
}
}
/**
* Register services for a specified Sprinkle.
*
* @param string $sprinkleName
*/
public function registerServices(string $sprinkleName): void
{
//Register the default services
$fullClassName = $this->getSprinkleDefaultServiceProvider($sprinkleName);
// Check that class exists, and register services
if (class_exists($fullClassName)) {
// Register core services
$serviceProvider = new $fullClassName();
$serviceProvider->register($this->ci);
}
// Register services from other providers
if ($this->sprinkles[$sprinkleName] instanceof Sprinkle) {
$this->sprinkles[$sprinkleName]->registerServices();
}
}
/**
* Returns sprinklePath parameter.
*
* @return string
*/
public function getSprinklesPath(): string
{
return $this->sprinklesPath;
}
/**
* Sets sprinklePath parameter.
*
* @param string $sprinklesPath
*
* @return static
*/
public function setSprinklesPath(string $sprinklesPath)
{
$this->sprinklesPath = $sprinklesPath;
return $this;
}
/**
* Load list of Sprinkles from a JSON schema file (e.g. 'sprinkles.json').
*
* @param string $schemaPath
*
* @return string[]
*/
protected function loadSchema(string $schemaPath): array
{
$sprinklesFile = @file_get_contents($schemaPath);
if ($sprinklesFile === false) {
$errorMessage = "Error: Unable to determine Sprinkle load order. File '$schemaPath' not found or unable to read. Please create a 'sprinkles.json' file and try again.";
throw new FileNotFoundException($errorMessage);
}
// Make sure sprinkle contains valid json
if (!$data = json_decode($sprinklesFile)) {
$errorMessage = "Error: Unable to determine Sprinkle load order. File '$schemaPath' doesn't contain valid json : " . json_last_error_msg();
throw new JsonException($errorMessage);
}
// Remove duplicates, keep the first one
// @see https://stackoverflow.com/a/2276400/445757
$sprinkles = $data->base;
$sprinkles = array_intersect_key(
$sprinkles,
array_unique(array_map('strtolower', $sprinkles))
);
return $sprinkles;
}
}