Skip to content

Commit 9791194

Browse files
committed
wip
1 parent f74a1ab commit 9791194

File tree

7 files changed

+102
-30
lines changed

7 files changed

+102
-30
lines changed

packages/core/src/DiscoveryConfig.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,34 @@
44

55
final class DiscoveryConfig
66
{
7-
public function __construct(
8-
public array $skipDiscovery = [],
9-
) {}
7+
private array $skipDiscovery = [];
8+
9+
public function shouldSkip(string $input): bool
10+
{
11+
return $this->skipDiscovery[$input] ?? false;
12+
}
13+
14+
public function skipClasses(string ...$classNames): self
15+
{
16+
foreach ($classNames as $className) {
17+
$this->skipDiscovery[$className] = true;
18+
}
19+
20+
return $this;
21+
}
22+
23+
public function skipPaths(string ...$paths): self
24+
{
25+
foreach ($paths as $path) {
26+
$realpath = realpath($path);
27+
28+
if ($realpath === false) {
29+
continue;
30+
}
31+
32+
$this->skipDiscovery[$realpath] = true;
33+
}
34+
35+
return $this;
36+
}
1037
}

packages/core/src/Kernel/LoadDiscoveryClasses.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function buildDiscovery(string $discoveryClass): Discovery
117117

118118
$input = $file->getPathname();
119119

120-
if ($this->shouldSkipBasedOnConfig($fileName)) {
120+
if ($this->shouldSkipBasedOnConfig($input)) {
121121
continue;
122122
}
123123

@@ -177,7 +177,7 @@ private function shouldSkipBasedOnConfig(ClassReflector|string $input): bool
177177
$input = $input->getName();
178178
}
179179

180-
return in_array($input, $this->discoveryConfig->skipDiscovery, strict: true);
180+
return $this->discoveryConfig->shouldSkip($input);
181181
}
182182

183183
/**

packages/core/src/Kernel/LoadDiscoveryLocations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function __construct(
2222
public function __invoke(): void
2323
{
2424
$this->kernel->discoveryLocations = [
25-
...$this->kernel->discoveryLocations,
2625
...$this->discoverCorePackages(),
2726
...$this->discoverVendorPackages(),
2827
...$this->discoverAppNamespaces(),
28+
...$this->kernel->discoveryLocations,
2929
];
3030
}
3131

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Tempest\Core\DiscoveryConfig;
4+
use Tests\Tempest\Fixtures\GlobalHiddenDiscovery;
5+
6+
return new DiscoveryConfig()
7+
->skipClasses(GlobalHiddenDiscovery::class)
8+
->skipPaths(__DIR__ . '/../../Fixtures/GlobalHiddenPathDiscovery.php');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Fixtures;
4+
5+
use Tempest\Discovery\Discovery;
6+
use Tempest\Discovery\DiscoveryLocation;
7+
use Tempest\Discovery\IsDiscovery;
8+
use Tempest\Reflection\ClassReflector;
9+
10+
final class GlobalHiddenDiscovery implements Discovery
11+
{
12+
public static bool $discovered = false;
13+
14+
use IsDiscovery;
15+
16+
public function discover(DiscoveryLocation $location, ClassReflector $class): void
17+
{
18+
}
19+
20+
public function apply(): void
21+
{
22+
self::$discovered = true;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Fixtures;
4+
5+
use Tempest\Discovery\Discovery;
6+
use Tempest\Discovery\DiscoveryLocation;
7+
use Tempest\Discovery\IsDiscovery;
8+
use Tempest\Reflection\ClassReflector;
9+
10+
final class GlobalHiddenPathDiscovery implements Discovery
11+
{
12+
public static bool $discovered = false;
13+
14+
use IsDiscovery;
15+
16+
public function discover(DiscoveryLocation $location, ClassReflector $class): void
17+
{
18+
}
19+
20+
public function apply(): void
21+
{
22+
self::$discovered = true;
23+
}
24+
}

tests/Integration/Core/LoadDiscoveryClassesTest.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Tests\Tempest\Integration\Core;
66

7+
use Tests\Tempest\Fixtures\GlobalHiddenPathDiscovery;
78
use PHPUnit\Framework\Attributes\Test;
89
use Tempest\Database\DatabaseMigration;
9-
use Tempest\Database\MigrationDiscovery;
1010
use Tempest\Database\Migrations\RunnableMigrations;
11-
use Tempest\Discovery\DiscoveryLocation;
1211
use Tests\Tempest\Fixtures\Discovery\HiddenDatabaseMigration;
1312
use Tests\Tempest\Fixtures\Discovery\HiddenMigratableDatabaseMigration;
13+
use Tests\Tempest\Fixtures\GlobalHiddenDiscovery;
1414
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1515

1616
use function Tempest\get;
@@ -23,37 +23,26 @@ final class LoadDiscoveryClassesTest extends FrameworkIntegrationTestCase
2323
#[Test]
2424
public function do_not_discover(): void
2525
{
26-
$this->kernel->discoveryClasses = [
27-
MigrationDiscovery::class,
28-
];
29-
30-
$this->kernel->discoveryLocations = [
31-
new DiscoveryLocation(
32-
'Tests\Tempest\Fixtures',
33-
__DIR__ . '../../Fixtures/Discovery',
34-
),
35-
];
36-
3726
$migrations = get(RunnableMigrations::class);
3827

3928
$this->assertNotContains(HiddenDatabaseMigration::class, $migrations);
4029
}
4130

4231
#[Test]
43-
public function do_not_discover_except(): void
32+
public function do_not_discover_global_class(): void
4433
{
45-
$this->kernel->discoveryClasses = [
46-
MigrationDiscovery::class,
47-
// TODO: update tests to add `PublishDiscovery` when it's merged
48-
];
34+
$this->assertFalse(GlobalHiddenDiscovery::$discovered);
35+
}
4936

50-
$this->kernel->discoveryLocations = [
51-
new DiscoveryLocation(
52-
'Tests\Tempest\Fixtures',
53-
__DIR__ . '../../Fixtures/Discovery',
54-
),
55-
];
37+
#[Test]
38+
public function do_not_discover_global_path(): void
39+
{
40+
$this->assertFalse(GlobalHiddenPathDiscovery::$discovered);
41+
}
5642

43+
#[Test]
44+
public function do_not_discover_except(): void
45+
{
5746
$migrations = get(RunnableMigrations::class);
5847

5948
$foundMigrations = array_filter(

0 commit comments

Comments
 (0)