-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add
Composer
util (#519)
- Loading branch information
1 parent
ca570bd
commit 5d855ee
Showing
4 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Core; | ||
|
||
use function Tempest\Support\arr; | ||
use Tempest\Support\PathHelper; | ||
|
||
final readonly class Composer | ||
{ | ||
/** @var array<ComposerNamespace> */ | ||
public array $namespaces; | ||
|
||
public ?ComposerNamespace $mainNamespace; | ||
|
||
public array $composer; | ||
|
||
public function __construct( | ||
string $root, | ||
) { | ||
$composerFilePath = PathHelper::make($root, 'composer.json'); | ||
|
||
$this->composer = $this->loadComposerFile($composerFilePath); | ||
$this->namespaces = arr($this->composer) | ||
->get('autoload.psr-4', default: arr()) | ||
->map(fn (string $path, string $namespace) => new ComposerNamespace($namespace, $path)) | ||
->values() | ||
->toArray(); | ||
|
||
foreach ($this->namespaces as $namespace) { | ||
if (str_starts_with($namespace->path, 'app/') || str_starts_with($namespace->path, 'src/')) { | ||
$this->mainNamespace = $namespace; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (! isset($this->mainNamespace) && count($this->namespaces)) { | ||
$this->mainNamespace = $this->namespaces[0]; | ||
} | ||
|
||
if (! isset($this->mainNamespace)) { | ||
throw new KernelException("Tempest requires at least one PSR-4 namespace to be defined in composer.json."); | ||
} | ||
} | ||
|
||
private function loadComposerFile(string $path): array | ||
{ | ||
if (! file_exists($path)) { | ||
throw new KernelException("Could not locate composer.json."); | ||
} | ||
|
||
return json_decode(file_get_contents($path), associative: true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Core; | ||
|
||
final readonly class ComposerNamespace | ||
{ | ||
public function __construct( | ||
public string $namespace, | ||
public string $path, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters