-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FlushCacheCommand.php
80 lines (68 loc) · 2.32 KB
/
FlushCacheCommand.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Shel\Neos\Terminal\Command;
/**
* This file is part of the Shel.Neos.Terminal package.
*
* (c) 2021 Sebastian Helzle
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cache\CacheManager;
use Neos\Flow\I18n\Translator;
use Shel\Neos\Terminal\Domain\CommandContext;
use Shel\Neos\Terminal\Domain\CommandInvocationResult;
class FlushCacheCommand implements TerminalCommandInterface
{
/**
* @Flow\Inject
* @var CacheManager
*/
protected $cacheManager;
/**
* @Flow\Inject
* @var Translator
*/
protected $translator;
public static function getCommandName(): string
{
return 'flushCache';
}
public static function getCommandDescription(): string
{
return 'Shel.Neos.Terminal:Main:command.flushCache.description';
}
public static function getCommandUsage(): string
{
return 'flushCache [<string>]';
}
public function invokeCommand(string $argument, CommandContext $commandContext): CommandInvocationResult
{
$cacheIdentifier = $argument;
$success = true;
if ($cacheIdentifier) {
if ($this->cacheManager->hasCache($cacheIdentifier)) {
$this->cacheManager->getCache($cacheIdentifier)->flush();
$result = $this->translator->translateById('command.flushCache.flushedOne', ['cacheIdentifier' => $cacheIdentifier]);
} else {
$success = false;
$result = $this->translator->translateById('command.flushCache.cacheDoesNotExist', ['cacheIdentifier' => $cacheIdentifier]);
}
} else {
$this->cacheManager->flushCaches();
$result = $this->translator->translateById('command.flushCache.flushedAll');
}
// Echo response as we have to exit the process prematurely or the application
// will throw errors due to the flushed caches.
// TODO: Find out if there is a better way to do this
header('Content-Type: application/json');
echo json_encode([
'success' => $success,
'result' => $result,
]);
exit;
}
}