Skip to content

Commit

Permalink
feat(core): add Composer util (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan-casey committed Oct 3, 2024
1 parent ca570bd commit 5d855ee
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
56 changes: 56 additions & 0 deletions src/Composer.php
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);
}
}
14 changes: 14 additions & 0 deletions src/ComposerNamespace.php
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,
) {
}
}
8 changes: 8 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __construct(
->setDiscoveryCache($discoveryCache)
->registerShutdownFunction()
->registerKernel()
->loadComposer()
->loadDiscoveryLocations()
->loadConfig()
->loadExceptionHandler()
Expand All @@ -62,6 +63,13 @@ private function createContainer(): Container
return $container;
}

private function loadComposer(): self
{
$this->container->singleton(Composer::class, new Composer($this->root));

return $this;
}

private function loadEnv(): self
{
$dotenv = Dotenv::createUnsafeImmutable($this->root);
Expand Down
11 changes: 5 additions & 6 deletions src/Kernel/LoadDiscoveryLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tempest\Core\Kernel;

use Tempest\Core\Composer;
use Tempest\Core\DiscoveryException;
use Tempest\Core\DiscoveryLocation;
use Tempest\Core\Kernel;
Expand All @@ -14,6 +15,7 @@
{
public function __construct(
private Kernel $kernel,
private Composer $composer,
) {
}

Expand Down Expand Up @@ -64,15 +66,12 @@ private function discoverCorePackages(): array
*/
private function discoverAppNamespaces(): array
{
$composer = $this->loadJsonFile(PathHelper::make($this->kernel->root, 'composer.json'));
$namespaceMap = $composer['autoload']['psr-4'] ?? [];

$discoveredLocations = [];

foreach ($namespaceMap as $namespace => $path) {
$path = PathHelper::make($this->kernel->root, $path);
foreach ($this->composer->namespaces as $namespace) {
$path = PathHelper::make($this->kernel->root, $namespace->path);

$discoveredLocations[] = new DiscoveryLocation($namespace, $path);
$discoveredLocations[] = new DiscoveryLocation($namespace->namespace, $path);
}

return $discoveredLocations;
Expand Down

0 comments on commit 5d855ee

Please sign in to comment.