From 3f9bddeb9dbc0d033bf9b6386e329c5686306ba3 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Tue, 19 Nov 2024 12:57:06 +0100 Subject: [PATCH] feat(core): install main namespace (#751) --- src/Tempest/Core/src/Composer.php | 29 +++++++++++++++---- src/Tempest/Debug/src/Debug.php | 6 ++++ .../Installers/FrameworkInstaller.php | 28 ++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/Tempest/Core/src/Composer.php b/src/Tempest/Core/src/Composer.php index b796757e6..f1ef949f2 100644 --- a/src/Tempest/Core/src/Composer.php +++ b/src/Tempest/Core/src/Composer.php @@ -12,16 +12,17 @@ final class Composer /** @var array */ public array $namespaces; - public ?ComposerNamespace $mainNamespace; + public ?ComposerNamespace $mainNamespace = null; - public array $composer; + private string $composerPath; + + private array $composer; public function __construct( - string $root, + private string $root, ) { - $composerFilePath = path($root, 'composer.json')->toString(); - - $this->composer = $this->loadComposerFile($composerFilePath); + $this->composerPath = path($this->root, 'composer.json')->toString(); + $this->composer = $this->loadComposerFile($this->composerPath); $this->namespaces = arr($this->composer) ->get('autoload.psr-4', default: arr()) ->map(fn (string $path, string $namespace) => new ComposerNamespace($namespace, $path)) @@ -48,6 +49,22 @@ public function setMainNamespace(ComposerNamespace $namespace): self return $this; } + public function addNamespace(string $namespace, string $path): self + { + $path = str_replace($this->root, '.', $path); + + $this->composer['autoload']['psr-4'][$namespace] = $path; + + return $this; + } + + public function save(): self + { + file_put_contents($this->composerPath, json_encode($this->composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + + return $this; + } + private function loadComposerFile(string $path): array { if (! file_exists($path)) { diff --git a/src/Tempest/Debug/src/Debug.php b/src/Tempest/Debug/src/Debug.php index 1ee39eea4..0ed1825d7 100644 --- a/src/Tempest/Debug/src/Debug.php +++ b/src/Tempest/Debug/src/Debug.php @@ -58,6 +58,12 @@ private function writeToLog(array $items, string $callPath): void return; } + $directory = dirname($this->logConfig->debugLogPath); + + if (! is_dir($directory)) { + mkdir(directory: $directory, recursive: true); + } + $handle = @fopen($this->logConfig->debugLogPath, 'a'); if (! $handle) { diff --git a/src/Tempest/Framework/Installers/FrameworkInstaller.php b/src/Tempest/Framework/Installers/FrameworkInstaller.php index a789ac77d..17c11f35d 100644 --- a/src/Tempest/Framework/Installers/FrameworkInstaller.php +++ b/src/Tempest/Framework/Installers/FrameworkInstaller.php @@ -19,6 +19,8 @@ public function getName(): string public function install(): void { + $this->installMainNamespace(); + $this->publish( source: __DIR__ . '/../../../../.env.example', destination: root_path('.env.example'), @@ -45,4 +47,30 @@ public function install(): void }, ); } + + private function installMainNamespace(): void + { + if ($this->composer->mainNamespace !== null) { + return; + } + + if (! $this->confirm('Tempest detected no main project namespace. Do you want to create it?', default: true)) { + return; + } + + $appPath = root_path('app/'); + + if (! is_dir($appPath)) { + mkdir($appPath); + } + + $this->composer + ->addNamespace( + 'App\\', + $appPath, + ) + ->save(); + + $this->success("Project namespace created: {$appPath}"); + } }