Skip to content

Commit

Permalink
feat(core): install main namespace (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Nov 19, 2024
1 parent 677cf3b commit 3f9bdde
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/Tempest/Core/src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ final class Composer
/** @var array<ComposerNamespace> */
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))
Expand All @@ -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)) {
Expand Down
6 changes: 6 additions & 0 deletions src/Tempest/Debug/src/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions src/Tempest/Framework/Installers/FrameworkInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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}");
}
}

0 comments on commit 3f9bdde

Please # to comment.