-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasCacheOption.php
More file actions
165 lines (145 loc) · 4.74 KB
/
HasCacheOption.php
File metadata and controls
165 lines (145 loc) · 4.74 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console\Input;
use Throwable;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Path;
/**
* Provides the standard cache option used by cache-writing commands.
*/
trait HasCacheOption
{
/**
* Adds the standard cache control option to the current command.
*
* @param string $description the cache option description
*
* @return static
*/
protected function addCacheOption(string $description): static
{
if ($this->getDefinition()->hasOption('cache')) {
return $this;
}
return $this->addOption(name: 'cache', mode: InputOption::VALUE_NONE, description: $description);
}
/**
* Adds the standard cache directory option to the current command.
*
* @param string $description the cache directory option description
* @param string $default the command-specific default cache directory
*
* @return static
*/
protected function addCacheDirOption(string $description, string $default): static
{
if ($this->getDefinition()->hasOption('cache-dir')) {
return $this;
}
return $this->addOption(
name: 'cache-dir',
mode: InputOption::VALUE_OPTIONAL,
description: $description,
default: $default,
);
}
/**
* Resolves whether cache writes SHOULD be enabled for the current invocation.
*
* @param InputInterface $input the current command input
* @param bool $default the command-specific default cache behavior when the option is omitted
*/
protected function isCacheEnabled(InputInterface $input, bool $default = true): bool
{
if ((bool) $input->getOption('cache')) {
return true;
}
if ($this->isNoCacheRequested($input)) {
return false;
}
return $default;
}
/**
* Returns the explicit cache flag that SHOULD be forwarded to nested commands.
*
* @param InputInterface $input the current command input
*/
protected function resolveCacheArgument(InputInterface $input): ?string
{
if ((bool) $input->getOption('cache')) {
return '--cache';
}
if ($this->isNoCacheRequested($input)) {
return '--no-cache';
}
return null;
}
/**
* Resolves a nested cache directory for a child command.
*
* @param InputInterface $input the current command input
* @param string $path the child cache path relative to the current command cache root
*/
protected function resolveCacheDirArgument(InputInterface $input, string $path = ''): ?string
{
if (! $this->hasExplicitCacheDirArgument($input)) {
return null;
}
try {
$cacheDir = $input->getOption('cache-dir');
} catch (InvalidArgumentException) {
return null;
}
if (! \is_string($cacheDir) || '' === $cacheDir) {
return null;
}
return '' === $path
? $cacheDir
: Path::join($cacheDir, $path);
}
/**
* Determines whether the current invocation explicitly passed `--cache-dir`.
*
* @param InputInterface $input the current command input
*/
private function hasExplicitCacheDirArgument(InputInterface $input): bool
{
try {
return $input->hasParameterOption('--cache-dir', true);
} catch (Throwable) {
return false;
}
}
/**
* Determines whether cache writes were explicitly disabled for the current invocation.
*
* The Composer application already provides a global `--no-cache` flag, so commands
* SHALL reuse that switch instead of redefining a local negated variant.
*
* @param InputInterface $input the current command input
*/
private function isNoCacheRequested(InputInterface $input): bool
{
try {
return true === $input->getOption('no-cache');
} catch (InvalidArgumentException) {
return false;
}
}
}