1212use Tempest \Core \Kernel \LoadConfig ;
1313use Tempest \Core \Kernel \LoadDiscoveryClasses ;
1414use Tempest \Core \Kernel \LoadDiscoveryLocations ;
15+ use function Tempest \env ;
1516use Tempest \EventBus \EventBus ;
1617use Tempest \Http \Exceptions \HttpProductionErrorHandler ;
1718use 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}
0 commit comments