-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathServiceProvider.php
110 lines (86 loc) · 3.81 KB
/
ServiceProvider.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
<?php
namespace Statamic\StaticCaching;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Illuminate\Support\Str;
use Statamic\Facades\Cascade;
use Statamic\StaticCaching\NoCache\DatabaseSession;
use Statamic\StaticCaching\NoCache\Session;
class ServiceProvider extends LaravelServiceProvider
{
public function register()
{
$this->app->singleton(StaticCacheManager::class, function ($app) {
return new StaticCacheManager($app);
});
$this->app->bind(Cacher::class, function ($app) {
return $app[StaticCacheManager::class]->driver();
});
$this->app->bind(Invalidator::class, function ($app) {
$class = config('statamic.static_caching.invalidation.class') ?? DefaultInvalidator::class;
return $app[$class];
});
$this->app->bind(DefaultInvalidator::class, function ($app) {
return new DefaultInvalidator(
$app[Cacher::class],
$app['config']['statamic.static_caching.invalidation.rules']
);
});
$this->app->singleton(Session::class, function ($app) {
$uri = $app[Cacher::class]->getUrl($app['request']);
if (config('statamic.static_caching.ignore_query_strings', false)) {
$uri = explode('?', $uri)[0];
}
if (Str::contains($uri, '?')) {
$uri = Str::before($uri, '?').'?'.Request::normalizeQueryString(Str::after($uri, '?'));
}
return match ($driver = config('statamic.static_caching.nocache', 'cache')) {
'cache' => new Session($uri),
'database' => new DatabaseSession($uri),
default => throw new \Exception('Nocache driver ['.$driver.'] is not supported.'),
};
});
$this->app->bind(UrlExcluder::class, function ($app) {
$class = config('statamic.static_caching.exclude.class') ?? DefaultUrlExcluder::class;
return $app[$class];
});
$this->app->bind(DefaultUrlExcluder::class, function ($app) {
$config = $app['config']['statamic.static_caching.exclude'];
// Before the urls sub-array was introduced, you could define
// the urls to be excluded at the top "exclude" array level.
$urls = $config['urls'] ?? $config;
return new DefaultUrlExcluder(
$app[Cacher::class]->getBaseUrl(),
$urls
);
});
$this->app->singleton(ResponseStatusTracker::class, fn () => new ResponseStatusTracker);
}
public function boot()
{
if (config('statamic.static_caching.strategy')) {
Event::subscribe(Invalidate::class);
}
// When the cascade gets hydrated, insert it into the
// nocache session so it can filter out contextual data.
Cascade::hydrated(function ($cascade) {
$this->app[Session::class]->setCascade($cascade->toArray());
});
Blade::directive('nocache', function ($exp) {
return '<?php echo app("Statamic\StaticCaching\NoCache\BladeDirective")->handle('.$exp.', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>';
});
Request::macro('normalizedFullUrl', function () {
return app(Cacher::class)->getUrl($this);
});
Request::macro('fakeStaticCacheStatus', function (int $status) {
$url = '/__shared-errors/'.$status;
$this->pathInfo = $url;
$this->requestUri = $url;
app(Session::class)->setUrl($url);
return $this;
});
$this->app[ResponseStatusTracker::class]->registerMacros();
}
}