Skip to content

Commit

Permalink
refactor(support): convert PathHelper to immutable class (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Nov 18, 2024
1 parent 3621006 commit 0dacd10
Show file tree
Hide file tree
Showing 31 changed files with 298 additions and 211 deletions.
2 changes: 1 addition & 1 deletion src/Tempest/Cache/src/ProjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
private readonly CacheConfig $cacheConfig,
) {
$this->pool = $this->cacheConfig->projectCachePool ?? new FilesystemAdapter(
directory: path($this->cacheConfig->directory, 'project'),
directory: path($this->cacheConfig->directory, 'project')->toString(),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Tempest/Console/src/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Tempest\Core\Tempest;
use Tempest\Log\Channels\AppendLogChannel;
use Tempest\Log\LogConfig;
use Tempest\Support\PathHelper;
use function Tempest\path;
use Throwable;

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

return $application;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;
use Tempest\Core\Kernel;
use Tempest\Support\PathHelper;
use function Tempest\path;

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

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

return new LogOutputBuffer($path);
}
Expand Down
1 change: 1 addition & 0 deletions src/Tempest/Core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"require": {
"php": "^8.3",
"tempest/container": "dev-main",
"tempest/support": "dev-main",
"vlucas/phpdotenv": "^5.6",
"filp/whoops": "^2.15"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Tempest/Core/src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Tempest\Core;

use function Tempest\path;
use function Tempest\Support\arr;
use Tempest\Support\PathHelper;

final class Composer
{
Expand All @@ -19,7 +19,7 @@ final class Composer
public function __construct(
string $root,
) {
$composerFilePath = PathHelper::make($root, 'composer.json');
$composerFilePath = path($root, 'composer.json')->toString();

$this->composer = $this->loadComposerFile($composerFilePath);
$this->namespaces = arr($this->composer)
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Core/src/DiscoveryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
?CacheItemPoolInterface $pool = null,
) {
$this->pool = $pool ?? new FilesystemAdapter(
directory: path($this->cacheConfig->directory, 'discovery'),
directory: path($this->cacheConfig->directory, 'discovery')->toString(),
);
}

Expand Down
26 changes: 13 additions & 13 deletions src/Tempest/Core/src/Kernel/LoadDiscoveryLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Tempest\Core\DiscoveryException;
use Tempest\Core\DiscoveryLocation;
use Tempest\Core\Kernel;
use Tempest\Support\PathHelper;
use function Tempest\path;

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

$discoveredLocations = [];
Expand All @@ -49,12 +49,12 @@ private function discoverCorePackages(): array
continue;
}

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

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

$discoveredLocations[] = new DiscoveryLocation($namespace, $namespacePath);
$discoveredLocations[] = new DiscoveryLocation($namespace, $namespacePath->toString());
}
}

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

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

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

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

$discoveredLocations = [];
Expand All @@ -96,7 +96,7 @@ private function discoverVendorPackages(): array
continue;
}

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

Expand All @@ -105,9 +105,9 @@ private function discoverVendorPackages(): array
}

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

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

