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
aidan-casey committed Nov 19, 2024
1 parent 0d57080 commit aca81fc
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions 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

0 comments on commit aca81fc

Please sign in to comment.