/
OctaneServiceProvider.php
228 lines (205 loc) · 8.63 KB
/
OctaneServiceProvider.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
<?php
namespace Laravel\Octane;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Laravel\Octane\Cache\OctaneArrayStore;
use Laravel\Octane\Cache\OctaneStore;
use Laravel\Octane\Contracts\DispatchesCoroutines;
use Laravel\Octane\Events\TickReceived;
use Laravel\Octane\Exceptions\DdException;
use Laravel\Octane\Exceptions\TaskException;
use Laravel\Octane\Exceptions\TaskTimeoutException;
use Laravel\Octane\Facades\Octane as OctaneFacade;
use Laravel\Octane\RoadRunner\ServerProcessInspector as RoadRunnerServerProcessInspector;
use Laravel\Octane\RoadRunner\ServerStateFile as RoadRunnerServerStateFile;
use Laravel\Octane\Swoole\ServerProcessInspector as SwooleServerProcessInspector;
use Laravel\Octane\Swoole\ServerStateFile as SwooleServerStateFile;
use Laravel\Octane\Swoole\SignalDispatcher;
use Laravel\Octane\Swoole\SwooleCoroutineDispatcher;
use Laravel\Octane\Swoole\SwooleTaskDispatcher;
class OctaneServiceProvider extends ServiceProvider
{
/**
* Register Octane's services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/octane.php', 'octane');
$this->bindListeners();
$this->app->singleton('octane', Octane::class);
$this->app->bind(RoadRunnerServerProcessInspector::class, function ($app) {
return new RoadRunnerServerProcessInspector(
$app->make(RoadRunnerServerStateFile::class),
new SymfonyProcessFactory,
new PosixExtension,
);
});
$this->app->bind(RoadRunnerServerStateFile::class, function ($app) {
return new RoadRunnerServerStateFile($app['config']->get(
'octane.state_file',
storage_path('logs/octane-server-state.json')
));
});
$this->app->bind(SwooleServerProcessInspector::class, function ($app) {
return new SwooleServerProcessInspector(
$app->make(SignalDispatcher::class),
$app->make(SwooleServerStateFile::class),
$app->make(Exec::class),
);
});
$this->app->bind(SwooleServerStateFile::class, function ($app) {
return new SwooleServerStateFile($app['config']->get(
'octane.state_file',
storage_path('logs/octane-server-state.json')
));
});
$this->app->bind(DispatchesCoroutines::class, function ($app) {
return class_exists('Swoole\Http\Server')
? new SwooleCoroutineDispatcher($app->bound('Swoole\Http\Server'))
: $app->make(SequentialCoroutineDispatcher::class);
});
}
/**
* Bootstrap Octane's services.
*
* @return void
*/
public function boot()
{
$dispatcher = $this->app[Dispatcher::class];
foreach ($this->app['config']->get('octane.listeners', []) as $event => $listeners) {
foreach (array_filter(array_unique($listeners)) as $listener) {
$dispatcher->listen($event, $listener);
}
}
$this->registerCacheDriver();
$this->registerCommands();
$this->registerHttpTaskHandlingRoutes();
$this->registerPublishing();
}
/**
* Bind the Octane event listeners in the container.
*
* @return void
*/
protected function bindListeners()
{
$this->app->singleton(Listeners\CollectGarbage::class);
$this->app->singleton(Listeners\CreateConfigurationSandbox::class);
$this->app->singleton(Listeners\DisconnectFromDatabases::class);
$this->app->singleton(Listeners\EnforceRequestScheme::class);
$this->app->singleton(Listeners\EnsureRequestServerPortMatchesScheme::class);
$this->app->singleton(Listeners\EnsureUploadedFilesAreValid::class);
$this->app->singleton(Listeners\EnsureUploadedFilesCanBeMoved::class);
$this->app->singleton(Listeners\FlushAuthenticationState::class);
$this->app->singleton(Listeners\FlushQueuedCookies::class);
$this->app->singleton(Listeners\FlushSessionState::class);
$this->app->singleton(Listeners\FlushTemporaryContainerInstances::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToAuthorizationGate::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToBroadcastManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToHttpKernel::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToMailManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToNotificationChannelManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToPipelineHub::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToQueueManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToRouter::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToValidationFactory::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToViewFactory::class);
$this->app->singleton(Listeners\GiveNewRequestInstanceToApplication::class);
$this->app->singleton(Listeners\GiveNewRequestInstanceToPaginator::class);
$this->app->singleton(Listeners\PrepareInertiaForNextOperation::class);
$this->app->singleton(Listeners\PrepareLivewireForNextOperation::class);
$this->app->singleton(Listeners\PrepareScoutForNextOperation::class);
$this->app->singleton(Listeners\PrepareSocialiteForNextOperation::class);
$this->app->singleton(Listeners\ReportException::class);
$this->app->singleton(Listeners\StopWorkerIfNecessary::class);
}
/**
* Register the Octane cache driver.
*
* @return void
*/
protected function registerCacheDriver()
{
if (empty($this->app['config']['octane.cache'])) {
return;
}
$store = $this->app->bound('octane.cacheTable')
? new OctaneStore($this->app['octane.cacheTable'])
: new OctaneArrayStore;
Event::listen(TickReceived::class, fn () => $store->refreshIntervalCaches());
Cache::extend('octane', fn () => Cache::repository($store));
}
/**
* Register the commands offered by Octane.
*
* @return void
*/
protected function registerCommands()
{
if ($this->app->runningInConsole()) {
$this->commands([
Commands\InstallCommand::class,
Commands\StartCommand::class,
Commands\StartRoadRunnerCommand::class,
Commands\StartSwooleCommand::class,
Commands\ReloadCommand::class,
Commands\StatusCommand::class,
Commands\StopCommand::class,
]);
}
}
/**
* Register the Octane routes that handle tasks from invokers not in a Server context.
*
* @return void
*/
protected function registerHttpTaskHandlingRoutes()
{
OctaneFacade::route('POST', '/octane/resolve-tasks', function (Request $request) {
try {
return new Response(serialize((new SwooleTaskDispatcher)->resolve(
unserialize(Crypt::decryptString($request->input('tasks'))),
$request->input('wait')
)), 200);
} catch (DecryptException) {
return new Response('', 403);
} catch (TaskException|DdException) {
return new Response('', 500);
} catch (TaskTimeoutException) {
return new Response('', 504);
}
});
OctaneFacade::route('POST', '/octane/dispatch-tasks', function (Request $request) {
try {
(new SwooleTaskDispatcher)->dispatch(
unserialize(Crypt::decryptString($request->input('tasks'))),
);
} catch (DecryptException) {
return new Response('', 403);
}
return new Response('', 200);
});
}
/**
* Register Octane's publishing.
*
* @return void
*/
protected function registerPublishing()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/octane.php' => config_path('octane.php'),
], 'octane-config');
}
}
}