|
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 |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
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 nystudio107\fastcgicachebust\models\Settings; |
|
21
|
|
|
use yii\base\ErrorException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
|
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
25
|
|
|
* @package FastcgiCacheBust |
|
|
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
|
|
28
|
|
|
class Cache extends Component |
|
29
|
|
|
{ |
|
30
|
|
|
// Public Methods |
|
31
|
|
|
// ========================================================================= |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Clears the entirety of the FastCGI Cache |
|
35
|
|
|
*/ |
|
|
|
|
|
|
36
|
|
|
public function clearAll() |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
|
|
|
|
|
39
|
|
|
* @var Settings $settings |
|
40
|
|
|
*/ |
|
41
|
|
|
$settings = FastcgiCacheBust::$plugin->getSettings(); |
|
42
|
|
|
if ($settings !== null && !empty($settings->cachePath)) { |
|
43
|
|
|
$cacheDirs = explode(',', $settings->cachePath); |
|
44
|
|
|
foreach ($cacheDirs as $cacheDir) { |
|
45
|
|
|
$cacheDir = Craft::parseEnv($cacheDir); |
|
|
|
|
|
|
46
|
|
|
$cacheDir = trim($cacheDir); |
|
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
FileHelper::clearDirectory($cacheDir); |
|
49
|
|
|
} catch (ErrorException $e) { |
|
50
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
51
|
|
|
} |
|
52
|
|
|
Craft::info( |
|
53
|
|
|
Craft::t( |
|
54
|
|
|
'fastcgi-cache-bust', |
|
55
|
|
|
'FastCGI Cache busted: `' . $cacheDir |
|
56
|
|
|
), |
|
57
|
|
|
__METHOD__ |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Determine whether the cache should be busted or not based on the $element |
|
65
|
|
|
* |
|
66
|
|
|
* @param Element $element |
|
|
|
|
|
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function shouldBustCache(Element $element): bool |
|
71
|
|
|
{ |
|
72
|
|
|
// Don't bust the cache if the element isn't ENABLED or LIVE |
|
73
|
|
|
if (($element->getStatus() !== Element::STATUS_ENABLED) && ($element->getStatus() !== Entry::STATUS_LIVE)) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
// Don't bust the cache if the element is a draft or revision |
|
77
|
|
|
if (ElementHelper::isDraftOrRevision($element)) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|