Skip to content

Commit 30411b6

Browse files
committed
Structure improvements
1 parent 7755ea9 commit 30411b6

8 files changed

+155
-14
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Closure;
2222
use Throwable;
2323

24-
abstract class AbstractAbstractHandler implements AbstractHandlerInterface
24+
abstract class AbstractHandler implements AbstractHandlerInterface
2525
{
2626
/**
2727
* Maximum trace depth (memory improvement)
@@ -33,6 +33,7 @@ abstract class AbstractAbstractHandler implements AbstractHandlerInterface
3333
protected static bool $enabledTraceLines = false;
3434

3535
protected bool $throwException = true;
36+
protected ?Throwable $exception = null;
3637
protected ?HttpMessagingInterface $http = null;
3738
protected ?Closure $eventCallable = null;
3839
protected ?SeverityLevelPool $severityLevelPool = null;
@@ -52,10 +53,10 @@ abstract protected function getCodeBlock(array $data, string $code, int $index =
5253
* If you want Blunder to trigger a specific exit code on error,
5354
* specify the code using this method.
5455
*
55-
* @param int $code The exit code to use.
56+
* @param ?int $code The exit code to use.
5657
* @return $this
5758
*/
58-
public function setExitCode(int $code): self
59+
public function setExitCode(?int $code): self
5960
{
6061
self::$exitCode = $code;
6162
return $this;
@@ -126,7 +127,7 @@ final public function setSeverity(SeverityLevelPool $severity): self
126127
* @param int $errLine
127128
* @param array $context
128129
* @return bool
129-
* @throws ErrorException
130+
* @throws Throwable
130131
*/
131132
public function errorHandler(int $errNo, string $errStr, string $errFile, int $errLine = 0, array $context = []): bool
132133
{
@@ -137,18 +138,27 @@ public function errorHandler(int $errNo, string $errStr, string $errFile, int $e
137138
return $redirectHandler;
138139
}
139140
$this->cleanOutputBuffers();
140-
$exception = new BlunderErrorException($errStr, 0, $errNo, $errFile, $errLine);
141+
$this->exception = new BlunderErrorException($errStr, 0, $errNo, $errFile, $errLine);
141142
if ($this->throwException) {
142-
$exception->setPrettyMessage($this->getErrorMessage($exception));
143-
throw $exception;
143+
$this->exception->setPrettyMessage($this->getErrorMessage($this->exception));
144+
throw $this->exception;
144145
} else {
145-
$this->exceptionHandler($exception);
146+
$this->exceptionHandler($this->exception);
146147
}
147148
return true;
148149
}
149150
return false;
150151
}
151152

153+
/**
154+
* Get exception if has been initiated
155+
* @return Throwable|null
156+
*/
157+
public function getException(): ?Throwable
158+
{
159+
return $this->exception;
160+
}
161+
152162
/**
153163
* Handle the errorHandler or redirect to PHP error to a new handler or
154164
*
@@ -241,7 +251,7 @@ protected function getTrace(throwable $exception): array
241251
*/
242252
protected function emitter(throwable $exception, ?ExceptionItem $exceptionItem = null): void
243253
{
244-
$this->cleanOutputBuffers();
254+
//$this->cleanOutputBuffers();
245255

246256
if (!headers_sent()) {
247257
header_remove('location');

src/Handlers/HtmlHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use MaplePHP\Blunder\Interfaces\HandlerInterface;
1414
use Throwable;
1515

16-
class HtmlHandler extends AbstractAbstractHandler implements HandlerInterface
16+
class HtmlHandler extends AbstractHandler implements HandlerInterface
1717
{
1818
/** @var string */
1919
public const CSS_FILE = 'main.css';
@@ -22,6 +22,11 @@ class HtmlHandler extends AbstractAbstractHandler implements HandlerInterface
2222

2323
protected static bool $enabledTraceLines = true;
2424

25+
public function testssss($www1)
26+
{
27+
return "Lorem1";
28+
}
29+
2530
/**
2631
* Exception handler output
2732
* @param Throwable $exception

src/Handlers/JsonHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use MaplePHP\Blunder\Interfaces\HandlerInterface;
1414
use Throwable;
1515

16-
class JsonHandler extends AbstractAbstractHandler implements HandlerInterface
16+
class JsonHandler extends AbstractHandler implements HandlerInterface
1717
{
1818
protected static bool $enabledTraceLines = true;
1919

src/Handlers/TextHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use MaplePHP\Blunder\SeverityLevelPool;
1414
use Throwable;
1515

16-
class TextHandler extends AbstractAbstractHandler implements HandlerInterface
16+
class TextHandler extends AbstractHandler implements HandlerInterface
1717
{
1818
protected static bool $enabledTraceLines = true;
1919

src/Handlers/XmlHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use SimpleXMLElement;
1515
use Throwable;
1616

17-
class XmlHandler extends AbstractAbstractHandler implements HandlerInterface
17+
class XmlHandler extends AbstractHandler implements HandlerInterface
1818
{
1919
protected static bool $enabledTraceLines = true;
2020

tests/unitary-blunder.php renamed to tests/unitary-blunder-event.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
$unit = new Unit();
1616

17-
$unit->case("MaplePHP pretty error handler test", function ($inst) {
17+
$unit->case("MaplePHP Blunder event test", function ($inst) {
1818

1919
// SilentHandler will hide the error that I have added in this file
2020
// and is using to test the Blunder library

tests/unitary-blunder-handlers.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* This is how a template test file should look like but
5+
* when used in MaplePHP framework you can skip the "bash code" at top and the "autoload file"!
6+
*/
7+
8+
use MaplePHP\Blunder\ExceptionItem;
9+
use MaplePHP\Blunder\Handlers\CliHandler;
10+
use MaplePHP\Blunder\Handlers\HtmlHandler;
11+
use MaplePHP\Blunder\Handlers\JsonHandler;
12+
use MaplePHP\Blunder\Handlers\PlainTextHandler;
13+
use MaplePHP\Blunder\Handlers\SilentHandler;
14+
use MaplePHP\Blunder\Handlers\TextHandler;
15+
use MaplePHP\Blunder\Handlers\XmlHandler;
16+
use MaplePHP\Blunder\Interfaces\HandlerInterface;
17+
use MaplePHP\Blunder\Run;
18+
use MaplePHP\Unitary\TestWrapper;
19+
use MaplePHP\Unitary\Unit;
20+
21+
// If you add true to Unit it will run in quite mode
22+
// and only report if it finds any errors!
23+
24+
25+
$unit = new Unit();
26+
27+
$unit->case("MaplePHP Blunder handler test", function ($inst) {
28+
29+
// SilentHandler will hide the error that I have added in this file
30+
// and is using to test the Blunder library
31+
32+
$run = new Run(new SilentHandler());
33+
$run->severity()
34+
->excludeSeverityLevels([E_WARNING, E_USER_WARNING])
35+
->redirectTo(function ($errNo, $errStr, $errFile, $errLine) use ($inst) {
36+
37+
$func = function (string $className) {
38+
$dispatch = $this->wrapper($className)->bind(function ($exception) {
39+
$this->setExitCode(null);
40+
ob_start();
41+
$this->exceptionHandler($exception);
42+
return ob_get_clean();
43+
});
44+
return $dispatch(new Exception("Mock exception"));
45+
};;
46+
47+
$inst->add($func(HtmlHandler::class), [
48+
'length' => 100,
49+
'isFullHtml' => true
50+
], "HtmlHandler do not return a valid HTML string");
51+
52+
$inst->add($func(JsonHandler::class), [
53+
'length' => 100,
54+
'isJson' => true
55+
], "JsonHandler do not return a valid JSON string");
56+
57+
$inst->add($func(TextHandler::class), [
58+
'length' => [10],
59+
], "TextHandler do not return a valid CLI string");
60+
61+
$inst->add($func(PlainTextHandler::class), [
62+
'length' => [10],
63+
], "PlainTextHandler do not return a valid CLI string");
64+
65+
$inst->add($func(XmlHandler::class), [
66+
'length' => [10],
67+
], "CliHandler do not return a valid CLI string");
68+
69+
$inst->add($func(CliHandler::class), [
70+
'length' => [1],
71+
], "CliHandler do not return a valid CLI string");
72+
73+
return true;
74+
});
75+
$run->load();
76+
77+
// Mock error
78+
echo $helloWorld;
79+
});
80+
81+
$unit->execute();

tests/unitary-blunder-redirect.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* This is how a template test file should look like but
5+
* when used in MaplePHP framework you can skip the "bash code" at top and the "autoload file"!
6+
*/
7+
8+
use MaplePHP\Blunder\Handlers\SilentHandler;
9+
use MaplePHP\Blunder\Run;
10+
use MaplePHP\Unitary\Unit;
11+
12+
// If you add true to Unit it will run in quite mode
13+
// and only report if it finds any errors!
14+
15+
$unit = new Unit();
16+
17+
$unit->case("MaplePHP Blunder redirect test", function ($inst) {
18+
19+
// SilentHandler will hide the error that I have added in this file
20+
// and is using to test the Blunder library
21+
$run = new Run(new SilentHandler());
22+
$run->severity()
23+
->excludeSeverityLevels([E_WARNING, E_USER_WARNING])
24+
->redirectTo(function ($errNo, $errStr, $errFile, $errLine) use ($inst) {
25+
$inst->add($errNo, [
26+
'equal' => 2
27+
]);
28+
29+
$inst->add($errStr, [
30+
'equal' => 'Undefined variable $helloWorld'
31+
]);
32+
33+
$inst->add(basename($errFile), [
34+
'equal' => 'unitary-blunder-redirect.php'
35+
]);
36+
37+
$inst->add(basename($errLine), [
38+
'isInt' => true
39+
]);
40+
});
41+
$run->load();
42+
echo $helloWorld;
43+
});
44+
45+
$unit->execute();

0 commit comments

Comments
 (0)