-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun.php
More file actions
executable file
·120 lines (106 loc) · 3.14 KB
/
Copy pathRun.php
File metadata and controls
executable file
·120 lines (106 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Class Run
*
* Main entry point for initializing the Blunder error handling framework.
* Registers error, exception, and shutdown handlers with severity filtering,
* event hooks, and optional PSR-7 HTTP support.
*
* Provides control over exit behavior, redirection prevention, and
* severity level customization.
*
* @package MaplePHP\Blunder
* @author Daniel Ronkainen
* @license Apache-2.0 license, Copyright © Daniel Ronkainen
* Don't delete this comment, it's part of the license.
*/
namespace MaplePHP\Blunder;
use MaplePHP\Blunder\Interfaces\AbstractHandlerInterface;
use MaplePHP\Blunder\Interfaces\HttpMessagingInterface;
use Closure;
final class Run
{
private AbstractHandlerInterface $handler;
private ?SeverityLevelPool $severity = null;
private bool $removeLocationHeader = false;
public function __construct(AbstractHandlerInterface $handler, ?HttpMessagingInterface $http = null)
{
$this->handler = $handler;
if ($http !== null) {
$this->handler->setHttp($http);
}
}
/**
* You can change exit code form default 1 on failure or disable it completely by passing null or false
*
* @param int|bool|null $code
* @return $this
*/
public function setExitCode(int|null|bool $code): self
{
$this->handler->setExitCode($code);
return $this;
}
/**
* Disable exit on failure
*
* @return $this
*/
public function disableExitCode(): self
{
$this->handler->setExitCode(false);
return $this;
}
/**
* Enable or disable the removal of the 'Location' header to prevent redirections.
*
* @param bool $removeRedirect
* @return $this
*/
public function removeLocationHeader(bool $removeRedirect): self
{
$this->removeLocationHeader = $removeRedirect;
return $this;
}
/**
* Access severity instance to set or exclude severity levels from error handler
*
* @return SeverityLevelPool
*/
public function severity(): SeverityLevelPool
{
if ($this->severity === null) {
$this->severity = new SeverityLevelPool();
}
return $this->severity;
}
/**
* The event callable will be triggered when an error occur.
* Note: Will add PSR-14 support for dispatch in the future.
*
* @param Closure $event
* @return void
*/
public function event(Closure $event): void
{
$this->handler->event($event);
}
/**
* Init the handlers
*
* @return void
*/
public function load(): void
{
if ($this->removeLocationHeader && !headers_sent()) {
header_remove('location');
}
// Will actually clear unwanted output in some cases
ob_start();
$this->handler->setSeverity($this->severity());
set_error_handler([$this->handler, "errorHandler"], $this->severity()->getSeverityLevelMask());
set_exception_handler([$this->handler, "exceptionHandler"]);
register_shutdown_function([$this->handler, "shutdownHandler"]);
ob_clean();
}
}