Skip to content

Commit b4a3104

Browse files
committed
Merge branch 'cleanup'
2 parents db9d8d2 + 2121ef6 commit b4a3104

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1263
-363
lines changed

docs/Console.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ temporary directory, created with mode `0600` if it doesn't already exist:
2121
sys_get_temp_dir() . '/<script_basename>-<realpath_hash>-<user_id>.log'
2222
```
2323

24-
If the value of environment variable `CONSOLE_OUTPUT` is `stderr` or `stdout`,
24+
If the value of environment variable `CONSOLE_TARGET` is `stderr` or `stdout`,
2525
[Console][Console] output is also written to `STDERR` or `STDOUT` respectively.
2626

27-
If `CONSOLE_OUTPUT` is not set and the script is running on the command line:
27+
If `CONSOLE_TARGET` is not set and the script is running on the command line:
2828

2929
- If `STDERR` is a TTY and `STDOUT` is not, console messages are written to
3030
`STDERR` so output to `STDOUT` isn't tainted
@@ -47,41 +47,44 @@ target, e.g. by calling [registerTarget()][registerTarget], before any other
4747
4848
### Output methods
4949

50+
<!-- prettier-ignore -->
5051
| `Console` method | `ConsoleLevel` | Message prefix | Default output target |
5152
| ---------------- | --------------- | -------------- | ------------------------- |
52-
| `error[Once]()` | `ERROR` = `3` | ` !! ` | `STDERR` |
53-
| `warn[Once]()` | `WARNING` = `4` | ` ! ` | `STDERR` |
54-
| `info[Once]()` | `NOTICE` = `5` | `==> ` | `STDOUT` |
55-
| `log[Once]()` | `INFO` = `6` | ` -> ` | `STDOUT` |
56-
| `debug[Once]()` | `DEBUG` = `7` | `--- ` | `STDOUT` (if `DEBUG` set) |
57-
| `group()`[^2] | `NOTICE` = `5` | `>>> ` | `STDOUT` |
58-
| `logProgress()` | `INFO` = `6` | ` -> ` | `STDOUT` |
53+
| `error[Once]()` | `ERROR` = `3` | ` !! ` | `STDERR` |
54+
| `warn[Once]()` | `WARNING` = `4` | ` ! ` | `STDERR` |
55+
| `info[Once]()` | `NOTICE` = `5` | ` ==> ` | `STDOUT` |
56+
| `log[Once]()` | `INFO` = `6` | ` -> ` | `STDOUT` |
57+
| `debug[Once]()` | `DEBUG` = `7` | ` --- ` | `STDOUT` (if `DEBUG` set) |
58+
| `group()`[^2] | `NOTICE` = `5` | ` >>> ` | `STDOUT` |
59+
| `logProgress()` | `INFO` = `6` | ` -> ` | `STDOUT` |
5960

6061
[^1]: Actually a facade for [ConsoleWriter][ConsoleWriter].
61-
62-
[^2]: `Console::group()` adds a level of indentation to all `Console` output
63-
until `Console::groupEnd()` is called.
62+
[^2]:
63+
`Console::group()` adds a level of indentation to all `Console` output until
64+
`Console::groupEnd()` is called.
6465

6566
### Formatting
6667

6768
The following Markdown-like syntax is supported in [Console][Console] messages:
6869

6970
| Style | Tag | Typical appearance | Example |
7071
| ------------ | ------------------------------------------------------ | ------------------------------------- | ------------------------------------------------------------------------- |
71-
| Heading | `___` text `___`<br>`***` text `***`<br>`##` text `##` | ***Bold + primary colour*** | `___NAME___`<br>`***NAME***`<br>`## NAME` (closing delimiter is optional) |
72+
| Heading | `___` text `___`<br>`***` text `***`<br>`##` text `##` | **_Bold + primary colour_** | `___NAME___`<br>`***NAME***`<br>`## NAME` (closing delimiter is optional) |
7273
| "Bold" | `__` text `__`<br>`**` text `**` | **Bold + default colour** | `__command__`<br>`**command**` |
73-
| "Italic" | `_` text `_`<br>`*` text `*` | *Secondary colour* | `_argument_`<br>`*argument*` |
74-
| "Underline" | `<` text `>` | *<u>Secondary colour + underline</u>* | `<argument>` |
74+
| "Italic" | `_` text `_`<br>`*` text `*` | _Secondary colour_ | `_argument_`<br>`*argument*` |
75+
| "Underline" | `<` text `>` | _<u>Secondary colour + underline</u>_ | `<argument>` |
7576
| Low priority | `~~` text `~~` | <small>Dim</small> | `~~/path/to/script.php:42~~` |
7677
| Inline code | `` ` `` text `` ` `` | <code>Bold</code> | `` The input format can be specified using the `-f/--from` option. `` |
7778
| Code block | ` ``` `<br>text<br>` ``` ` | <pre><code>Unchanged</code></pre> | <pre><code>\`\`\`&#10;$baz = Foo::bar();&#10;\`\`\`</code></pre> |
7879

79-
8080
[Application]: https://lkrms.github.io/php-util/Lkrms.Container.Application.html
8181
[CliApplication]: https://lkrms.github.io/php-util/Lkrms.Cli.CliApplication.html
8282
[Console]: https://lkrms.github.io/php-util/Lkrms.Facade.Console.html
83-
[ConsoleWriter]: https://lkrms.github.io/php-util/Lkrms.Console.ConsoleWriter.html
84-
[logOutput]: https://lkrms.github.io/php-util/Lkrms.Container.Application.html#_logOutput
85-
[registerStdioTargets]: https://lkrms.github.io/php-util/Lkrms.Console.ConsoleWriter.html#_registerStdioTargets
86-
[registerTarget]: https://lkrms.github.io/php-util/Lkrms.Console.ConsoleWriter.html#_registerTarget
87-
83+
[ConsoleWriter]:
84+
https://lkrms.github.io/php-util/Lkrms.Console.ConsoleWriter.html
85+
[logOutput]:
86+
https://lkrms.github.io/php-util/Lkrms.Container.Application.html#_logOutput
87+
[registerStdioTargets]:
88+
https://lkrms.github.io/php-util/Lkrms.Console.ConsoleWriter.html#_registerStdioTargets
89+
[registerTarget]:
90+
https://lkrms.github.io/php-util/Lkrms.Console.ConsoleWriter.html#_registerTarget

lk-util/Command/Concept/Command.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
66
use Lkrms\Cli\CliCommand;
77
use Lkrms\Contract\IProvider;
8+
use Lkrms\Exception\FilesystemErrorException;
89
use Lkrms\LkUtil\Catalog\EnvVar;
910
use Lkrms\Utility\Env;
1011
use Lkrms\Utility\File;
1112
use Lkrms\Utility\Get;
13+
use Lkrms\Utility\Json;
1214
use JsonException;
1315

1416
/**
@@ -126,24 +128,25 @@ protected function getJson(string $file, ?string &$path = null, bool $associativ
126128
if ($file === '-') {
127129
$file = 'php://stdin';
128130
} else {
129-
$file = File::realpath($file);
130-
131-
if ($file === false) {
131+
try {
132+
$file = File::realpath($file);
133+
} catch (FilesystemErrorException $ex) {
132134
throw new CliInvalidArgumentsException(sprintf(
133135
'file not found: %s',
134136
$_file,
135137
));
136138
}
137139

138-
if (strpos($file, $this->App->getBasePath()) === 0) {
139-
$path = './' . substr($file, strlen($this->App->getBasePath()) + 1);
140-
} else {
141-
$path = $file;
142-
}
140+
$relative = File::relativeToParent($file, $this->App->getBasePath());
141+
$path = $relative === null ? $file : "./{$relative}";
143142
}
144143

144+
$json = File::getContents($file);
145+
145146
try {
146-
return json_decode(file_get_contents($file), $associative, 512, \JSON_THROW_ON_ERROR);
147+
return $associative
148+
? Json::parseObjectAsArray($json)
149+
: Json::parse($json);
147150
} catch (JsonException $ex) {
148151
throw new CliInvalidArgumentsException(sprintf(
149152
"invalid JSON in '%s': %s",

lk-util/Command/Generate/Concept/GenerateCommand.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Lkrms\Utility\Get;
2525
use Lkrms\Utility\Package;
2626
use Lkrms\Utility\Reflect;
27+
use Lkrms\Utility\Str;
2728
use Lkrms\Utility\Test;
2829
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
2930
use SebastianBergmann\Diff\Differ;
@@ -445,7 +446,7 @@ function ($match) use ($type, $namespace, $templates, $filename, &$inputClassTem
445446
protected function getTypeAlias(string $type, ?string $filename = null, bool $returnFqcn = true): ?string
446447
{
447448
$type = ltrim($type, '\\');
448-
$lower = strtolower($type);
449+
$lower = Str::lower($type);
449450
if ($filename !== null &&
450451
($alias = $this->InputFileTypeMaps[$filename][$lower] ?? null)) {
451452
return $this->getFqcnAlias($type, $alias, $returnFqcn);
@@ -474,15 +475,15 @@ protected function getTypeAlias(string $type, ?string $filename = null, bool $re
474475
protected function getFqcnAlias(string $fqcn, ?string $alias = null, bool $returnFqcn = true): ?string
475476
{
476477
$fqcn = ltrim($fqcn, '\\');
477-
$_fqcn = strtolower($fqcn);
478+
$_fqcn = Str::lower($fqcn);
478479

479480
// If $fqcn has already been imported, use its alias
480481
if ($lastAlias = $this->ImportMap[$_fqcn] ?? null) {
481482
return $lastAlias;
482483
}
483484

484485
$alias = $alias === null ? Get::basename($fqcn) : $alias;
485-
$_alias = strtolower($alias);
486+
$_alias = Str::lower($alias);
486487

487488
// Use $alias if it already maps to $fqcn
488489
if (($aliasFqcn = $this->AliasMap[$_alias] ?? null) &&
@@ -595,7 +596,7 @@ protected function generateImports(): array
595596
{
596597
$map = [];
597598
foreach ($this->ImportMap as $alias) {
598-
$import = $this->AliasMap[strtolower($alias)];
599+
$import = $this->AliasMap[Str::lower($alias)];
599600
if (!strcasecmp($alias, Get::basename($import))) {
600601
$map[$import] = null;
601602
continue;
@@ -787,11 +788,7 @@ protected function handleOutput($lines): void
787788
return;
788789
}
789790
if ($this->Check || !$this->ReplaceIfExists) {
790-
$basePath = Package::path();
791-
$relative = File::realpath($file);
792-
if (strpos($relative, $basePath) === 0) {
793-
$relative = substr($relative, strlen($basePath) + 1);
794-
}
791+
$relative = File::relativeToParent($file, Package::path(), $file);
795792
print (new Differ(new StrictUnifiedDiffOutputBuilder([
796793
'fromFile' => "a/$relative",
797794
'toFile' => "b/$relative",

lk-util/Command/Generate/GenerateBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ protected function run(string ...$args)
492492
$T = sprintf('T%s', $i < 0 ? '' : $i);
493493
$i++;
494494
} while (array_key_exists($T, $this->InputClassTemplates) ||
495-
array_key_exists(strtolower($T), $this->AliasMap));
495+
array_key_exists(Str::lower($T), $this->AliasMap));
496496
$templateTag = clone $templateTag;
497497
$templateTag->Name = $T;
498498
$templateTag->Variance = null;

lk-util/Command/Http/SendHttpRequest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Lkrms\Utility\Arr;
1212
use Lkrms\Utility\Convert;
1313
use Lkrms\Utility\Get;
14+
use Lkrms\Utility\Json;
15+
use Lkrms\Utility\Str;
1416

1517
/**
1618
* Sends HTTP requests to HTTP sync providers
@@ -43,7 +45,7 @@ final class SendHttpRequest extends Command
4345
private function getMethod(): string
4446
{
4547
return $this->HttpMethod
46-
?? ($this->HttpMethod = strtoupper(Arr::last($this->nameParts())));
48+
?? ($this->HttpMethod = Str::upper(Arr::last($this->nameParts())));
4749
}
4850

4951
public function description(): string
@@ -152,6 +154,6 @@ protected function run(string ...$args)
152154
$result = $array;
153155
}
154156

155-
echo json_encode($result) . "\n";
157+
echo Json::prettyPrint($result) . "\n";
156158
}
157159
}

scripts/generate.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
use Lkrms\Tests\Sync\Provider\JsonPlaceholderApi;
4949
use Lkrms\Utility\Arr;
5050
use Lkrms\Utility\File;
51+
use Lkrms\Utility\Json;
52+
use Lkrms\Utility\Package;
5153

5254
$loader = require dirname(__DIR__) . '/vendor/autoload.php';
5355

@@ -125,7 +127,7 @@ function generated($commandOrFile): void
125127
throw new LogicException('No file generated');
126128
}
127129

128-
$generated[] = '/' . File::relativeToParent($file);
130+
$generated[] = '/' . File::relativeToParent($file, Package::path());
129131
}
130132

131133
$status = 0;
@@ -166,7 +168,7 @@ function generated($commandOrFile): void
166168
generated($generateEntity);
167169
if ($save && $generateEntity->Entity !== null) {
168170
File::createDir(dirname($file));
169-
file_put_contents($file, json_encode($generateEntity->Entity, \JSON_PRETTY_PRINT));
171+
file_put_contents($file, Json::prettyPrint($generateEntity->Entity));
170172
generated($file);
171173
}
172174
}

src/Cli/CliApplication.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Lkrms\Utility\Json;
1818
use Lkrms\Utility\Package;
1919
use Lkrms\Utility\Pcre;
20+
use Lkrms\Utility\Str;
2021
use Lkrms\Utility\Sys;
2122
use LogicException;
2223

@@ -506,7 +507,7 @@ private function generateHelp(string $name, $node, int $helpType, string ...$arg
506507
$formats = TagFormats::getManPageFormats();
507508
printf(
508509
"%% %s(%d) %s | %s\n\n",
509-
strtoupper(str_replace(' ', '-', trim($this->getProgramName() . " $name"))),
510+
Str::upper(str_replace(' ', '-', trim($this->getProgramName() . " $name"))),
510511
(int) ($args[0] ?? '1'),
511512
$args[1] ?? Package::version(),
512513
$args[2] ?? (Package::name() . ' Documentation'),

src/Cli/CliOption.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public function getValueName(): string
624624
return '';
625625
}
626626

627-
return strtolower(Str::toWords($this->ValueName));
627+
return Str::lower(Str::toWords($this->ValueName));
628628
}
629629

630630
/**
@@ -658,7 +658,7 @@ public function formatValueName(bool $withMarkup = true): string
658658
strpbrk($this->ValueName, Char::ALPHABETIC_UPPER) !== false &&
659659
strpbrk($this->ValueName, Char::ALPHABETIC_LOWER) === false
660660
) {
661-
$name = strtoupper($name);
661+
$name = Str::upper($name);
662662
if (!$withMarkup) {
663663
return $name;
664664
}
@@ -866,7 +866,7 @@ public function filterValue($value, ?string $source = null, ?int $policy = null)
866866
if (!$this->CaseSensitive) {
867867
$normalised = [];
868868
foreach ($value as $value) {
869-
$lower = strtolower((string) $value);
869+
$lower = Str::lower((string) $value);
870870
$normalised[] = $this->AllowedValues[$lower] ?? $value;
871871
}
872872
$value = $normalised;

src/Concept/ConvertibleEnumeration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Lkrms\Concern\IsConvertibleEnumeration;
66
use Lkrms\Contract\IConvertibleEnumeration;
7+
use Lkrms\Utility\Str;
78
use LogicException;
89

910
/**
@@ -40,7 +41,7 @@ abstract class ConvertibleEnumeration extends Enumeration implements IConvertibl
4041
final public static function fromName(string $name)
4142
{
4243
$value = static::$ValueMap[$name]
43-
?? static::$ValueMap[strtoupper($name)]
44+
?? static::$ValueMap[Str::upper($name)]
4445
?? null;
4546
if ($value === null) {
4647
throw new LogicException(

src/Concept/ReflectiveEnumeration.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Lkrms\Concern\IsConvertibleEnumeration;
66
use Lkrms\Contract\IConvertibleEnumeration;
7+
use Lkrms\Utility\Str;
78
use LogicException;
89
use ReflectionClass;
910

@@ -60,7 +61,7 @@ private static function loadMaps(): void
6061
);
6162
}
6263
// Add UPPER_CASE names to $valueMap if not already present
63-
$valueMap += array_combine(array_map('strtoupper', array_keys($valueMap)), $valueMap);
64+
$valueMap += array_combine(array_map([Str::class, 'upper'], array_keys($valueMap)), $valueMap);
6465
self::$ValueMaps[static::class] = $valueMap;
6566
self::$NameMaps[static::class] = $nameMap;
6667
}
@@ -74,7 +75,7 @@ final public static function fromName(string $name)
7475
self::loadMaps();
7576
}
7677
$value = self::$ValueMaps[static::class][$name]
77-
?? self::$ValueMaps[static::class][strtoupper($name)]
78+
?? self::$ValueMaps[static::class][Str::upper($name)]
7879
?? null;
7980
if ($value === null) {
8081
throw new LogicException(

0 commit comments

Comments
 (0)