Repository navigation
Expand file tree
/
Copy pathRefreshCacheJob.php
More file actions
60 lines (51 loc) · 1.44 KB
/
Copy pathRefreshCacheJob.php
File metadata and controls
60 lines (51 loc) · 1.44 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
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
class RefreshCacheJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $paths;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(array $paths)
{
$this->paths = $paths;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
foreach ($this->paths as $path) {
$this->clearCache($path);
$this->warmCache($path);
}
}
protected function clearCache(string $path): void
{
$staticCachingPath = config('statamic.static_caching.strategies.full.path', public_path('static'));
$globPath = $staticCachingPath . '/' . $path . '*';
foreach (File::glob($globPath) as $file) {
File::delete($file);
}
}
protected function warmCache(string $path): void
{
$fullUrl = rtrim(config('app.url'), '/') . '/' . ltrim($path, '/');
$response = Http::get($fullUrl);
if ($response->failed()) {
throw new \Exception("Failed to warm cache for: {$fullUrl}");
}
}
}