Skip to content
This repository was archived by the owner on Mar 27, 2019. It is now read-only.

Commit 2c46904

Browse files
committed
Merge branch 'release/2.2.0'
2 parents f00c6cb + 6a4bbab commit 2c46904

11 files changed

+84
-172
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Asplode changelog
22

3+
## 2.2.0 (2016-04-19)
4+
5+
- **[IMPROVED]** Increased the amount of memory reserved by the fatal error
6+
handler, to improve the chances of catching memory exhausted fatals.
7+
38
## 2.1.0 (2015-12-11)
49

510
- **[IMPROVED]** Error exceptions use file and line from the original error, and

src/Asplode.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ abstract class Asplode
2424
* Installs a new error handler, and a new fatal error handler
2525
* simultaneously.
2626
*
27-
* @param Isolator|null $isolator The isolator to use.
27+
* @param integer $reservedMemory The amount of memory to reserve for fatal error handling.
28+
* @param Isolator|null $isolator The isolator to use.
2829
*
2930
* @return tuple<ErrorHandlerInterface,FatalErrorHandlerInterface> A tuple containing the installed error handler and fatal error handler.
3031
* @throws ErrorHandlingConfigurationException If the error reporting level is incorrectly configured.
3132
*/
32-
public static function install(Isolator $isolator = null)
33-
{
34-
$fatalHandler = static::installFatalHandler($isolator);
33+
public static function install(
34+
$reservedMemory = 1048576,
35+
Isolator $isolator = null
36+
) {
37+
$fatalHandler = static::installFatalHandler($reservedMemory, $isolator);
3538

3639
return array(static::installErrorHandler($isolator), $fatalHandler);
3740
}
@@ -58,14 +61,17 @@ public static function installErrorHandler(Isolator $isolator = null)
5861
* This handler will, on shutdown, detect any installed exception handler,
5962
* and pass an exception representing any fatal errors to said handler.
6063
*
61-
* @param Isolator|null $isolator The isolator to use.
64+
* @param integer $reservedMemory The amount of memory to reserve for fatal error handling.
65+
* @param Isolator|null $isolator The isolator to use.
6266
*
6367
* @return FatalErrorHandlerInterface The installed fatal error handler.
6468
*/
65-
public static function installFatalHandler(Isolator $isolator = null)
66-
{
69+
public static function installFatalHandler(
70+
$reservedMemory = 1048576,
71+
Isolator $isolator = null
72+
) {
6773
$handler = new FatalErrorHandler(null, $isolator);
68-
$handler->install();
74+
$handler->install($reservedMemory);
6975

7076
return $handler;
7177
}

src/ErrorHandler.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct(
3636
Isolator $isolator = null
3737
) {
3838
$this->isolator = Isolator::get($isolator);
39+
3940
if (null === $stack) {
4041
$stack = new ErrorHandlerStack($isolator);
4142
}
@@ -93,14 +94,15 @@ public function fallbackHandler()
9394
*/
9495
public function install()
9596
{
96-
if (0 === $this->isolator()->error_reporting()) {
97+
if (0 === $this->isolator->error_reporting()) {
9798
throw new ErrorHandlingConfigurationException();
9899
}
100+
99101
if ($this->isInstalled()) {
100102
throw new AlreadyInstalledException();
101103
}
102104

103-
$this->stack()->push($this);
105+
$this->stack->push($this);
104106
}
105107

106108
/**
@@ -110,10 +112,11 @@ public function install()
110112
*/
111113
public function uninstall()
112114
{
113-
$handler = $this->stack()->pop();
115+
$handler = $this->stack->pop();
116+
114117
if ($handler !== $this) {
115118
if (null !== $handler) {
116-
$this->stack()->push($handler);
119+
$this->stack->push($handler);
117120
}
118121

119122
throw new NotInstalledException();
@@ -127,7 +130,7 @@ public function uninstall()
127130
*/
128131
public function isInstalled()
129132
{
130-
return $this->stack()->handler() === $this;
133+
return $this->stack->handler() === $this;
131134
}
132135

133136
/**
@@ -146,7 +149,7 @@ public function handle($severity, $message, $filename, $lineno)
146149
if (
147150
E_DEPRECATED === $severity ||
148151
E_USER_DEPRECATED === $severity ||
149-
0 === $this->isolator()->error_reporting()
152+
0 === $this->isolator->error_reporting()
150153
) {
151154
$fallbackHandler = $this->fallbackHandler();
152155

@@ -172,16 +175,6 @@ public function __invoke($severity, $message, $filename, $lineno)
172175
return $this->handle($severity, $message, $filename, $lineno);
173176
}
174177

175-
/**
176-
* Get the isolator.
177-
*
178-
* @return Isolator The isolator.
179-
*/
180-
protected function isolator()
181-
{
182-
return $this->isolator;
183-
}
184-
185178
private $stack;
186179
private $fallbackHandler;
187180
private $isolator;

src/FatalErrorHandler.php

Lines changed: 27 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Eloquent\Asplode;
1313

1414
use Eloquent\Asplode\Error\FatalErrorException;
15-
use Eloquent\Asplode\Error\FatalErrorExceptionInterface;
1615
use Eloquent\Asplode\Exception\AlreadyInstalledException;
1716
use Eloquent\Asplode\Exception\NotInstalledException;
1817
use Eloquent\Asplode\HandlerStack\ExceptionHandlerStack;
@@ -35,12 +34,14 @@ public function __construct(
3534
Isolator $isolator = null
3635
) {
3736
$this->isolator = Isolator::get($isolator);
37+
3838
if (null === $stack) {
3939
$stack = new ExceptionHandlerStack($isolator);
4040
}
4141

4242
$this->stack = $stack;
43-
$this->isEnabled = $this->isRegistered = false;
43+
$this->isRegistered = false;
44+
$this->isEnabled = false;
4445
}
4546

4647
/**
@@ -56,17 +57,22 @@ public function stack()
5657
/**
5758
* Installs this fatal error handler.
5859
*
60+
* @param integer $reservedMemory The amount of memory to reserve for fatal error handling.
61+
*
5962
* @throws AlreadyInstalledException If this fatal error handler is already installed.
6063
*/
61-
public function install()
64+
public function install($reservedMemory = 1048576)
6265
{
63-
if ($this->isInstalled()) {
66+
if ($this->isEnabled) {
6467
throw new AlreadyInstalledException();
6568
}
6669

67-
if (!$this->isRegistered()) {
68-
$this->beforeRegister();
69-
$this->isolator()->register_shutdown_function($this);
70+
if (!$this->isRegistered) {
71+
$this->reservedMemory =
72+
$this->isolator->str_repeat(' ', $reservedMemory);
73+
$this->isolator
74+
->class_exists('Eloquent\Asplode\Error\FatalErrorException');
75+
$this->isolator->register_shutdown_function($this);
7076
$this->isRegistered = true;
7177
}
7278

@@ -80,7 +86,7 @@ public function install()
8086
*/
8187
public function uninstall()
8288
{
83-
if (!$this->isInstalled()) {
89+
if (!$this->isEnabled) {
8490
throw new NotInstalledException();
8591
}
8692

@@ -94,7 +100,7 @@ public function uninstall()
94100
*/
95101
public function isInstalled()
96102
{
97-
return $this->isRegistered() && $this->isEnabled();
103+
return $this->isRegistered && $this->isEnabled;
98104
}
99105

100106
/**
@@ -107,17 +113,24 @@ public function isInstalled()
107113
*/
108114
public function handle()
109115
{
110-
if (!$this->isEnabled()) {
116+
if (!$this->isEnabled) {
111117
return;
112118
}
113119

114-
$error = $this->isolator()->error_get_last();
120+
$this->reservedMemory = '';
121+
$error = $this->isolator->error_get_last();
122+
115123
if (null === $error) {
116124
return;
117125
}
118126

119-
$this->freeMemory();
120-
$this->handleFatalError(
127+
$handler = $this->stack->handler();
128+
129+
if (null === $handler) {
130+
return;
131+
}
132+
133+
$handler(
121134
new FatalErrorException(
122135
$error['message'],
123136
$error['type'],
@@ -137,95 +150,7 @@ public function handle()
137150
*/
138151
public function __invoke()
139152
{
140-
return $this->handle();
141-
}
142-
143-
/**
144-
* Returns true if this handler is registered.
145-
*
146-
* @return boolean True if this handler is registered.
147-
*/
148-
protected function isRegistered()
149-
{
150-
return $this->isRegistered;
151-
}
152-
153-
/**
154-
* Returns true if this handler is enabled.
155-
*
156-
* @return boolean True if this handler is enabled.
157-
*/
158-
protected function isEnabled()
159-
{
160-
return $this->isEnabled;
161-
}
162-
163-
/**
164-
* Get the isolator.
165-
*
166-
* @return Isolator The isolator.
167-
*/
168-
protected function isolator()
169-
{
170-
return $this->isolator;
171-
}
172-
173-
/**
174-
* This method is called just before the shutdown function is registered.
175-
*/
176-
protected function beforeRegister()
177-
{
178-
$this->loadClasses();
179-
$this->reserveMemory();
180-
}
181-
182-
/**
183-
* Pre-loads any classes or interfaces required in the event of a fatal
184-
* error.
185-
*/
186-
protected function loadClasses()
187-
{
188-
$this->isolator()
189-
->class_exists('Eloquent\Asplode\Error\FatalErrorException');
190-
}
191-
192-
/**
193-
* Reserves an amount of memory for use in the case of an out-of-memory
194-
* fatal error.
195-
*
196-
* @param integer|null $size The amount of memory to reserve.
197-
*/
198-
protected function reserveMemory($size = null)
199-
{
200-
if (null === $size) {
201-
$size = 10240;
202-
}
203-
204-
$this->reservedMemory = $this->isolator()->str_repeat(' ', $size);
205-
}
206-
207-
/**
208-
* Frees the previously reseverd memory.
209-
*/
210-
protected function freeMemory()
211-
{
212-
$this->reservedMemory = '';
213-
}
214-
215-
/**
216-
* Handles PHP fatal errors.
217-
*
218-
* @param FatalErrorExceptionInterface $error The fatal error to handle.
219-
*/
220-
protected function handleFatalError(FatalErrorExceptionInterface $error)
221-
{
222-
$handler = $this->stack()->handler();
223-
224-
if (null === $handler) {
225-
return;
226-
}
227-
228-
$handler($error);
153+
$this->handle();
229154
}
230155

231156
private $stack;

src/FatalErrorHandlerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ interface FatalErrorHandlerInterface
2222
/**
2323
* Installs this fatal error handler.
2424
*
25+
* @param integer $reservedMemory The amount of memory to reserve for fatal error handling.
26+
*
2527
* @throws AlreadyInstalledException If this fatal error handler is already installed.
2628
*/
27-
public function install();
29+
public function install($reservedMemory = 1048576);
2830

2931
/**
3032
* Uninstalls this fatal error handler.

src/HandlerStack/AbstractHandlerStack.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __construct(Isolator $isolator = null)
3737
public function handler()
3838
{
3939
$handler = $this->pop();
40+
4041
if (null !== $handler) {
4142
$this->push($handler);
4243
}
@@ -74,6 +75,7 @@ public function pushAll(array $handlers)
7475
public function clear()
7576
{
7677
$handlers = array();
78+
7779
while (null !== ($handler = $this->pop())) {
7880
$handlers[] = $handler;
7981
}
@@ -108,11 +110,13 @@ public function restore(array $handlers)
108110
public function executeWith($callable, $handler = null)
109111
{
110112
$handlers = $this->clear();
113+
111114
if (null !== $handler) {
112115
$this->push($handler);
113116
}
114117

115118
$error = null;
119+
116120
try {
117121
$result = $callable();
118122
} catch (Exception $error) {
@@ -128,15 +132,5 @@ public function executeWith($callable, $handler = null)
128132
return $result;
129133
}
130134

131-
/**
132-
* Get the isolator.
133-
*
134-
* @return Isolator The isolator.
135-
*/
136-
protected function isolator()
137-
{
138-
return $this->isolator;
139-
}
140-
141-
private $isolator;
135+
protected $isolator;
142136
}

0 commit comments

Comments
 (0)