-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUtilityController.php
More file actions
64 lines (51 loc) · 1.86 KB
/
Copy pathUtilityController.php
File metadata and controls
64 lines (51 loc) · 1.86 KB
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
<?php
namespace Thoughtco\StatamicCacheTracker\Http\Controllers;
use Statamic\Http\Controllers\Controller;
use Statamic\Support\Str;
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
class UtilityController extends Controller
{
public function __invoke(): array
{
if (! $urls = request()->input('urls')) {
return [
'message' => __('No URLs provided'),
];
}
if ($urls == '*') {
Tracker::flush();
return [
'message' => __('Cache flushed'),
];
}
$urls = collect(explode(PHP_EOL, $urls));
$wildcards = $urls->filter(fn ($url) => Str::endsWith($url, '*'));
// remove any non-wildcards first
$urls->reject(fn ($url) => Str::endsWith($url, '*'))
->each(function ($url) {
if (Str::endsWith($url, '/')) {
$url = substr($url, 0, -1);
}
Tracker::remove($url);
});
collect(Tracker::all())
->each(function ($data) use ($wildcards) {
$wildcards->each(function ($wildcard) use ($data) {
$prefix = Str::beforeLast($wildcard, '*');
if (Str::startsWith($prefix, ['http://', 'https://'])) {
$matches = Str::startsWith($data['url'], $prefix);
} else {
$prefix = '/'.ltrim($prefix, '/');
$urlPath = parse_url($data['url'], PHP_URL_PATH) ?? $data['url'];
$matches = str_contains($urlPath, $prefix);
}
if ($matches) {
Tracker::remove($data['url']);
}
});
});
return [
'message' => __('URLs cleared from cache'),
];
}
}