Skip to content

Commit 3f9bdde

Browse files
authored
feat(core): install main namespace (#751)
1 parent 677cf3b commit 3f9bdde

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/Tempest/Core/src/Composer.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ final class Composer
1212
/** @var array<ComposerNamespace> */
1313
public array $namespaces;
1414

15-
public ?ComposerNamespace $mainNamespace;
15+
public ?ComposerNamespace $mainNamespace = null;
1616

17-
public array $composer;
17+
private string $composerPath;
18+
19+
private array $composer;
1820

1921
public function __construct(
20-
string $root,
22+
private string $root,
2123
) {
22-
$composerFilePath = path($root, 'composer.json')->toString();
23-
24-
$this->composer = $this->loadComposerFile($composerFilePath);
24+
$this->composerPath = path($this->root, 'composer.json')->toString();
25+
$this->composer = $this->loadComposerFile($this->composerPath);
2526
$this->namespaces = arr($this->composer)
2627
->get('autoload.psr-4', default: arr())
2728
->map(fn (string $path, string $namespace) => new ComposerNamespace($namespace, $path))
@@ -48,6 +49,22 @@ public function setMainNamespace(ComposerNamespace $namespace): self
4849
return $this;
4950
}
5051

52+
public function addNamespace(string $namespace, string $path): self
53+
{
54+
$path = str_replace($this->root, '.', $path);
55+
56+
$this->composer['autoload']['psr-4'][$namespace] = $path;
57+
58+
return $this;
59+
}
60+
61+
public function save(): self
62+
{
63+
file_put_contents($this->composerPath, json_encode($this->composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
64+
65+
return $this;
66+
}
67+
5168
private function loadComposerFile(string $path): array
5269
{
5370
if (! file_exists($path)) {

src/Tempest/Debug/src/Debug.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ private function writeToLog(array $items, string $callPath): void
5858
return;
5959
}
6060

61+
$directory = dirname($this->logConfig->debugLogPath);
62+
63+
if (! is_dir($directory)) {
64+
mkdir(directory: $directory, recursive: true);
65+
}
66+
6167
$handle = @fopen($this->logConfig->debugLogPath, 'a');
6268

6369
if (! $handle) {

src/Tempest/Framework/Installers/FrameworkInstaller.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function getName(): string
1919

2020
public function install(): void
2121
{
22+
$this->installMainNamespace();
23+
2224
$this->publish(
2325
source: __DIR__ . '/../../../../.env.example',
2426
destination: root_path('.env.example'),
@@ -45,4 +47,30 @@ public function install(): void
4547
},
4648
);
4749
}
50+
51+
private function installMainNamespace(): void
52+
{
53+
if ($this->composer->mainNamespace !== null) {
54+
return;
55+
}
56+
57+
if (! $this->confirm('Tempest detected no main project namespace. Do you want to create it?', default: true)) {
58+
return;
59+
}
60+
61+
$appPath = root_path('app/');
62+
63+
if (! is_dir($appPath)) {
64+
mkdir($appPath);
65+
}
66+
67+
$this->composer
68+
->addNamespace(
69+
'App\\',
70+
$appPath,
71+
)
72+
->save();
73+
74+
$this->success("Project namespace created: {$appPath}");
75+
}
4876
}

0 commit comments

Comments
 (0)