Skip to content

Commit 9b6f42f

Browse files
committed
patch: use simple function t() instead method translate
1 parent 3bd4243 commit 9b6f42f

15 files changed

+98
-96
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
"autoload": {
3030
"psr-4": {
3131
"Ahc\\Cli\\": "src/"
32-
}
32+
},
33+
"files": [
34+
"src/functions.php"
35+
]
3336
},
3437
"autoload-dev": {
3538
"psr-4": {

src/Application.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
*/
4141
class Application
4242
{
43-
use InflectsString;
44-
4543
/**
4644
* Locale of CLI.
4745
*/
@@ -189,7 +187,7 @@ public function add(Command $command, string $alias = '', bool $default = false)
189187
$this->aliases[$alias] ??
190188
null
191189
) {
192-
throw new InvalidArgumentException($this->translate('Command "%s" already added', [$name]));
190+
throw new InvalidArgumentException(t('Command "%s" already added', [$name]));
193191
}
194192

195193
if ($alias) {
@@ -218,7 +216,7 @@ public function add(Command $command, string $alias = '', bool $default = false)
218216
public function defaultCommand(string $commandName): self
219217
{
220218
if (!isset($this->commands[$commandName])) {
221-
throw new InvalidArgumentException($this->translate('Command "%s" does not exist', [$commandName]));
219+
throw new InvalidArgumentException(t('Command "%s" does not exist', [$commandName]));
222220
}
223221

224222
$this->default = $commandName;
@@ -414,8 +412,8 @@ public function showHelp(): mixed
414412
public function showDefaultHelp(): mixed
415413
{
416414
$writer = $this->io()->writer();
417-
$header = "{$this->name}, {$this->translate('version')} {$this->version}";
418-
$footer = $this->translate('Run `<command> --help` for specific help');
415+
$header = "{$this->name}, " . t('version') . " {$this->version}";
416+
$footer = t('Run `<command> --help` for specific help');
419417

420418
if ($this->logo) {
421419
$writer->logo($this->logo, true);

src/Helper/InflectsString.php

-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Ahc\Cli\Helper;
1313

14-
use Ahc\Cli\Application;
15-
1614
use function lcfirst;
1715
use function mb_strwidth;
1816
use function mb_substr;
@@ -77,15 +75,4 @@ public function substr(string $string, int $start, ?int $length = null): string
7775

7876
return substr($string, $start, $length);
7977
}
80-
81-
/**
82-
* Translates a message using the translator.
83-
*/
84-
public static function translate(string $text, array $args = []): string
85-
{
86-
$translations = Application::$locales[Application::$locale] ?? [];
87-
$text = $translations[$text] ?? $text;
88-
89-
return sprintf($text, ...$args);
90-
}
9178
}

src/Helper/OutputHelper.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Ahc\Cli\Output\Writer;
2121
use Throwable;
2222

23+
use function Ahc\Cli\t;
2324
use function array_map;
2425
use function array_shift;
2526
use function asort;
@@ -79,7 +80,7 @@ public function printTrace(Throwable $e): void
7980

8081
$this->writer->colors(
8182
"{$eClass} <red>{$e->getMessage()}</end><eol/>" .
82-
"({$this->translate('thrown in')} <yellow>{$e->getFile()}</end><white>:{$e->getLine()})</end>"
83+
'(' . t('thrown in') . " <yellow>{$e->getFile()}</end><white>:{$e->getLine()})</end>"
8384
);
8485

8586
// @codeCoverageIgnoreStart
@@ -89,7 +90,7 @@ public function printTrace(Throwable $e): void
8990
}
9091
// @codeCoverageIgnoreEnd
9192

92-
$traceStr = "<eol/><eol/><bold>{$this->translate('Stack Trace')}:</end><eol/><eol/>";
93+
$traceStr = '<eol/><eol/><bold>' . t('Stack Trace') . ':</end><eol/><eol/>';
9394

9495
foreach ($e->getTrace() as $i => $trace) {
9596
$trace += ['class' => '', 'type' => '', 'function' => '', 'file' => '', 'line' => '', 'args' => []];
@@ -99,7 +100,7 @@ public function printTrace(Throwable $e): void
99100
$traceStr .= " <comment>$i)</end> <red>$symbol</end><comment>($args)</end>";
100101
if ('' !== $trace['file']) {
101102
$file = realpath($trace['file']);
102-
$traceStr .= "<eol/> <yellow>{$this->translate('at')} $file</end><white>:{$trace['line']}</end><eol/>";
103+
$traceStr .= "<eol/> <yellow>" . t('at') . " $file</end><white>:{$trace['line']}</end><eol/>";
103104
}
104105
}
105106

@@ -187,7 +188,7 @@ protected function showHelp(string $for, array $items, string $header = '', stri
187188
$this->writer->help_header($header, true);
188189
}
189190

190-
$this->writer->eol()->help_category($this->translate($for) . ':', true);
191+
$this->writer->eol()->help_category(t($for) . ':', true);
191192

192193
if (empty($items)) {
193194
$this->writer->help_text(' (n/a)', true);
@@ -231,7 +232,7 @@ public function showUsage(string $usage): self
231232
$usage = str_replace('$0', $_SERVER['argv'][0] ?? '[cmd]', $usage);
232233

233234
if (!str_contains($usage, ' ## ')) {
234-
$this->writer->eol()->help_category($this->translate('Usage Examples') . ':', true)->colors($usage)->eol();
235+
$this->writer->eol()->help_category(t('Usage Examples') . ':', true)->colors($usage)->eol();
235236

236237
return $this;
237238
}
@@ -248,7 +249,7 @@ public function showUsage(string $usage): self
248249
return str_pad('# ', $maxlen - array_shift($lines), ' ', STR_PAD_LEFT);
249250
}, $usage);
250251

251-
$this->writer->eol()->help_category($this->translate('Usage Examples') . ':', true)->colors($usage)->eol();
252+
$this->writer->eol()->help_category(t('Usage Examples') . ':', true)->colors($usage)->eol();
252253

253254
return $this;
254255
}
@@ -263,11 +264,11 @@ public function showCommandNotFound(string $attempted, array $available): self
263264
}
264265
}
265266

