Skip to content

Commit bb75e81

Browse files
innocenzibrendt
andauthored
refactor(support)!: improve architecture of support utilities (#940)
Co-authored-by: brendt <[email protected]>
1 parent 16345b6 commit bb75e81

File tree

110 files changed

+5443
-2714
lines changed

Some content is hidden

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

110 files changed

+5443
-2714
lines changed

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@
122122
"src/Tempest/Mapper/src/functions.php",
123123
"src/Tempest/Reflection/src/functions.php",
124124
"src/Tempest/Router/src/functions.php",
125+
"src/Tempest/Support/src/Arr/functions.php",
126+
"src/Tempest/Support/src/Html/functions.php",
127+
"src/Tempest/Support/src/Language/functions.php",
128+
"src/Tempest/Support/src/Namespace/functions.php",
129+
"src/Tempest/Support/src/Random/functions.php",
130+
"src/Tempest/Support/src/Regex/functions.php",
131+
"src/Tempest/Support/src/Str/functions.php",
125132
"src/Tempest/Support/src/functions.php",
126133
"src/Tempest/View/src/functions.php",
127134
"src/Tempest/Vite/src/functions.php"

src/Tempest/Cache/src/ProjectCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Psr\Cache\CacheItemPoolInterface;
88
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
9-
use function Tempest\path;
9+
use function Tempest\Support\path;
1010

1111
final class ProjectCache implements Cache
1212
{

src/Tempest/Console/src/Components/Concerns/RendersControls.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Tempest\Console\Components\ComponentState;
88
use Tempest\Console\InteractiveConsoleComponent;
99
use Tempest\Console\Terminal\Terminal;
10-
use Tempest\Support\StringHelper;
1110
use function Tempest\Support\arr;
11+
use function Tempest\Support\str;
1212

1313
/**
1414
* @mixin InteractiveConsoleComponent
@@ -48,7 +48,7 @@ private function renderControls(array $controls, int $maxWidth): string
4848

4949
if ($render->stripTags()->length() >= $maxWidth) {
5050
$prefix = $marginLeft . '<style="fg-gray">·</style>';
51-
$render = new StringHelper($prefix)
51+
$render = str($prefix)
5252
->append($render)
5353
->explode($separator)
5454
->implode("\n" . $prefix . $marginLeft);

src/Tempest/Console/src/Components/Interactive/SearchComponent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Tempest\Console\Point;
2222
use Tempest\Console\StaticConsoleComponent;
2323
use Tempest\Console\Terminal\Terminal;
24-
use Tempest\Support\ArrayHelper;
24+
use function Tempest\Support\Arr\wrap;
2525

2626
final class SearchComponent implements InteractiveConsoleComponent, HasCursor, HasStaticComponent
2727
{
@@ -48,7 +48,7 @@ public function __construct(
4848
$this->options = new OptionCollection([]);
4949

5050
if ($this->multiple) {
51-
$this->default = ArrayHelper::wrap($this->default);
51+
$this->default = wrap($this->default);
5252
}
5353

5454
$this->updateQuery();

src/Tempest/Console/src/Components/OptionCollection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Countable;
88
use Iterator;
99
use Stringable;
10-
use Tempest\Support\ArrayHelper;
10+
use Tempest\Support\Arr\ImmutableArray;
1111
use UnitEnum;
1212
use function Tempest\Support\arr;
1313

@@ -35,7 +35,7 @@ public function setCollection(iterable $options): void
3535
{
3636
$options = arr($options);
3737

38-
$this->preserveKeys = $options->isAssoc();
38+
$this->preserveKeys = $options->isAssociative();
3939
$this->options = $options
4040
->map(fn (mixed $value, string|int $key) => new Option($key, $value))
4141
->toArray();
@@ -93,8 +93,8 @@ public function toggleCurrent(): void
9393
}
9494
}
9595

96-
/** @return ArrayHelper<Option> */
97-
public function getOptions(): ArrayHelper
96+
/** @return ImmutableArray<Option> */
97+
public function getOptions(): ImmutableArray
9898
{
9999
return arr($this->filteredOptions)->values();
100100
}
@@ -121,7 +121,6 @@ public function getRawSelectedOptions(array $default = []): array
121121
return $default;
122122
}
123123

124-
// TODO: PR `tap` to `ArrayHelper`
125124
if (! $this->preserveKeys) {
126125
return array_values($selected);
127126
}

src/Tempest/Console/src/Components/Renderers/InstructionsRenderer.php

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

55
namespace Tempest\Console\Components\Renderers;
66

7-
use Tempest\Support\StringHelper;
87
use function Tempest\Support\arr;
98
use function Tempest\Support\str;
109

@@ -16,10 +15,10 @@ public function render(string|array $lines): string
1615
{
1716
$lines = arr($lines)
1817
->filter()
19-
->flatMap(fn (string $string) => str($string)->split(self::MAX_WIDTH)->toArray())
18+
->flatMap(fn (string $string) => str($string)->chunk(self::MAX_WIDTH)->toArray())
2019
->toArray();
2120

22-
$text = new StringHelper(PHP_EOL);
21+
$text = str(PHP_EOL);
2322

2423
foreach ($lines as $line) {
2524
$text = $text->append(' ', '<style="bold fg-green">│</style>', ' ', trim($line), PHP_EOL);

src/Tempest/Console/src/Components/Renderers/KeyValueRenderer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Tempest\Console\Components\Renderers;
66

77
use Stringable;
8-
use Tempest\Support\StringHelper;
8+
use Tempest\Support\Str\MutableString;
99
use function Tempest\root_path;
1010
use function Tempest\Support\str;
1111

@@ -32,18 +32,18 @@ public function render(Stringable|string $key, null|Stringable|string $value = n
3232
->toString();
3333
}
3434

35-
private function cleanText(null|Stringable|string $text): StringHelper
35+
private function cleanText(null|Stringable|string $text): MutableString
3636
{
37-
$text = str($text)->trim();
37+
$text = new MutableString($text)->trim();
3838

3939
if ($text->length() === 0) {
40-
return str();
40+
return new MutableString();
4141
}
4242

4343
return $text
4444
->replaceRegex('/\[([^]]+)]/', '<em>[$1]</em>')
4545
->when(fn ($s) => $s->endsWith(['.', '?', '!', ':']), fn ($s) => $s->replaceAt(-1, 1, ''))
46-
->replace(root_path(), '')
46+
->erase(root_path())
4747
->trim();
4848
}
4949
}

src/Tempest/Console/src/Components/Renderers/MessageRenderer.php

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

55
namespace Tempest\Console\Components\Renderers;
66

7-
use Tempest\Support\ArrayHelper;
7+
use Tempest\Support\Arr\ImmutableArray;
88
use function Tempest\Support\str;
99

1010
final readonly class MessageRenderer
@@ -18,7 +18,7 @@ public function __construct(
1818
public function render(string $contents, ?string $title = null): string
1919
{
2020
$title = str($title ?? $this->label)->toString();
21-
$lines = ArrayHelper::explode($contents, "\n")
21+
$lines = ImmutableArray::explode($contents, "\n")
2222
->map(fn ($s, $i) => str_repeat(' ', ($i === 0 ? 1 : strlen($title) + 4)) . $s)
2323
->implode("\n");
2424

src/Tempest/Console/src/Components/Renderers/RendersInput.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Stringable;
88
use Tempest\Console\Components\ComponentState;
99
use Tempest\Console\Terminal\Terminal;
10-
use Tempest\Support\StringHelper;
10+
use Tempest\Support\Str\ImmutableString;
1111

1212
trait RendersInput
1313
{
@@ -17,7 +17,7 @@ trait RendersInput
1717

1818
public const int MARGIN_TOP = 1;
1919

20-
private StringHelper $frame;
20+
private ImmutableString $frame;
2121

2222
private Terminal $terminal;
2323

@@ -46,7 +46,7 @@ private function prepareRender(Terminal $terminal, ComponentState $state): self
4646
$this->leftBorder = "<style=\"dim {$this->getStyle()}\">│</style>";
4747
$this->maxLineCharacters = $this->terminal->width - mb_strlen($this->marginX . ' ' . $this->paddingX) - self::MARGIN_X;
4848

49-
$this->frame = new StringHelper(str_repeat("\n", self::MARGIN_TOP));
49+
$this->frame = new ImmutableString(str_repeat("\n", self::MARGIN_TOP));
5050

5151
return $this;
5252
}
@@ -66,7 +66,7 @@ private function truncate(?string $string = null, int $maxLineOffset = 0): strin
6666
return '';
6767
}
6868

69-
return new StringHelper($string)
69+
return new ImmutableString($string)
7070
->truncate($this->maxLineCharacters - 1 - $maxLineOffset, end: '') // -1 is for the ellipsis
7171
->toString();
7272
}

src/Tempest/Console/src/Components/Renderers/TextInputRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function render(
4141
// splits the text to an array so we can work with individual lines
4242
$lines = str(($buffer->text ?: $placeholder) ?: '')
4343
->explode("\n")
44-
->flatMap(fn (string $line) => str($line)->split($this->maxLineCharacters)->toArray())
44+
->flatMap(fn (string $line) => str($line)->chunk($this->maxLineCharacters)->toArray())
4545
->map(static fn (string $line) => str($line)->replaceEnd("\n", ' '));
4646

4747
// calculates scroll offset based on cursor position

0 commit comments

Comments
 (0)