1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Action\System; |
4
|
|
|
|
5
|
|
|
// From PSR-7 |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
// From 'charcoal-admin' |
10
|
|
|
use Charcoal\Admin\Action\System\AbstractCacheAction; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Empty the entire cache pool of items. |
14
|
|
|
*/ |
15
|
|
|
class ClearCacheAction extends AbstractCacheAction |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @todo Implement support for deleting a specific cache item. |
19
|
|
|
* @param RequestInterface $request A PSR-7 compatible Request instance. |
20
|
|
|
* @param ResponseInterface $response A PSR-7 compatible Response instance. |
21
|
|
|
* @return ResponseInterface |
22
|
|
|
*/ |
23
|
|
|
public function run(RequestInterface $request, ResponseInterface $response) |
24
|
|
|
{ |
25
|
|
|
$translator = $this->translator(); |
26
|
|
|
|
27
|
|
|
$cacheType = $request->getParam('cache_type'); |
|
|
|
|
28
|
|
|
if (!is_string($cacheType) || empty($cacheType)) { |
29
|
|
|
$this->addFeedback('error', $translator->translate('Cache type not defined.')); |
30
|
|
|
$this->setSuccess(false); |
31
|
|
|
return $response->withStatus(400); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$result = false; |
35
|
|
|
$message = null; |
36
|
|
|
|
37
|
|
|
if ($cacheType === 'global') { |
38
|
|
|
$result = $this->clearGlobalCache(); |
39
|
|
|
|
40
|
|
|
if ($result) { |
41
|
|
|
$message = $translator->translate('Cache cleared successfully.'); |
42
|
|
|
} else { |
43
|
|
|
$message = $translator->translate('Failed to clear cache.'); |
44
|
|
|
} |
45
|
|
|
} elseif ($cacheType === 'pages') { |
46
|
|
|
$result = $this->clearPagesCache(); |
47
|
|
|
|
48
|
|
|
if ($result) { |
49
|
|
|
$message = $translator->translate('Pages cache cleared successfully.'); |
50
|
|
|
} else { |
51
|
|
|
$message = $translator->translate('Failed to clear pages cache.'); |
52
|
|
|
} |
53
|
|
|
} elseif ($cacheType === 'objects') { |
54
|
|
|
$result = $this->clearObjectsCache(); |
55
|
|
|
|
56
|
|
|
if ($result) { |
57
|
|
|
$message = $translator->translate('Objects cache cleared successfully.'); |
58
|
|
|
} else { |
59
|
|
|
$message = $translator->translate('Failed to clear objects cache.'); |
60
|
|
|
} |
61
|
|
|
} elseif ($cacheType === 'item') { |
62
|
|
|
$message = $translator->translate('Deleting cache items is unsupported, for now.'); |
63
|
|
|
} else { |
64
|
|
|
$this->addFeedback('error', $translator->translate(sprintf('Invalid cache type "%s"', $cacheType))); |
65
|
|
|
$this->setSuccess(false); |
66
|
|
|
return $response->withStatus(400); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->setSuccess($result); |
70
|
|
|
|
71
|
|
|
if ($result) { |
72
|
|
|
$this->addFeedback('success', $message); |
73
|
|
|
return $response; |
74
|
|
|
} else { |
75
|
|
|
$this->addFeedback('error', $message); |
76
|
|
|
return $response->withStatus(500); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Clear the global cache. |
82
|
|
|
* |
83
|
|
|
* @return boolean TRUE if cache type cleared, FALSE otherwise. |
84
|
|
|
*/ |
85
|
|
|
private function clearGlobalCache() |
86
|
|
|
{ |
87
|
|
|
$cache = $this->cachePool(); |
88
|
|
|
$result = $cache->clear(); |
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Clear the pages cache. |
94
|
|
|
* |
95
|
|
|
* @return boolean TRUE if cache type cleared, FALSE otherwise. |
96
|
|
|
*/ |
97
|
|
|
private function clearPagesCache() |
98
|
|
|
{ |
99
|
|
|
$cache = $this->cachePool(); |
100
|
|
|
$result = $cache->deleteItems([ 'request', 'template' ]); |
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Clear the objects cache. |
106
|
|
|
* |
107
|
|
|
* @return boolean TRUE if cache type cleared, FALSE otherwise. |
108
|
|
|
*/ |
109
|
|
|
private function clearObjectsCache() |
110
|
|
|
{ |
111
|
|
|
$cache = $this->cachePool(); |
112
|
|
|
$result = $cache->deleteItems([ 'object', 'metadata' ]); |
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|