Skip to content

Commit

Permalink
fix(core): discovery errors being silenced (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Nov 6, 2024
1 parent e22492a commit f5b848c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/Tempest/Core/src/DiscoveryLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public function __construct(
public string $path,
) {
}

public function isVendor(): bool
{
return str_contains($this->path, '/vendor/')
|| str_contains($this->path, '\\vendor\\');
}
}
32 changes: 26 additions & 6 deletions src/Tempest/Core/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Tempest\Core\Kernel\LoadConfig;
use Tempest\Core\Kernel\LoadDiscoveryClasses;
use Tempest\Core\Kernel\LoadDiscoveryLocations;
use function Tempest\env;
use Tempest\EventBus\EventBus;
use Tempest\Http\Exceptions\HttpProductionErrorHandler;
use Whoops\Handler\PrettyPageHandler;
Expand All @@ -29,20 +30,22 @@ final class Kernel

public function __construct(
public string $root,
/** @var \Tempest\Core\DiscoveryLocation[] $discoveryLocations */
public array $discoveryLocations = [],
?Container $container = null,
) {
$this->container = $container ?? $this->createContainer();

$this
->loadEnv()
->registerKernelErrorHandler()
->registerShutdownFunction()
->registerKernel()
->loadComposer()
->loadDiscoveryLocations()
->loadConfig()
->loadDiscovery()
->loadExceptionHandler()
->registerErrorHandler()
->event(KernelEvent::BOOTED);
}

Expand Down Expand Up @@ -135,7 +138,7 @@ private function loadConfig(): self
return $this;
}

private function loadExceptionHandler(): self
private function registerErrorHandler(): self
{
$appConfig = $this->container->get(AppConfig::class);

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

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

return $this;
}

private function registerKernelErrorHandler(): self
{
$environment = Environment::tryFrom(env('ENVIRONMENT', 'production'));

if ($environment->isTesting()) {
return $this;
}

if ($environment->isProduction()) {
$handler = new HttpProductionErrorHandler();
set_exception_handler($handler->handleException(...));
set_error_handler($handler->handleError(...)); // @phpstan-ignore-line
} else {
$whoops = new Run();
$whoops->pushHandler(new PrettyPageHandler());
$whoops->register();
}

return $this;
}
}
13 changes: 10 additions & 3 deletions src/Tempest/Core/src/Kernel/LoadDiscoveryClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tempest\Core\Kernel;

use Error;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
Expand Down Expand Up @@ -92,10 +93,16 @@ public function __invoke(): void
$file->getPathname(),
);

try {
// Discovery errors (syntax errors, missing imports, etc.)
// are ignored when they happen in vendor files,
// but they are allowed to be thrown in project code
if ($discoveryLocation->isVendor()) {
try {
$input = new ClassReflector($className);
} catch (Throwable|Error) {
}
} elseif (class_exists($className)) {
$input = new ClassReflector($className);
} catch (Throwable) {
// Nothing should happen
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/Modules/Home/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(private Logger $logger)
#[Get(uri: '/')]
public function __invoke(Request $request): View
{
// ld('hi')
// throw new Exception('Home');
ll('ll');
$this->logger->debug('logger');
Expand Down
11 changes: 9 additions & 2 deletions tests/Integration/Core/LoadDiscoveryClassesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\Test;
use Tempest\Core\DiscoveryCache;
use Tempest\Core\DiscoveryLocation;
use Tempest\Core\Kernel\LoadDiscoveryClasses;
use Tempest\Database\DatabaseConfig;
use Tempest\Database\MigrationDiscovery;
Expand All @@ -27,7 +28,10 @@ public function do_not_discover(): void
];

$this->kernel->discoveryLocations = [
realpath(__DIR__ . '../../Fixtures/Discovery'),
new DiscoveryLocation(
'Tests\Tempest\Fixtures',
__DIR__ . '../../Fixtures/Discovery'
),
];

(new LoadDiscoveryClasses(
Expand All @@ -50,7 +54,10 @@ public function do_not_discover_except(): void
];

$this->kernel->discoveryLocations = [
realpath(__DIR__ . '../../Fixtures/Discovery'),
new DiscoveryLocation(
'Tests\Tempest\Fixtures',
__DIR__ . '../../Fixtures/Discovery'
),
];

(new LoadDiscoveryClasses(
Expand Down

0 comments on commit f5b848c

Please sign in to comment.