1 | <?php |
||
2 | |||
3 | namespace Arbory\Base\Services; |
||
4 | |||
5 | use Arbory\Base\Admin\Form\Fields\Text; |
||
6 | use Arbory\Base\Admin\Settings\Setting; |
||
7 | use Illuminate\Support\Facades\Artisan; |
||
8 | |||
9 | class NodeRoutesCache |
||
10 | { |
||
11 | protected const CACHE_KEY = 'nodes.last_update'; |
||
12 | |||
13 | public static function cacheRoutes(): void |
||
14 | { |
||
15 | Artisan::call('route:cache'); |
||
16 | file_put_contents(self::getCachedRoutesTimestampPath(), self::getLatestNodeUpdateTimestamp()); |
||
17 | } |
||
18 | |||
19 | public static function clearCache(): void |
||
20 | { |
||
21 | Artisan::call('route:clear'); |
||
22 | |||
23 | $cachedRoutesTimestampPath = self::getCachedRoutesTimestampPath(); |
||
24 | if (file_exists($cachedRoutesTimestampPath)) { |
||
25 | unlink($cachedRoutesTimestampPath); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | public static function getCurrentCacheTimestamp(): ?int |
||
30 | { |
||
31 | $cachedRoutesTimestampPath = self::getCachedRoutesTimestampPath(); |
||
32 | |||
33 | if (! file_exists($cachedRoutesTimestampPath)) { |
||
34 | return null; |
||
35 | } |
||
36 | |||
37 | return (int) file_get_contents($cachedRoutesTimestampPath); |
||
38 | } |
||
39 | |||
40 | // use dedicated timestamp file to fix filesystem vs laravel app timezone problems |
||
41 | private static function getCachedRoutesTimestampPath(): string |
||
42 | { |
||
43 | return app()->getCachedRoutesPath() . '.timestamp'; |
||
44 | } |
||
45 | |||
46 | public static function isRouteCacheObsolete(): bool |
||
47 | { |
||
48 | $currentCacheTimestamp = self::getCurrentCacheTimestamp(); |
||
49 | |||
50 | if (! $currentCacheTimestamp) { |
||
0 ignored issues
–
show
|
|||
51 | return true; |
||
52 | } |
||
53 | |||
54 | $lastModifiedTimestamp = self::getLatestNodeUpdateTimestamp(); |
||
55 | |||
56 | if (! $lastModifiedTimestamp) { |
||
0 ignored issues
–
show
The expression
$lastModifiedTimestamp of type integer|null is loosely compared to false ; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||
57 | return false; |
||
58 | } |
||
59 | |||
60 | return $lastModifiedTimestamp > $currentCacheTimestamp; |
||
61 | } |
||
62 | |||
63 | public static function getLatestNodeUpdateTimestamp(): ?int |
||
64 | { |
||
65 | $lastUpdate = Setting::query()->where('name', self::CACHE_KEY)->first(); |
||
66 | |||
67 | if (! $lastUpdate) { |
||
68 | return null; |
||
69 | } |
||
70 | |||
71 | return (int) $lastUpdate->value; |
||
72 | } |
||
73 | |||
74 | public static function setLastUpdateTimestamp(int $time): void |
||
75 | { |
||
76 | Setting::query()->updateOrCreate( |
||
77 | ['name' => self::CACHE_KEY], |
||
78 | ['value' => $time, 'type' => Text::class] |
||
79 | ); |
||
80 | } |
||
81 | } |
||
82 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: