1 | <?php |
||||||
2 | /** |
||||||
3 | * FastCGI Cache Bust plugin for Craft CMS 3.x |
||||||
4 | * |
||||||
5 | * Bust the Nginx FastCGI Cache when entries are saved or created. |
||||||
6 | * |
||||||
7 | * @link https://nystudio107.com |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
8 | * @copyright Copyright (c) 2017 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
9 | */ |
||||||
0 ignored issues
–
show
|
|||||||
10 | |||||||
11 | namespace nystudio107\fastcgicachebust\services; |
||||||
12 | |||||||
13 | use Craft; |
||||||
14 | use craft\base\Component; |
||||||
15 | use craft\base\Element; |
||||||
16 | use craft\elements\Entry; |
||||||
17 | use craft\helpers\ElementHelper; |
||||||
18 | use craft\helpers\FileHelper; |
||||||
19 | use nystudio107\fastcgicachebust\FastcgiCacheBust; |
||||||
20 | use yii\base\ErrorException; |
||||||
21 | |||||||
22 | /** |
||||||
0 ignored issues
–
show
|
|||||||
23 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
24 | * @package FastcgiCacheBust |
||||||
0 ignored issues
–
show
|
|||||||
25 | * @since 1.0.0 |
||||||
0 ignored issues
–
show
|
|||||||
26 | */ |
||||||
0 ignored issues
–
show
|
|||||||
27 | class Cache extends Component |
||||||
28 | { |
||||||
29 | // Public Methods |
||||||
30 | // ========================================================================= |
||||||
31 | |||||||
32 | /** |
||||||
33 | * Clears the entirety of the FastCGI Cache |
||||||
34 | */ |
||||||
0 ignored issues
–
show
|
|||||||
35 | public function clearAll(): void |
||||||
36 | { |
||||||
37 | $settings = FastcgiCacheBust::$plugin->getSettings(); |
||||||
0 ignored issues
–
show
The method
getSettings() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
38 | if ($settings !== null && !empty($settings->cachePath)) { |
||||||
39 | $cacheDirs = explode(',', $settings->cachePath); |
||||||
40 | foreach ($cacheDirs as $cacheDir) { |
||||||
41 | $cacheDir = Craft::parseEnv($cacheDir); |
||||||
0 ignored issues
–
show
The function
Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||||
42 | $cacheDir = trim($cacheDir); |
||||||
0 ignored issues
–
show
It seems like
$cacheDir can also be of type null ; however, parameter $string of trim() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
43 | try { |
||||||
44 | FileHelper::clearDirectory($cacheDir); |
||||||
45 | } catch (ErrorException $errorException) { |
||||||
46 | Craft::error($errorException->getMessage(), __METHOD__); |
||||||
47 | } |
||||||
48 | |||||||
49 | Craft::info( |
||||||
50 | Craft::t( |
||||||
51 | 'fastcgi-cache-bust', |
||||||
52 | 'FastCGI Cache busted: `' . $cacheDir |
||||||
53 | ), |
||||||
54 | __METHOD__ |
||||||
55 | ); |
||||||
56 | } |
||||||
57 | } |
||||||
58 | } |
||||||
59 | |||||||
60 | /** |
||||||
61 | * Determine whether the cache should be busted or not based on the $element |
||||||
62 | * |
||||||
63 | * @param Element $element |
||||||
0 ignored issues
–
show
|
|||||||
64 | * |
||||||
65 | * @return bool |
||||||
66 | */ |
||||||
67 | public function shouldBustCache(Element $element): bool |
||||||
68 | { |
||||||
69 | // Don't bust the cache if the element isn't ENABLED or LIVE |
||||||
70 | if (($element->getStatus() !== Element::STATUS_ENABLED) && ($element->getStatus() !== Entry::STATUS_LIVE)) { |
||||||
71 | return false; |
||||||
72 | } |
||||||
73 | // Don't bust the cache if the element is a draft or revision |
||||||
74 | if (ElementHelper::isDraftOrRevision($element)) { |
||||||
75 | return false; |
||||||
76 | } |
||||||
77 | |||||||
78 | return true; |
||||||
79 | } |
||||||
80 | } |
||||||
81 |