Expand Down
11 changes: 6 additions & 5 deletions src/Tempest/Core/src/PublishesFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
use Tempest\Generation\Exceptions\FileGenerationFailedException;
use Tempest\Generation\StubFileGenerator;
use Tempest\Support\PathHelper;
use function Tempest\path;
use Tempest\Support\NamespaceHelper;
use function Tempest\Support\str;
use Tempest\Validation\Rules\EndsWith;
use Tempest\Validation\Rules\NotEmpty;
Expand Down Expand Up @@ -133,15 +134,15 @@ public function publishImports(): void
public function getSuggestedPath(string $className, ?string $pathPrefix = null, ?string $classSuffix = null): string
{
// Separate input path and classname
$inputClassName = PathHelper::toClassName($className);
$inputPath = str(PathHelper::make($className))->replaceLast($inputClassName, '')->toString();
$inputClassName = NamespaceHelper::toClassName($className);
$inputPath = str(path($className))->replaceLast($inputClassName, '')->toString();
$className = str($inputClassName)
->pascal()
->finish($classSuffix ?? '')
->toString();

// Prepare the suggested path from the project namespace
return str(PathHelper::make(
return str(path(
$this->composer->mainNamespace->path,
$pathPrefix ?? '',
$inputPath,
Expand All @@ -158,7 +159,7 @@ public function getSuggestedPath(string $className, ?string $pathPrefix = null,
*/
public function promptTargetPath(string $suggestedPath): string
{
$className = PathHelper::toClassName($suggestedPath);
$className = NamespaceHelper::toClassName($suggestedPath);

return $this->console->ask(
question: sprintf('Where do you want to save the file "%s"?', $className),
Expand Down
19 changes: 3 additions & 16 deletions src/Tempest/Core/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,15 @@
use Tempest\Core\Composer;
use Tempest\Core\DeferredTasks;
use Tempest\Core\Kernel;
use function Tempest\Support\path;
use function Tempest\Support\str;

/**
* Creates and sanitizes a file system path from the given `$parts`. The resulting path is not checked against the file system.
*/
function path(string ...$parts): string
{
$path = implode('/', $parts);

return str_replace(
['///', '//', '\\', '\\\\'],
'/',
$path,
);
}

/**
* Creates a path scoped within the root of the project
*/
function root_path(string ...$parts): string
{
return path(realpath(get(Kernel::class)->root), ...$parts);
return path(realpath(get(Kernel::class)->root), ...$parts)->toString();
}

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

return path($composer->mainNamespace->path, ...$parts);
return path($composer->mainNamespace->path, ...$parts)->toString();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Framework/Testing/InstallerTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setRoot(string $root): self

public function path(string $path): string
{
return path($this->root, $path);
return path($this->root, $path)->toString();
}

public function put(string $path, string $contents): self
Expand Down
11 changes: 6 additions & 5 deletions src/Tempest/Generation/src/StubFileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use Tempest\Generation\Enums\StubFileType;
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
use Tempest\Generation\Exceptions\FileGenerationFailedException;
use Tempest\Support\PathHelper;
use function Tempest\path;
use Tempest\Support\NamespaceHelper;
use function Tempest\Support\str;
use Tempest\Support\StringHelper;
use Throwable;
Expand Down Expand Up @@ -51,8 +52,8 @@ public function generateClassFile(
$this->prepareFilesystem($targetPath);

// Transform stub to class
$namespace = PathHelper::toMainNamespace($targetPath);
$classname = PathHelper::toClassName($targetPath);
$namespace = NamespaceHelper::toMainNamespace($targetPath);
$classname = NamespaceHelper::toClassName($targetPath);
$classManipulator = (new ClassManipulator($stubFile->filePath))
->setNamespace($namespace)
->setClassName($classname);
Expand All @@ -68,8 +69,8 @@ public function generateClassFile(
// Run all manipulations
$classManipulator = array_reduce(
array: $manipulations,
initial: $classManipulator,
callback: fn (ClassManipulator $manipulator, Closure $manipulation) => $manipulation($manipulator)
callback: fn (ClassManipulator $manipulator, Closure $manipulation) => $manipulation($manipulator),
initial: $classManipulator
);

if (file_exists($targetPath) && $shouldOverride) {
Expand Down
6 changes: 3 additions & 3 deletions src/Tempest/Http/src/HttpApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Tempest\Http\Session\Session;
use Tempest\Log\Channels\AppendLogChannel;
use Tempest\Log\LogConfig;
use Tempest\Support\PathHelper;
use function Tempest\path;
use Throwable;

#[Singleton]
Expand All @@ -41,9 +41,9 @@ public static function boot(
&& $logConfig->serverLogPath === null
&& $logConfig->channels === []
) {
$logConfig->debugLogPath = PathHelper::make($container->get(Kernel::class)->root, '/log/debug.log');
$logConfig->debugLogPath = path($container->get(Kernel::class)->root, '/log/debug.log')->toString();
$logConfig->serverLogPath = env('SERVER_LOG');
$logConfig->channels[] = new AppendLogChannel(PathHelper::make($root, '/log/tempest.log'));
$logConfig->channels[] = new AppendLogChannel(path($root, '/log/tempest.log')->toString());
}

return $application;
Expand Down
4 changes: 2 additions & 2 deletions src/Tempest/Http/src/Session/Managers/FileSessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function isValid(SessionId $id): bool

private function getPath(SessionId $id): string
{
return path($this->sessionConfig->path, (string)$id);
return path($this->sessionConfig->path, (string)$id)->toString();
}

private function resolve(SessionId $id): ?Session
Expand Down Expand Up @@ -129,7 +129,7 @@ private function persist(SessionId $id, ?array $data = null): Session

public function cleanup(): void
{
$sessionFiles = glob(path($this->sessionConfig->path, '/*'));
$sessionFiles = glob(path($this->sessionConfig->path, '/*')->toString());

foreach ($sessionFiles as $sessionFile) {
$id = new SessionId(pathinfo($sessionFile, PATHINFO_FILENAME));
Expand Down
4 changes: 2 additions & 2 deletions src/Tempest/Http/src/Static/StaticGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ public function __invoke(): void
continue;
}

$directory = pathinfo($file, PATHINFO_DIRNAME);
$directory = $file->dirname();

if (! is_dir($directory)) {
mkdir($directory, recursive: true);
}

file_put_contents($file, $content);
file_put_contents($file->path(), $content);

$this->writeln("- <em>{$uri}</em> > <u>{$file}</u>");
} catch (Throwable $e) {
Expand Down
Loading

0 comments on commit 0dacd10

Please # to comment.