-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend.php
79 lines (62 loc) · 2.79 KB
/
extend.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
<?php
namespace ClarkWinkelmann\Scratchpad;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extend;
use Flarum\Http\Middleware\AuthenticateWithSession;
$extenders = [
(new Extend\Frontend('forum'))
->js(__DIR__ . '/js/dist/forum.js'),
(new Extend\Frontend('admin'))
->js(__DIR__ . '/js/dist/admin.js')
->css(__DIR__ . '/resources/less/admin.less')
->content(Content\AddThemeCss::class),
new Extend\Locales(__DIR__ . '/resources/locale'),
(new Extend\Routes('api'))
->get('/scratchpads', 'scratchpad.api.index', Controllers\ListScratchpadController::class)
->post('/scratchpads', 'scratchpad.api.store', Controllers\CreateScratchpadController::class)
->patch('/scratchpads/{id:[0-9]+}', 'scratchpad.api.update', Controllers\UpdateScratchpadController::class)
->delete('/scratchpads/{id:[0-9]+}', 'scratchpad.api.delete', Controllers\DeleteScratchpadController::class)
->post('/scratchpads/{id:[0-9]+}/compile', 'scratchpad.api.compile', Controllers\CompileScratchpadController::class),
(new Extend\Middleware('forum'))
->insertAfter(AuthenticateWithSession::class, Middlewares\AuthenticateWithLiveToken::class),
(new Extend\Middleware('admin'))
->insertAfter(AuthenticateWithSession::class, Middlewares\AuthenticateWithLiveToken::class),
(new Extend\ErrorHandling())
->handler(ErrorHandling\ValidationExceptionWithMeta::class, ErrorHandling\ValidationExceptionWithMetaHandler::class),
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(ForumAttributes::class),
(new Extend\ServiceProvider())
->register(Providers\RegisterAssets::class)
->register(Providers\TestRoutes::class),
];
LiveCodeHelper::boot();
/**
* @var $repository ScratchpadRepository
*/
$repository = resolve(ScratchpadRepository::class);
try {
foreach ($repository->allEnabled() as $scratchpad) {
if ($scratchpad->id === LiveCodeHelper::$ignoreScratchpadId) {
continue;
}
try {
$return = $scratchpad->evaluatePhp();
if (is_array($return)) {
$extenders = array_merge($extenders, $return);
}
} catch (\Throwable $exception) {
PHPLoadErrors::record($scratchpad, $exception);
}
}
} catch (\Exception $exception) {
// A table not found exception might be thrown if migrations for this extensions have not run yet
// We will silence such an error
// We catch \Exception because it could be either a PDOException or QueryException
if (!str_contains($exception->getMessage(), 'scratchpads')) {
throw $exception;
}
}
if (LiveCodeHelper::$php) {
$extenders = array_merge($extenders, PHPEvaluator::evaluate(LiveCodeHelper::$php));
}
return $extenders;