Skip to content

Commit 0dacd10

Browse files
authored
refactor(support): convert PathHelper to immutable class (#743)
1 parent 3621006 commit 0dacd10

31 files changed

+298
-211
lines changed

src/Tempest/Cache/src/ProjectCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818
private readonly CacheConfig $cacheConfig,
1919
) {
2020
$this->pool = $this->cacheConfig->projectCachePool ?? new FilesystemAdapter(
21-
directory: path($this->cacheConfig->directory, 'project'),
21+
directory: path($this->cacheConfig->directory, 'project')->toString(),
2222
);
2323
}
2424

src/Tempest/Console/src/ConsoleApplication.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Tempest\Core\Tempest;
1414
use Tempest\Log\Channels\AppendLogChannel;
1515
use Tempest\Log\LogConfig;
16-
use Tempest\Support\PathHelper;
16+
use function Tempest\path;
1717
use Throwable;
1818

1919
final readonly class ConsoleApplication implements Application
@@ -45,8 +45,8 @@ public static function boot(
4545
$logConfig->debugLogPath === null
4646
&& $logConfig->channels === []
4747
) {
48-
$logConfig->debugLogPath = PathHelper::make($container->get(Kernel::class)->root, '/log/debug.log');
49-
$logConfig->channels[] = new AppendLogChannel(PathHelper::make($container->get(Kernel::class)->root, '/log/tempest.log'));
48+
$logConfig->debugLogPath = path($container->get(Kernel::class)->root, '/log/debug.log')->toString();
49+
$logConfig->channels[] = new AppendLogChannel(path($container->get(Kernel::class)->root, '/log/tempest.log')->toString());
5050
}
5151

5252
return $application;

src/Tempest/Console/src/Initializers/LogOutputBufferInitializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Tempest\Container\Initializer;
1111
use Tempest\Container\Singleton;
1212
use Tempest\Core\Kernel;
13-
use Tempest\Support\PathHelper;
13+
use function Tempest\path;
1414

1515
final readonly class LogOutputBufferInitializer implements Initializer
1616
{
@@ -20,7 +20,7 @@ public function initialize(Container $container): LogOutputBuffer
2020
$consoleConfig = $container->get(ConsoleConfig::class);
2121
$kernel = $container->get(Kernel::class);
2222

23-
$path = $consoleConfig->logPath ?? PathHelper::make($kernel->root, 'console.log');
23+
$path = $consoleConfig->logPath ?? path($kernel->root, 'console.log')->toString();
2424

2525
return new LogOutputBuffer($path);
2626
}

src/Tempest/Core/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"require": {
77
"php": "^8.3",
88
"tempest/container": "dev-main",
9+
"tempest/support": "dev-main",
910
"vlucas/phpdotenv": "^5.6",
1011
"filp/whoops": "^2.15"
1112
},

src/Tempest/Core/src/Composer.php

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

55
namespace Tempest\Core;
66

7+
use function Tempest\path;
78
use function Tempest\Support\arr;
8-
use Tempest\Support\PathHelper;
99

1010
final class Composer
1111
{
@@ -19,7 +19,7 @@ final class Composer
1919
public function __construct(
2020
string $root,
2121
) {
22-
$composerFilePath = PathHelper::make($root, 'composer.json');
22+
$composerFilePath = path($root, 'composer.json')->toString();
2323

2424
$this->composer = $this->loadComposerFile($composerFilePath);
2525
$this->namespaces = arr($this->composer)

src/Tempest/Core/src/DiscoveryCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
?CacheItemPoolInterface $pool = null,
2828
) {
2929
$this->pool = $pool ?? new FilesystemAdapter(
30-
directory: path($this->cacheConfig->directory, 'discovery'),
30+
directory: path($this->cacheConfig->directory, 'discovery')->toString(),
3131
);
3232
}
3333

src/Tempest/Core/src/Kernel/LoadDiscoveryLocations.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Tempest\Core\DiscoveryException;
99
use Tempest\Core\DiscoveryLocation;
1010
use Tempest\Core\Kernel;
11-
use Tempest\Support\PathHelper;
11+
use function Tempest\path;
1212

1313
/** @internal */
1414
final readonly class LoadDiscoveryLocations
@@ -35,8 +35,8 @@ public function __invoke(): void
3535
*/
3636
private function discoverCorePackages(): array
3737
{
38-
$composerPath = PathHelper::make($this->kernel->root, 'vendor/composer');
39-
$installed = $this->loadJsonFile(PathHelper::make($composerPath, 'installed.json'));
38+
$composerPath = path($this->kernel->root, 'vendor/composer');
39+
$installed = $this->loadJsonFile(path($composerPath, 'installed.json')->toString());
4040
$packages = $installed['packages'] ?? [];
4141

4242
$discoveredLocations = [];
@@ -49,12 +49,12 @@ private function discoverCorePackages(): array
4949
continue;
5050
}
5151

52-
$packagePath = PathHelper::make($composerPath, $package['install-path'] ?? '');
52+
$packagePath = path($composerPath, $package['install-path'] ?? '');
5353

5454
foreach ($package['autoload']['psr-4'] as $namespace => $namespacePath) {
55-
$namespacePath = PathHelper::make($packagePath, $namespacePath);
55+
$namespacePath = path($packagePath, $namespacePath);
5656

57-
$discoveredLocations[] = new DiscoveryLocation($namespace, $namespacePath);
57+
$discoveredLocations[] = new DiscoveryLocation($namespace, $namespacePath->toString());
5858
}
5959
}
6060

