Skip to content

Commit 892da0c

Browse files
authored
feat(core): load composer dev namespaces (#1736)
1 parent 3b5f2a7 commit 892da0c

File tree

15 files changed

+52
-12
lines changed

15 files changed

+52
-12
lines changed

packages/core/src/Composer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ final class Composer
1818
/** @var array<Psr4Namespace> */
1919
public array $namespaces;
2020

21+
/** @var array<Psr4Namespace> */
22+
public array $devNamespaces;
23+
2124
public ?Psr4Namespace $mainNamespace = null;
2225

2326
private string $composerPath;
@@ -60,6 +63,13 @@ public function load(): self
6063
->unique(fn (Psr4Namespace $ns) => $ns->namespace)
6164
->toArray();
6265

66+
$this->devNamespaces = arr($this->composer)
67+
->get('autoload-dev.psr-4', default: arr())
68+
->map(fn (string $path, string $namespace) => new Psr4Namespace($namespace, $path))
69+
->sortByCallback(fn (Psr4Namespace $ns1, Psr4Namespace $ns2) => strlen($ns1->path) <=> strlen($ns2->path))
70+
->values()
71+
->toArray();
72+
6373
return $this;
6474
}
6575

packages/discovery/src/DiscoveryLocation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tempest\Discovery;
66

77
use Tempest\Support\Filesystem;
8+
use Tempest\Support\Namespace\Psr4Namespace;
89

910
final class DiscoveryLocation
1011
{
@@ -23,6 +24,11 @@ public function __construct(
2324
$this->path = Filesystem\normalize_path(rtrim($path, '\\/'));
2425
}
2526

27+
public static function fromNamespace(Psr4Namespace $namespace): self
28+
{
29+
return new self($namespace->namespace, $namespace->path);
30+
}
31+
2632
public function isVendor(): bool
2733
{
2834
return str_contains($this->path, '/vendor/') || str_contains($this->path, '\\vendor\\') || str_starts_with($this->namespace, 'Tempest');

tests/Integration/Console/Middleware/ValidateNamedArgumentsMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Integration\Console\Middleware;
3+
namespace Tests\Tempest\Integration\Console\Middleware;
44

55
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
66

tests/Integration/Core/ComposerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,28 @@ public function errors_without_composer_file(): void
140140

141141
new Composer(root: __DIR__, executor: $this->container->get(ProcessExecutor::class))->load();
142142
}
143+
144+
#[Test]
145+
public function dev_namespaces(): void
146+
{
147+
$composer = $this->initializeComposer([
148+
'autoload' => [
149+
'psr-4' => [
150+
'App\\' => 'app/',
151+
],
152+
],
153+
'autoload-dev' => [
154+
'psr-4' => [
155+
'Test\\' => 'test/',
156+
'Foo\\' => 'foo/',
157+
],
158+
],
159+
]);
160+
161+
$this->assertCount(2, $composer->devNamespaces);
162+
$this->assertSame('Test\\', $composer->devNamespaces[1]->namespace);
163+
$this->assertSame('test/', $composer->devNamespaces[1]->path);
164+
$this->assertSame('Foo\\', $composer->devNamespaces[0]->namespace);
165+
$this->assertSame('foo/', $composer->devNamespaces[0]->path);
166+
}
143167
}

tests/Integration/Core/ViewComponentsInstallerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Integration\Core;
3+
namespace Tests\Tempest\Integration\Core;
44

55
use Tempest\Support\Namespace\Psr4Namespace;
66
use Tempest\View\ViewComponent;

tests/Integration/Database/QueryStatements/CompoundStatementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Integration\Database\QueryStatements;
3+
namespace Tests\Tempest\Integration\Database\QueryStatements;
44

55
use Tempest\Database\Config\DatabaseDialect;
66
use Tempest\Database\QueryStatements\CompoundStatement;

tests/Integration/Database/RefreshModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Integration\Database;
3+
namespace Tests\Tempest\Integration\Database;
44

55
use Tempest\Database\Migrations\CreateMigrationsTable;
66
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;

tests/Integration/Framework/Commands/MetaViewComponentCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Integration\Framework\Commands;
3+
namespace Tests\Tempest\Integration\Framework\Commands;
44

55
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
66

tests/Integration/Http/ViewDataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Integration\Http;
5+
namespace Tests\Tempest\Integration\Http;
66

77
use Tempest\View\GenericView;
88
use Tests\Tempest\Fixtures\Controllers\RelativeViewController;

tests/Integration/Route/PsrRequestToGenericRequestMapperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Integration\Route;
5+
namespace Tests\Tempest\Integration\Route;
66

77
use Laminas\Diactoros\ServerRequest;
88
use Laminas\Diactoros\Stream;

0 commit comments

Comments
 (0)