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.

Handlers/AbstractHandler.php renamed to src/Handlers/AbstractHandler.php

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
/**
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Interfaces/HandlerInterface.php renamed to src/Interfaces/HandlerInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ public function shutdownHandler(): void;
5858
*/
5959
public function event(Closure $event): void;
6060

61+
/**
62+
* Set expected severity mask
63+
* @param int $severity
64+
* @return self
65+
*/
66+
public function setSeverity(int $severity): self;
6167
}

Run.php renamed to src/Run.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Run
1717
{
1818
private HandlerInterface $handler;
1919
private ?SeverityLevelPool $severity = null;
20+
private bool $removeLocationHeader = false;
2021

2122
public function __construct(HandlerInterface $handler, ?HttpMessagingInterface $http = null)
2223
{
@@ -26,6 +27,28 @@ public function __construct(HandlerInterface $handler, ?HttpMessagingInterface $
2627
}
2728
}
2829

30+
/**
31+
* You can disable exit code 1 so Blunder can be used in test cases
32+
* @param bool $disable
33+
* @return $this
34+
*/
35+
public function disableExitCode(bool $disable = true): self
36+
{
37+
$this->handler->disableExitCode($disable);
38+
return $this;
39+
}
40+
41+
/**
42+
* Enable or disable the removal of the 'Location' header to prevent redirections.
43+
* @param bool $removeRedirect
44+
* @return $this
45+
*/
46+
public function removeLocationHeader(bool $removeRedirect): self
47+
{
48+
$this->removeLocationHeader = $removeRedirect;
49+
return $this;
50+
}
51+
2952
/**
3053
* Access severity instance to set or exclude severity levels from error handler
3154
* @return SeverityLevelPool
@@ -56,14 +79,15 @@ public function event(Closure $event): void
5679
*/
5780
public function load(): void
5881
{
59-
if (!headers_sent()) {
82+
if ($this->removeLocationHeader && !headers_sent()) {
6083
header_remove('location');
6184
}
62-
85+
ob_start();
6386
$this->handler->setSeverity($this->severity()->getSeverityLevelMask());
6487
set_error_handler([$this->handler, "errorHandler"], $this->severity()->getSeverityLevelMask());
6588
set_exception_handler([$this->handler, "exceptionHandler"]);
6689
register_shutdown_function([$this->handler, "shutdownHandler"]);
90+
ob_clean();
6791
}
6892

6993
}
File renamed without changes.

main.css renamed to src/main.css

File renamed without changes.

main.js renamed to src/main.js

File renamed without changes.

tests/unitary-blunder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
// SilentHandler will hide the error that I have added in this file
2020
// and is using to test the Blunder library
2121
$run = new Run(new SilentHandler());
22+
$run->disableExitCode();
2223
$run->event(function ($item, $http) use ($inst) {
23-
24-
2524
$inst->add($item->getStatus(), function () {
2625
return $this->equal("warning");
2726

0 commit comments

Comments
 (0)