forked from FriendsOfFlarum/clockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClockworkServiceProvider.php
136 lines (110 loc) · 4.43 KB
/
ClockworkServiceProvider.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
<?php
/*
* This file is part of fof/clockwork.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\Clockwork\Provider;
use Clockwork\DataSource\EloquentDataSource;
use Clockwork\DataSource\LaravelCacheDataSource;
use Clockwork\DataSource\LaravelEventsDataSource;
use Clockwork\DataSource\MonologDataSource;
use Clockwork\DataSource\XdebugDataSource;
use Clockwork\Request\Log;
use Clockwork\Support\Laravel\ClockworkSupport;
use Clockwork\Support\Vanilla\Clockwork;
use Flarum\Foundation\Paths;
use Flarum\Group\Group;
use FoF\Clockwork\Clockwork\FlarumAuthenticator;
use FoF\Clockwork\Clockwork\FlarumDataSource;
use FoF\Clockwork\Middleware\BeforeRouteExecutionMiddleware;
use FoF\Clockwork\Middleware\ClockworkMiddleware;
use Illuminate\Support\ServiceProvider;
class ClockworkServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->runningInConsole()) {
return;
}
$this->app['clockwork.eloquent']->listenToEvents();
$this->app['clockwork.cache']->listenToEvents();
$this->app['clockwork.flarum']->listenToEvents();
$this->app['clockwork.events']->listenToEvents();
// This is done to dispatch an event right before controller execution,
// and after all middleware, including extension middleware.
foreach (['admin', 'forum', 'api'] as $frontend) {
$this->app->extend("flarum.$frontend.middleware", function ($middleware) {
$middleware[] = BeforeRouteExecutionMiddleware::class;
return $middleware;
});
}
// Remove the clockwork middleware from API Client handling
$this->app->extend('flarum.api_client.exclude_middleware', function (array $exlusions) {
$exlusions[] = ClockworkMiddleware::class;
return $exlusions;
});
}
public function register()
{
if ($this->runningInConsole()) {
return;
}
$this->app->singleton('clockwork.support', function ($app) {
return new ClockworkSupport($app);
});
$this->app->singleton('clockwork.authenticator', function () {
return new FlarumAuthenticator(Group::ADMINISTRATOR_ID);
});
$this->app->singleton('clockwork.log', function () {
return new Log();
});
$this->app->singleton('clockwork.eloquent', function ($app) {
return new EloquentDataSource($app['db'], $app['events']);
});
$this->app->singleton('clockwork.cache', function ($app) {
return new LaravelCacheDataSource($app['events']);
});
$this->app->singleton('clockwork.events', function ($app) {
return new LaravelEventsDataSource($app['events'], [
'Flarum\\\\Event\\\\.+',
'Flarum\\\\Api\\\\Event\\\\.+',
]);
});
$this->app->singleton('clockwork.xdebug', function () {
return new XdebugDataSource();
});
$this->app->singleton('clockwork.flarum', function ($app) {
return (new FlarumDataSource($app))
->setLog($app['clockwork.log']);
});
$this->app['clockwork.flarum']->listenToEarlyEvents();
$this->app->singleton('clockwork', function ($app) {
// Resolve paths instead of using deprecated `storage_path(...)`
$paths = resolve(Paths::class);
/** @var Clockwork|\Clockwork\Clockwork $clockwork */
$clockwork = Clockwork::init([
'enable' => true,
'storage_files_path' => $paths->storage.'/clockwork',
]);
$clockwork->setAuthenticator($app['clockwork.authenticator']);
$clockwork
->addDataSource(new MonologDataSource($app['log']))
->addDataSource($app['clockwork.eloquent'])
->addDataSource($app['clockwork.cache'])
->addDataSource($app['clockwork.events'])
->addDataSource($app['clockwork.flarum']);
if (in_array('xdebug', get_loaded_extensions())) {
$clockwork->addDataSource($app['clockwork.xdebug']);
}
return $clockwork;
});
}
protected function runningInConsole(): bool
{
return php_sapi_name() === 'cli';
}
}