266-
$this->writer->error($this->translate('Command %s not found', [$attempted]), true);
267+
$this->writer->error(t('Command %s not found', [$attempted]), true);
267268
if ($closest) {
268269
asort($closest);
269270
$closest = key($closest);
270-
$this->writer->bgRed($this->translate('Did you mean %s?', [$closest]), true);
271+
$this->writer->bgRed(t('Did you mean %s?', [$closest]), true);
271272
}
272273

273274
return $this;

src/Helper/Shell.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Ahc\Cli\Exception\RuntimeException;
1515

16+
use function Ahc\Cli\t;
1617
use function fclose;
1718
use function function_exists;
1819
use function fwrite;
@@ -37,8 +38,6 @@
3738
*/
3839
class Shell
3940
{
40-
use InflectsString;
41-
4241
const STDIN_DESCRIPTOR_KEY = 0;
4342
const STDOUT_DESCRIPTOR_KEY = 1;
4443
const STDERR_DESCRIPTOR_KEY = 2;
@@ -101,7 +100,7 @@ public function __construct(protected string $command, protected ?string $input
101100
{
102101
// @codeCoverageIgnoreStart
103102
if (!function_exists('proc_open')) {
104-
throw new RuntimeException($this->translate('Required proc_open could not be found in your PHP setup.'));
103+
throw new RuntimeException(t('Required proc_open could not be found in your PHP setup.'));
105104
}
106105
// @codeCoverageIgnoreEnd
107106

@@ -183,7 +182,7 @@ protected function checkTimeout(): void
183182
if ($executionDuration > $this->processTimeout) {
184183
$this->kill();
185184

186-
throw new RuntimeException($this->translate('Timeout occurred, process terminated.'));
185+
throw new RuntimeException(t('Timeout occurred, process terminated.'));
187186
}
188187
// @codeCoverageIgnoreStart
189188
}
@@ -218,7 +217,7 @@ public function setOptions(
218217
public function execute(bool $async = false, ?array $stdin = null, ?array $stdout = null, ?array $stderr = null): self
219218
{
220219
if ($this->isRunning()) {
221-
throw new RuntimeException($this->translate('Process is already running.'));
220+
throw new RuntimeException(t('Process is already running.'));
222221
}
223222

224223
$this->descriptors = $this->prepareDescriptors($stdin, $stdout, $stderr);
@@ -236,7 +235,7 @@ public function execute(bool $async = false, ?array $stdin = null, ?array $stdou
236235

237236
// @codeCoverageIgnoreStart
238237
if (!is_resource($this->process)) {
239-
throw new RuntimeException($this->translate('Bad program could not be started.'));
238+
throw new RuntimeException(t('Bad program could not be started.'));
240239
}
241240
// @codeCoverageIgnoreEnd
242241

src/IO/Interactor.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace Ahc\Cli\IO;
1313

14-
use Ahc\Cli\Helper\InflectsString;
1514
use Ahc\Cli\Input\Reader;
1615
use Ahc\Cli\Output\Writer;
1716
use Throwable;
1817

18+
use function Ahc\Cli\t;
1919
use function array_keys;
2020
use function array_map;
2121
use function count;
@@ -196,8 +196,6 @@
196196
*/
197197
class Interactor
198198
{
199-
use InflectsString;
200-
201199
protected Reader $reader;
202200
protected Writer $writer;
203201

@@ -315,7 +313,7 @@ public function choices(string $text, array $choices, $default = null, bool $cas
315313
*/
316314
public function prompt(string $text, $default = null, ?callable $fn = null, int $retry = 3): mixed
317315
{
318-
$error = $this->translate('Invalid value. Please try again!');
316+
$error = t('Invalid value. Please try again!');
319317
$hidden = func_get_args()[4] ?? false;
320318
$readFn = ['read', 'readHidden'][(int) $hidden];
321319

@@ -373,7 +371,7 @@ protected function listOptions(array $choices, $default = null, bool $multi = fa
373371
$this->writer->eol()->choice(str_pad(" [$choice]", $maxLen + 6))->answer($desc);
374372
}
375373

376-
$label = $this->translate($multi ? 'Choices (comma separated)' : 'Choice');
374+
$label = t($multi ? 'Choices (comma separated)' : 'Choice');
377375

378376
$this->writer->eol()->question($label);
379377

src/Input/Command.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
use Ahc\Cli\Output\Writer;
2222
use Closure;
2323

24+
use function Ahc\Cli\t;
2425
use function array_filter;
2526
use function array_keys;
2627
use function end;
2728
use function explode;
2829
use function func_num_args;
29-
use function sprintf;
3030
use function str_contains;
3131
use function strstr;
3232

@@ -83,9 +83,9 @@ public function __construct(
8383
*/
8484
protected function defaults(): self
8585
{
86-
$this->option('-h, --help', $this->translate('Show help'))->on([$this, 'showHelp']);
87-
$this->option('-V, --version', $this->translate('Show version'))->on([$this, 'showVersion']);
88-
$this->option('-v, --verbosity', $this->translate('Verbosity level'), null, 0)->on(
86+
$this->option('-h, --help', t('Show help'))->on([$this, 'showHelp']);
87+
$this->option('-V, --version', t('Show version'))->on([$this, 'showVersion']);
88+
$this->option('-v, --verbosity', t('Verbosity level'), null, 0)->on(
8989
fn () => $this->set('verbosity', ($this->verbosity ?? 0) + 1) && false
9090
);
9191

@@ -196,7 +196,7 @@ public function argument(string $raw, string $desc = '', $default = null): self
196196
$argument = new Argument($raw, $desc, $default);
197197

198198
if ($this->_argVariadic) {
199-
throw new InvalidParameterException($this->translate('Only last argument can be variadic'));
199+
throw new InvalidParameterException(t('Only last argument can be variadic'));
200200
}
201201

202202
if ($argument->variadic()) {
@@ -303,7 +303,7 @@ protected function handleUnknown(string $arg, ?string $value = null): mixed
303303

304304
// Has some value, error!
305305
if ($values) {
306-
throw new RuntimeException($this->translate('Option "%s" not registered', [$arg]));
306+
throw new RuntimeException(t('Option "%s" not registered', [$arg]));
307307
}
308308

309309
// Has no value, show help!
@@ -356,13 +356,13 @@ public function showDefaultHelp(): mixed
356356
$io->logo($logo, true);
357357
}
358358

359-
$io->help_header("{$this->translate('Command')} {$this->_name}, {$this->translate('version')} {$this->_version}", true)->eol();
359+
$io->help_header(t('Command') . " {$this->_name}, " . t('version') . " {$this->_version}", true)->eol();
360360
$io->help_summary($this->_desc, true)->eol();
361-
$io->help_text("{$this->translate('Usage')}: ")->help_example("{$this->_name} {$this->translate('[OPTIONS...] [ARGUMENTS...]')}", true);
361+
$io->help_text(t('Usage') . ': ')->help_example("{$this->_name} " . t('[OPTIONS...] [ARGUMENTS...]'), true);
362362

363363
$helper
364364
->showArgumentsHelp($this->allArguments())
365-
->showOptionsHelp($this->allOptions(), '', $this->translate('Legend: <required> [optional] variadic...'));
365+
->showOptionsHelp($this->allOptions(), '', t('Legend: <required> [optional] variadic...'));
366366

367367
if ($this->_usage) {
368368
$helper->showUsage($this->_usage);

src/Input/Parameter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Ahc\Cli\Helper\InflectsString;
1515

16+
use function Ahc\Cli\t;
1617
use function json_encode;
1718
use function ltrim;
1819
use function strpos;
@@ -83,7 +84,7 @@ public function desc(bool $withDefault = false): string
8384
return $this->desc;
8485
}
8586

86-
return ltrim($this->translate('%s [default: %s]', [$this->desc, json_encode($this->default)]));
87+
return ltrim(t('%s [default: %s]', [$this->desc, json_encode($this->default)]));
8788
}
8889

8990
/**

src/Input/Parser.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
use Ahc\Cli\Exception\InvalidParameterException;
1515
use Ahc\Cli\Exception\RuntimeException;
16-
use Ahc\Cli\Helper\InflectsString;
1716
use Ahc\Cli\Helper\Normalizer;
1817
use InvalidArgumentException;
1918

19+
use function Ahc\Cli\t;
2020
use function array_diff_key;
2121
use function array_filter;
2222
use function array_key_exists;
@@ -25,7 +25,6 @@
2525
use function count;
2626
use function in_array;
2727
use function reset;
28-
use function sprintf;
2928
use function substr;
3029

3130
/**
@@ -38,8 +37,6 @@
3837
*/
3938
abstract class Parser
4039
{
41-
use InflectsString;
42-
4340
/** @var string|null The last seen variadic option name */
4441
protected ?string $_lastVariadic = null;
4542

@@ -224,9 +221,7 @@ protected function validate(): void
224221
[$name, $label] = [$item->long(), 'Option'];
225222
}
226223

227-
throw new RuntimeException(
228-
$this->translate('%s "%s" is required', [$label, $name])
229-
);
224+
throw new RuntimeException(t('%s "%s" is required', [$label, $name]));
230225
}
231226
}
232227

@@ -267,7 +262,7 @@ public function unset(string $name): self
267262
protected function ifAlreadyRegistered(Parameter $param): void
268263
{
269264
if ($this->registered($param->attributeName())) {
270-
throw new InvalidParameterException($this->translate(
265+
throw new InvalidParameterException(t(
271266
'The parameter "%s" is already registered',
272267
[$param instanceof Option ? $param->long() : $param->name()]
273268
));

0 commit comments

Comments
 (0)