Skip to content

Commit 71fa8d9

Browse files
Merge pull request #3 from MaplePHP/develop
Improve OB handling
2 parents 3d23496 + dba7532 commit 71fa8d9

18 files changed

+54
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"autoload": {
3838
"psr-4": {
39-
"MaplePHP\\Blunder\\": ""
39+
"MaplePHP\\Blunder\\": "src"
4040
}
4141
},
4242
"minimum-stability": "dev"
File renamed without changes.
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ abstract class AbstractHandler implements HandlerInterface
2828
protected const MAX_TRACE_LENGTH = 40;
2929

3030
protected bool $throwException = true;
31+
protected static bool $disableExitCode = false;
3132
protected ?HttpMessagingInterface $http = null;
3233
protected ?Closure $eventCallable = null;
3334
protected int $severity = E_ALL;
@@ -41,6 +42,17 @@ abstract class AbstractHandler implements HandlerInterface
4142
*/
4243
abstract protected function getCodeBlock(array $data, string $code, int $index = 0): string;
4344

45+
/**
46+
* You can disable exit code 1 so Blunder can be used in test cases
47+
* @param bool $disable
48+
* @return $this
49+
*/
50+
public function disableExitCode(bool $disable = true): self
51+
{
52+
self::$disableExitCode = $disable;
53+
return $this;
54+
}
55+
4456
/**
4557
* The event callable will be triggered when an error occur.
4658
* Note: Will add PSR-14 support for dispatch in the future.
@@ -130,6 +142,8 @@ public function shutdownHandler(): void
130142
);
131143
}
132144
}
145+
146+
exit((int)(!self::$disableExitCode));
133147
}
134148

135149

@@ -170,6 +184,11 @@ protected function emitter(throwable $exception, ?ExceptionItem $exceptionItem =
170184
{
171185
$this->cleanOutputBuffers();
172186

187+
if (!headers_sent()) {
188+
header_remove('location');
189+
header('HTTP/1.1 500 Internal Server Error');
190+
}
191+
173192
$response = $this->getHttp()->response()->withoutHeader('location');
174193
$response->createHeaders();
175194
$response->executeHeaders();
@@ -183,9 +202,7 @@ protected function emitter(throwable $exception, ?ExceptionItem $exceptionItem =
183202
}
184203
$stream->rewind();
185204
echo $stream->read((int)$stream->getSize());
186-
187-
// Exit execute to prevent response under to be triggered in some cases
188-
exit();
205+
exit((int)(!self::$disableExitCode));
189206
}
190207

191208
/**

0 commit comments

Comments
 (0)