@@ -69,9 +69,9 @@ private function discoverAppNamespaces(): array
6969
$discoveredLocations = [];
7070

7171
foreach ($this->composer->namespaces as $namespace) {
72-
$path = PathHelper::make($this->kernel->root, $namespace->path);
72+
$path = path($this->kernel->root, $namespace->path);
7373

74-
$discoveredLocations[] = new DiscoveryLocation($namespace->namespace, $path);
74+
$discoveredLocations[] = new DiscoveryLocation($namespace->namespace, $path->toString());
7575
}
7676

7777
return $discoveredLocations;
@@ -82,8 +82,8 @@ private function discoverAppNamespaces(): array
8282
*/
8383
private function discoverVendorPackages(): array
8484
{
85-
$composerPath = PathHelper::make($this->kernel->root, 'vendor/composer');
86-
$installed = $this->loadJsonFile(PathHelper::make($composerPath, 'installed.json'));
85+
$composerPath = path($this->kernel->root, 'vendor/composer');
86+
$installed = $this->loadJsonFile(path($composerPath, 'installed.json')->toString());
8787
$packages = $installed['packages'] ?? [];
8888

8989
$discoveredLocations = [];
@@ -96,7 +96,7 @@ private function discoverVendorPackages(): array
9696
continue;
9797
}
9898

99-
$packagePath = PathHelper::make($composerPath, $package['install-path'] ?? '');
99+
$packagePath = path($composerPath, $package['install-path'] ?? '');
100100
$requiresTempest = isset($package['require']['tempest/framework']) || isset($package['require']['tempest/core']);
101101
$hasPsr4Namespaces = isset($package['autoload']['psr-4']);
102102

@@ -105,9 +105,9 @@ private function discoverVendorPackages(): array
105105
}
106106

107107
foreach ($package['autoload']['psr-4'] as $namespace => $namespacePath) {
108-
$path = PathHelper::make($packagePath, $namespacePath);
108+
$path = path($packagePath, $namespacePath);
109109

110-
$discoveredLocations[] = new DiscoveryLocation($namespace, $path);
110+
$discoveredLocations[] = new DiscoveryLocation($namespace, $path->toString());
111111
}
112112
}
113113

src/Tempest/Core/src/PublishesFiles.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
1414
use Tempest\Generation\Exceptions\FileGenerationFailedException;
1515
use Tempest\Generation\StubFileGenerator;
16-
use Tempest\Support\PathHelper;
16+
use function Tempest\path;
17+
use Tempest\Support\NamespaceHelper;
1718
use function Tempest\Support\str;
1819
use Tempest\Validation\Rules\EndsWith;
1920
use Tempest\Validation\Rules\NotEmpty;
@@ -133,15 +134,15 @@ public function publishImports(): void
133134
public function getSuggestedPath(string $className, ?string $pathPrefix = null, ?string $classSuffix = null): string
134135
{
135136
// Separate input path and classname
136-
$inputClassName = PathHelper::toClassName($className);
137-
$inputPath = str(PathHelper::make($className))->replaceLast($inputClassName, '')->toString();
137+
$inputClassName = NamespaceHelper::toClassName($className);
138+
$inputPath = str(path($className))->replaceLast($inputClassName, '')->toString();
138139
$className = str($inputClassName)
139140
->pascal()
140141
->finish($classSuffix ?? '')
141142
->toString();
142143

143144
// Prepare the suggested path from the project namespace
144-
return str(PathHelper::make(
145+
return str(path(
145146
$this->composer->mainNamespace->path,
146147
$pathPrefix ?? '',
147148
$inputPath,
@@ -158,7 +159,7 @@ public function getSuggestedPath(string $className, ?string $pathPrefix = null,
158159
*/
159160
public function promptTargetPath(string $suggestedPath): string
160161
{
161-
$className = PathHelper::toClassName($suggestedPath);
162+
$className = NamespaceHelper::toClassName($suggestedPath);
162163

163164
return $this->console->ask(
164165
question: sprintf('Where do you want to save the file "%s"?', $className),

src/Tempest/Core/src/functions.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,15 @@
88
use Tempest\Core\Composer;
99
use Tempest\Core\DeferredTasks;
1010
use Tempest\Core\Kernel;
11+
use function Tempest\Support\path;
1112
use function Tempest\Support\str;
1213

13-
/**
14-
* Creates and sanitizes a file system path from the given `$parts`. The resulting path is not checked against the file system.
15-
*/
16-
function path(string ...$parts): string
17-
{
18-
$path = implode('/', $parts);
19-
20-
return str_replace(
21-
['///', '//', '\\', '\\\\'],
22-
'/',
23-
$path,
24-
);
25-
}
26-
2714
/**
2815
* Creates a path scoped within the root of the project
2916
*/
3017
function root_path(string ...$parts): string
3118
{
32-
return path(realpath(get(Kernel::class)->root), ...$parts);
19+
return path(realpath(get(Kernel::class)->root), ...$parts)->toString();
3320
}
3421

3522
/**
@@ -39,7 +26,7 @@ function src_path(string ...$parts): string
3926
{
4027
$composer = get(Composer::class);
4128

42-
return path($composer->mainNamespace->path, ...$parts);
29+
return path($composer->mainNamespace->path, ...$parts)->toString();
4330
}
4431

4532
/**

src/Tempest/Framework/Testing/InstallerTester.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function setRoot(string $root): self
4848

4949
public function path(string $path): string
5050
{
51-
return path($this->root, $path);
51+
return path($this->root, $path)->toString();
5252
}
5353

5454
public function put(string $path, string $contents): self

0 commit comments

Comments
 (0)