-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClearCache.php
More file actions
96 lines (75 loc) · 3.47 KB
/
ClearCache.php
File metadata and controls
96 lines (75 loc) · 3.47 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
namespace MagicPush\CliToolkit\Parametizer\ScriptClass\ScriptClassLauncher\Subcommand\ClearCache;
use MagicPush\CliToolkit\Parametizer\CliRequest\CliRequest;
use MagicPush\CliToolkit\Parametizer\Config\Builder\ConfigBuilder;
use MagicPush\CliToolkit\Parametizer\EnvironmentConfig;
use MagicPush\CliToolkit\Parametizer\HelpFormatter;
use MagicPush\CliToolkit\Parametizer\ScriptClass\ScriptClassLauncher\Subcommand\ScriptLauncherSubcommandAbstract;
use MagicPush\CliToolkit\Parametizer\ScriptDetector\ScriptClassDetector;
use MagicPush\CliToolkit\Utils;
use RuntimeException;
class ClearCache extends ScriptLauncherSubcommandAbstract {
protected readonly HelpFormatter $formatterOutput;
protected readonly HelpFormatter $formatterError;
protected readonly bool $isVerbose;
/**
* @param ClearCacheContext|null $context The actual value must not be `null`.
* The availability of a default value is made solely for the compliance
* with the parent abstract method signature.
*/
public static function getConfigBuilder(
?EnvironmentConfig $envConfig = null,
bool $throwOnException = false,
?ClearCacheContext $context = null,
): ConfigBuilder {
$configBuilder = parent::getConfigBuilder($envConfig, $throwOnException);
if (null === $context) {
throw new RuntimeException(ClearCacheContext::class . ' instance is not set');
}
$formatter = HelpFormatter::createForStdOut();
$detectorClassLastNameFormatted = $formatter->helpNote(Utils::getClassShortName(ScriptClassDetector::class));
$configBuilder
->shortDescription("Removes {$detectorClassLastNameFormatted}'s cache file.")
->description("
Removes {$detectorClassLastNameFormatted}'s cache file: "
. $formatter->paramValue($context->detectorCacheFilePath) . "
")
->newFlag('--verbose', '-v')
->description('Print various messages during the subcommand execution.');
return $configBuilder;
}
public function __construct(CliRequest $request, protected readonly ClearCacheContext $context) {
parent::__construct($request);
$this->formatterOutput = HelpFormatter::createForStdOut();
$this->formatterError = HelpFormatter::createForStdErr();
$this->isVerbose = $request->getParamAsBool('verbose');
}
public function execute(): void {
$this->logOutput(
'Deleting the script detector cache file '
. $this->formatterOutput->paramValue($this->context->detectorCacheFilePath)
. '...',
);
if (unlink($this->context->detectorCacheFilePath)) {
$this->logOutput(' OK' . PHP_EOL);
} else {
$this->logOutput(PHP_EOL)
->logError(
$this->formatterError->error('Unable to delete the script detector cache file: ')
. $this->formatterError->paramValue($this->context->detectorCacheFilePath)
. PHP_EOL,
);
}
}
protected function logOutput(string $message): static {
if ($this->isVerbose) {
echo $message;
}
return $this;
}
protected function logError(string $message): static {
fwrite(STDERR, $message);
return $this;
}
}