-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandFactory.php
More file actions
223 lines (187 loc) · 6.18 KB
/
CommandFactory.php
File metadata and controls
223 lines (187 loc) · 6.18 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
namespace Smeghead\PhpVariableHardUsage\Option;
use Smeghead\PhpVariableHardUsage\Command\CheckCommand;
use Smeghead\PhpVariableHardUsage\Command\CommandInterface;
use Smeghead\PhpVariableHardUsage\Command\HelpCommand;
use Smeghead\PhpVariableHardUsage\Command\ScopesCommand;
use Smeghead\PhpVariableHardUsage\Command\SingleCommand;
use Smeghead\PhpVariableHardUsage\Command\VersionCommand;
/**
* コマンドライン引数を解析し、適切なコマンドと引数を生成するクラス
*/
final class CommandFactory
{
/** @var array<string> */
private array $argv;
/**
* @param array<string> $argv コマンドライン引数
*/
public function __construct(array $argv)
{
$this->argv = $argv;
}
/**
* コマンドライン引数を解析し、コマンドと引数を返す
*/
public function create(): CommandInterface
{
// 引数がない場合はヘルプコマンド
if (count($this->argv) < 2) {
return new HelpCommand();
}
$command = $this->argv[1];
// ヘルプと バージョン表示は特別処理
if ($command === '--help') {
return new HelpCommand();
}
if ($command === '--version') {
return new VersionCommand();
}
// コマンドに応じた処理
switch ($command) {
case 'single':
return $this->parseSingleCommand();
case 'scopes':
return $this->parseScopesCommand();
case 'check':
return $this->parseCheckCommand();
default:
// 後方互換性のため、引数そのものをファイル名として解釈
return new SingleCommand($command);
}
}
/**
* 単一ファイルコマンドを解析
*/
private function parseSingleCommand(): CommandInterface
{
$args = array_slice($this->argv, 2);
if (empty($args)) {
return new HelpCommand();
}
return new SingleCommand($args[0]);
}
/**
* スコープコマンドを解析
*/
private function parseScopesCommand(): CommandInterface
{
$args = array_slice($this->argv, 2);
if (empty($args)) {
return new HelpCommand();
}
return new ScopesCommand($args);
}
/**
* チェックコマンドを解析
*/
private function parseCheckCommand(): CommandInterface
{
$args = array_slice($this->argv, 2);
if (empty($args)) {
return new HelpCommand();
}
$parsedArgs = $this->parseArguments($args);
if (empty($parsedArgs->paths)) {
return new HelpCommand();
}
$threshold = isset($parsedArgs->options['threshold']) ? intval($parsedArgs->options['threshold']) : null;
return new CheckCommand($parsedArgs->paths, $threshold);
}
/**
* コマンドライン引数を解析して、オプションとパスに分離する
*
* @param array<string> $args
* @return ParsedArguments
*/
private function parseArguments(array $args): ParsedArguments
{
$options = [];
$paths = [];
$i = 0;
while ($i < count($args)) {
$arg = $args[$i];
if ($this->isOptionWithValue($arg, '--threshold', $args, $i)) {
$options['threshold'] = (int)$args[$i + 1];
$i += 2;
} elseif ($this->isOptionWithInlineValue($arg, '--threshold=', $matches)) {
$options['threshold'] = (int)$matches[1];
$i++;
} elseif ($this->isOption($arg)) {
[$name, $value] = $this->parseOption($arg);
$options[$name] = $value;
$i++;
} else {
$paths[] = $arg;
$i++;
}
}
return new ParsedArguments($paths, $options);
}
/**
* 値を持つオプションかどうかを判定
*
* @param string $arg 現在の引数
* @param string $optionName オプション名
* @param array<string> $args 全引数
* @param int $index 現在の位置
* @return bool
*/
private function isOptionWithValue(string $arg, string $optionName, array $args, int $index): bool
{
return $arg === $optionName && isset($args[$index + 1]);
}
/**
* インライン値を持つオプションかどうかを判定
*
* @param string $arg 現在の引数
* @param string $prefix オプションのプレフィックス
* @param null &$matches 正規表現のマッチ結果を格納する変数
* @return bool
*/
private function isOptionWithInlineValue(string $arg, string $prefix, &$matches): bool
{
return preg_match('/^' . preg_quote($prefix, '/') . '(\d+)$/', $arg, $matches) === 1;
}
/**
* オプションかどうかを判定
*
* @param string $arg 現在の引数
* @return bool
*/
private function isOption(string $arg): bool
{
return strpos($arg, '--') === 0;
}
/**
* オプション文字列をパースして名前と値を取得
*
* @param string $option オプション文字列
* @return array{0: string, 1: string|bool} [オプション名, オプション値]
*/
private function parseOption(string $option): array
{
$optName = substr($option, 2);
if (strpos($optName, '=') !== false) {
[$name, $value] = explode('=', $optName, 2);
return [$name, $value];
}
return [$optName, true];
}
}
/**
* パース済みの引数を表すクラス
*/
final class ParsedArguments
{
/**
* @param array<string> $paths パスのリスト
* @param array<string, string|int|bool|null> $options オプションのマップ
*/
public function __construct(
public readonly array $paths,
public readonly array $options
) {
}
}