forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppServiceProvider.php
133 lines (113 loc) · 4.57 KB
/
AppServiceProvider.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
<?php
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.
namespace App\Providers;
use App\Hashing\OsuBcryptHasher;
use App\Libraries\MorphMap;
use App\Libraries\OsuCookieJar;
use App\Libraries\OsuMessageSelector;
use App\Libraries\RateLimiter;
use App\Singletons;
use Datadog;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Support\ServiceProvider;
use Knuckles\Scribe\Scribe;
use Laravel\Octane\Contracts\DispatchesTasks;
use Laravel\Octane\SequentialTaskDispatcher;
use Laravel\Octane\Swoole\SwooleTaskDispatcher;
use Queue;
use Swoole\Http\Server;
class AppServiceProvider extends ServiceProvider
{
const LOCAL_CACHE_SINGLETONS = [
'chat-filters' => Singletons\ChatFilters::class,
'countries' => Singletons\Countries::class,
'groups' => Singletons\Groups::class,
'layout-cache' => Singletons\LayoutCache::class,
'medals' => Singletons\Medals::class,
'smilies' => Singletons\Smilies::class,
];
const SINGLETONS = [
'OsuAuthorize' => Singletons\OsuAuthorize::class,
'assets-manifest' => Singletons\AssetsManifest::class,
'clean-html' => Singletons\CleanHTML::class,
'ip2asn' => Singletons\Ip2Asn::class,
'local-cache-manager' => Singletons\LocalCacheManager::class,
'mods' => Singletons\Mods::class,
'route-section' => Singletons\RouteSection::class,
'score-pins' => Singletons\UserScorePins::class,
];
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Relation::morphMap(MorphMap::flippedMap());
$GLOBALS['cfg'] = \Config::all();
Queue::after(function (JobProcessed $event) {
app('OsuAuthorize')->resetCache();
app('local-cache-manager')->incrementResetTicker();
Datadog::increment(
$GLOBALS['cfg']['datadog-helper']['prefix_web'].'.queue.run',
1,
[
'job' => $event->job->payload()['data']['commandName'],
'queue' => $event->job->getQueue(),
]
);
});
$this->app->make('translator')->setSelector(new OsuMessageSelector());
app('url')->forceScheme(substr($GLOBALS['cfg']['app']['url'], 0, 5) === 'https' ? 'https' : 'http');
Request::setTrustedProxies($GLOBALS['cfg']['trustedproxy']['proxies'], $GLOBALS['cfg']['trustedproxy']['headers']);
// newest scribe tries to rename {modelName} parameters to {id}
// but it kind of doesn't work with our route handlers.
Scribe::normalizeEndpointUrlUsing(fn ($url) => $url);
\Hash::extend('osubcrypt', fn () => new OsuBcryptHasher());
}
/**
* Register any application services.
*
* This service provider is a great spot to register your various container
* bindings with the application.
*
* @return void
*/
public function register()
{
foreach (array_merge(static::SINGLETONS, static::LOCAL_CACHE_SINGLETONS) as $name => $class) {
$this->app->singleton($name, fn () => new $class());
}
$localCacheManager = app('local-cache-manager');
foreach (static::LOCAL_CACHE_SINGLETONS as $name => $_class) {
$localCacheManager->registerSingleton(app($name));
}
$this->app->singleton('cookie', function ($app) {
$config = $GLOBALS['cfg']['session'];
return (new OsuCookieJar())->setDefaultPathAndDomain(
$config['path'],
$config['domain'],
$config['secure'],
$config['same_site'] ?? null
);
});
$this->app->singleton(RateLimiter::class, function ($app) {
return new RateLimiter($app->make('cache')->driver(
$app['config']->get('cache.limiter')
));
});
// pre-bind to avoid SwooleHttpTaskDispatcher and fallback when not running in a swoole context.
$this->app->bind(
DispatchesTasks::class,
fn ($app) => $app->bound(Server::class) ? new SwooleTaskDispatcher() : new SequentialTaskDispatcher()
);
$env = $this->app->environment();
if ($env === 'testing' || $env === 'dusk.local') {
// This is needed for testing with Dusk.
$this->app->register(AdditionalDuskServiceProvider::class);
}
}
}