Skip to content

Commit f5b848c

Browse files
authored
fix(core): discovery errors being silenced (#688)
1 parent e22492a commit f5b848c

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

src/Tempest/Core/src/DiscoveryLocation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ public function __construct(
1111
public string $path,
1212
) {
1313
}
14+
15+
public function isVendor(): bool
16+
{
17+
return str_contains($this->path, '/vendor/')
18+
|| str_contains($this->path, '\\vendor\\');
19+
}
1420
}

src/Tempest/Core/src/Kernel.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Tempest\Core\Kernel\LoadConfig;
1313
use Tempest\Core\Kernel\LoadDiscoveryClasses;
1414
use Tempest\Core\Kernel\LoadDiscoveryLocations;
15+
use function Tempest\env;
1516
use Tempest\EventBus\EventBus;
1617
use Tempest\Http\Exceptions\HttpProductionErrorHandler;
1718
use Whoops\Handler\PrettyPageHandler;
@@ -29,20 +30,22 @@ final class Kernel
2930

3031
public function __construct(
3132
public string $root,
33+
/** @var \Tempest\Core\DiscoveryLocation[] $discoveryLocations */
3234
public array $discoveryLocations = [],
3335
?Container $container = null,
3436
) {
3537
$this->container = $container ?? $this->createContainer();
3638

3739
$this
3840
->loadEnv()
41+
->registerKernelErrorHandler()
3942
->registerShutdownFunction()
4043
->registerKernel()
4144
->loadComposer()
4245
->loadDiscoveryLocations()
4346
->loadConfig()
4447
->loadDiscovery()
45-
->loadExceptionHandler()
48+
->registerErrorHandler()
4649
->event(KernelEvent::BOOTED);
4750
}
4851

@@ -135,7 +138,7 @@ private function loadConfig(): self
135138
return $this;
136139
}
137140

138-
private function loadExceptionHandler(): self
141+
private function registerErrorHandler(): self
139142
{
140143
$appConfig = $this->container->get(AppConfig::class);
141144

@@ -151,10 +154,6 @@ private function loadExceptionHandler(): self
151154
$handler = $this->container->get(HttpProductionErrorHandler::class);
152155
set_exception_handler($handler->handleException(...));
153156
set_error_handler($handler->handleError(...)); // @phpstan-ignore-line
154-
} else {
155-
$whoops = new Run();
156-
$whoops->pushHandler(new PrettyPageHandler());
157-
$whoops->register();
158157
}
159158

160159
return $this;
@@ -175,4 +174,25 @@ private function event(object $event): self
175174

176175
return $this;
177176
}
177+
178+
private function registerKernelErrorHandler(): self
179+
{
180+
$environment = Environment::tryFrom(env('ENVIRONMENT', 'production'));
181+
182+
if ($environment->isTesting()) {
183+
return $this;
184+
}
185+
186+
if ($environment->isProduction()) {
187+
$handler = new HttpProductionErrorHandler();
188+
set_exception_handler($handler->handleException(...));
189+
set_error_handler($handler->handleError(...)); // @phpstan-ignore-line
190+
} else {
191+
$whoops = new Run();
192+
$whoops->pushHandler(new PrettyPageHandler());
193+
$whoops->register();
194+
}
195+
196+
return $this;
197+
}
178198
}

src/Tempest/Core/src/Kernel/LoadDiscoveryClasses.php

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

55
namespace Tempest\Core\Kernel;
66

7+
use Error;
78
use FilesystemIterator;
89
use RecursiveDirectoryIterator;
910
use RecursiveIteratorIterator;
@@ -92,10 +93,16 @@ public function __invoke(): void
9293
$file->getPathname(),
9394
);
9495

95-
try {
96+
// Discovery errors (syntax errors, missing imports, etc.)
97+
// are ignored when they happen in vendor files,
98+
// but they are allowed to be thrown in project code
99+
if ($discoveryLocation->isVendor()) {
100+
try {
101+
$input = new ClassReflector($className);
102+
} catch (Throwable|Error) {
103+
}
104+
} elseif (class_exists($className)) {
96105
$input = new ClassReflector($className);
97-
} catch (Throwable) {
98-
// Nothing should happen
99106
}
100107
}
101108

tests/Fixtures/Modules/Home/HomeController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct(private Logger $logger)
1818
#[Get(uri: '/')]
1919
public function __invoke(Request $request): View
2020
{
21+
// ld('hi')
2122
// throw new Exception('Home');
2223
ll('ll');
2324
$this->logger->debug('logger');

tests/Integration/Core/LoadDiscoveryClassesTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\Test;
88
use Tempest\Core\DiscoveryCache;
9+
use Tempest\Core\DiscoveryLocation;
910
use Tempest\Core\Kernel\LoadDiscoveryClasses;
1011
use Tempest\Database\DatabaseConfig;
1112
use Tempest\Database\MigrationDiscovery;
@@ -27,7 +28,10 @@ public function do_not_discover(): void
2728
];
2829

2930
$this->kernel->discoveryLocations = [
30-
realpath(__DIR__ . '../../Fixtures/Discovery'),
31+
new DiscoveryLocation(
32+
'Tests\Tempest\Fixtures',
33+
__DIR__ . '../../Fixtures/Discovery'
34+
),
3135
];
3236

3337
(new LoadDiscoveryClasses(
@@ -50,7 +54,10 @@ public function do_not_discover_except(): void
5054
];
5155

5256
$this->kernel->discoveryLocations = [
53-
realpath(__DIR__ . '../../Fixtures/Discovery'),
57+
new DiscoveryLocation(
58+
'Tests\Tempest\Fixtures',
59+
__DIR__ . '../../Fixtures/Discovery'
60+
),
5461
];
5562

5663
(new LoadDiscoveryClasses(

0 commit comments

Comments
 (0)