ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Whoops\Run Class Reference
+ Collaboration diagram for Whoops\Run:

Public Member Functions

 pushHandler ($handler)
 Pushes a handler to the end of the stack. More...
 
 popHandler ()
 Removes the last handler in the stack and returns it. More...
 
 getHandlers ()
 Returns an array with all handlers, in the order they were added to the stack. More...
 
 clearHandlers ()
 Clears all handlers in the handlerStack, including the default PrettyPage handler. More...
 
 register ()
 Registers this instance as an error handler. More...
 
 unregister ()
 Unregisters all handlers registered by this Whoops instance. More...
 
 allowQuit ($exit=null)
 Should Whoops allow Handlers to force the script to quit? More...
 
 silenceErrorsInPaths ($patterns, $levels=10240)
 Silence particular errors in particular files. More...
 
 sendHttpCode ($code=null)
 
 writeToOutput ($send=null)
 Should Whoops push output directly to the client? If this is false, output will be returned by handleException. More...
 
 handleException (Exception $exception)
 Handles an exception, ultimately generating a Whoops error page. More...
 
 handleError ($level, $message, $file=null, $line=null)
 Converts generic PHP errors to instances, before passing them off to be handled. More...
 
 handleShutdown ()
 Special case to deal with Fatal errors and the like. More...
 

Data Fields

const EXCEPTION_HANDLER = "handleException"
 
const ERROR_HANDLER = "handleError"
 
const SHUTDOWN_HANDLER = "handleShutdown"
 

Protected Member Functions

 getInspector (Exception $exception)
 

Protected Attributes

 $isRegistered
 
 $allowQuit = true
 
 $sendOutput = true
 
 $sendHttpCode = 500
 
 $handlerStack = array()
 
 $silencedPatterns = array()
 

Private Member Functions

 writeToOutputNow ($output)
 Echo something to the browser. More...
 

Static Private Member Functions

static isLevelFatal ($level)
 

Private Attributes

 $canThrowExceptions = true
 

Detailed Description

Definition at line 17 of file Run.php.

Member Function Documentation

◆ allowQuit()

Whoops\Run::allowQuit (   $exit = null)

Should Whoops allow Handlers to force the script to quit?

Parameters
bool | int$exit
Returns
bool

Definition at line 148 of file Run.php.

149  {
150  if (func_num_args() == 0) {
151  return $this->allowQuit;
152  }
153 
154  return $this->allowQuit = (bool) $exit;
155  }
allowQuit($exit=null)
Should Whoops allow Handlers to force the script to quit?
Definition: Run.php:148
$allowQuit
Definition: Run.php:24

◆ clearHandlers()

Whoops\Run::clearHandlers ( )

Clears all handlers in the handlerStack, including the default PrettyPage handler.

Returns
Run

Definition at line 88 of file Run.php.

89  {
90  $this->handlerStack = array();
91  return $this;
92  }

◆ getHandlers()

Whoops\Run::getHandlers ( )

Returns an array with all handlers, in the order they were added to the stack.

Returns
array

Definition at line 78 of file Run.php.

79  {
80  return $this->handlerStack;
81  }
$handlerStack
Definition: Run.php:35

◆ getInspector()

Whoops\Run::getInspector ( Exception  $exception)
protected
Parameters
Exception$exception
Returns
Inspector

Definition at line 98 of file Run.php.

99  {
100  return new Inspector($exception);
101  }

◆ handleError()

Whoops\Run::handleError (   $level,
  $message,
  $file = null,
  $line = null 
)

Converts generic PHP errors to instances, before passing them off to be handled.

This method MUST be compatible with set_error_handler.

Parameters
int$level
string$message
string$file
int$line
Returns
bool
Exceptions
ErrorException

Definition at line 309 of file Run.php.

References $file.

310  {
311  if ($level & error_reporting()) {
312  foreach ($this->silencedPatterns as $entry) {
313  $pathMatches = (bool) preg_match($entry["pattern"], $file);
314  $levelMatches = $level & $entry["levels"];
315  if ($pathMatches && $levelMatches) {
316  // Ignore the error, abort handling
317  return true;
318  }
319  }
320 
321  $exception = new ErrorException($message, $level, 0, $file, $line);
322  if ($this->canThrowExceptions) {
323  throw $exception;
324  } else {
325  $this->handleException($exception);
326  }
327  // Do not propagate errors which were already handled by Whoops.
328  return true;
329  }
330 
331  // Propagate error to the next handler, allows error_get_last() to
332  // work on silenced errors.
333  return false;
334  }
print $file
handleException(Exception $exception)
Handles an exception, ultimately generating a Whoops error page.
Definition: Run.php:233

◆ handleException()

Whoops\Run::handleException ( Exception  $exception)

Handles an exception, ultimately generating a Whoops error page.

Parameters
Exception$exception
Returns
string Output generated by handlers

Definition at line 233 of file Run.php.

References exit.

234  {
235  // Walk the registered handlers in the reverse order
236  // they were registered, and pass off the exception
237  $inspector = $this->getInspector($exception);
238 
239  // Capture output produced while handling the exception,
240  // we might want to send it straight away to the client,
241  // or return it silently.
242  ob_start();
243 
244  // Just in case there are no handlers:
245  $handlerResponse = null;
246 
247  foreach (array_reverse($this->handlerStack) as $handler) {
248  $handler->setRun($this);
249  $handler->setInspector($inspector);
250  $handler->setException($exception);
251 
252  // The HandlerInterface does not require an Exception passed to handle()
253  // and neither of our bundled handlers use it.
254  // However, 3rd party handlers may have already relied on this parameter,
255  // and removing it would be possibly breaking for users.
256  $handlerResponse = $handler->handle($exception);
257 
258  if (in_array($handlerResponse, array(Handler::LAST_HANDLER, Handler::QUIT))) {
259  // The Handler has handled the exception in some way, and
260  // wishes to quit execution (Handler::QUIT), or skip any
261  // other handlers (Handler::LAST_HANDLER). If $this->allowQuit
262  // is false, Handler::QUIT behaves like Handler::LAST_HANDLER
263  break;
264  }
265  }
266 
267  $willQuit = $handlerResponse == Handler::QUIT && $this->allowQuit();
268 
269  $output = ob_get_clean();
270 
271  // If we're allowed to, send output generated by handlers directly
272  // to the output, otherwise, and if the script doesn't quit, return
273  // it so that it may be used by the caller
274  if ($this->writeToOutput()) {
275  // @todo Might be able to clean this up a bit better
276  // If we're going to quit execution, cleanup all other output
277  // buffers before sending our own output:
278  if ($willQuit) {
279  while (ob_get_level() > 0) {
280  ob_end_clean();
281  }
282  }
283 
284  $this->writeToOutputNow($output);
285  }
286 
287  if ($willQuit) {
288  flush(); // HHVM fix for https://github.com/facebook/hhvm/issues/4055
289  exit(1);
290  }
291 
292  return $output;
293  }
exit
Definition: login.php:54
writeToOutput($send=null)
Should Whoops push output directly to the client? If this is false, output will be returned by handle...
Definition: Run.php:217
writeToOutputNow($output)
Echo something to the browser.
Definition: Run.php:370
getInspector(Exception $exception)
Definition: Run.php:98
allowQuit($exit=null)
Should Whoops allow Handlers to force the script to quit?
Definition: Run.php:148

◆ handleShutdown()

Whoops\Run::handleShutdown ( )

Special case to deal with Fatal errors and the like.

Definition at line 339 of file Run.php.

340  {
341  // If we reached this step, we are in shutdown handler.
342  // An exception thrown in a shutdown handler will not be propagated
343  // to the exception handler. Pass that information along.
344  $this->canThrowExceptions = false;
345 
346  $error = error_get_last();
347  if ($error && $this->isLevelFatal($error['type'])) {
348  // If there was a fatal error,
349  // it was not handled in handleError yet.
350  $this->handleError(
351  $error['type'],
352  $error['message'],
353  $error['file'],
354  $error['line']
355  );
356  }
357  }
handleError($level, $message, $file=null, $line=null)
Converts generic PHP errors to instances, before passing them off to be handled. ...
Definition: Run.php:309
static isLevelFatal($level)
Definition: Run.php:396

◆ isLevelFatal()

static Whoops\Run::isLevelFatal (   $level)
staticprivate

Definition at line 396 of file Run.php.

References $errors.

397  {
398  $errors = E_ERROR;
399  $errors |= E_PARSE;
400  $errors |= E_CORE_ERROR;
401  $errors |= E_CORE_WARNING;
402  $errors |= E_COMPILE_ERROR;
403  $errors |= E_COMPILE_WARNING;
404  return ($level & $errors) > 0;
405  }
$errors

◆ popHandler()

Whoops\Run::popHandler ( )

Removes the last handler in the stack and returns it.

Returns null if there"s nothing else to pop.

Returns
null|HandlerInterface

Definition at line 68 of file Run.php.

69  {
70  return array_pop($this->handlerStack);
71  }

◆ pushHandler()

Whoops\Run::pushHandler (   $handler)

Pushes a handler to the end of the stack.

Exceptions
InvalidArgumentExceptionIf argument is not callable or instance of HandlerInterface
Parameters
Callable | HandlerInterface$handler
Returns
Run

Definition at line 46 of file Run.php.

47  {
48  if (is_callable($handler)) {
49  $handler = new CallbackHandler($handler);
50  }
51 
52  if (!$handler instanceof HandlerInterface) {
53  throw new InvalidArgumentException(
54  "Argument to " . __METHOD__ . " must be a callable, or instance of"
55  . "Whoops\\Handler\\HandlerInterface"
56  );
57  }
58 
59  $this->handlerStack[] = $handler;
60  return $this;
61  }

◆ register()

Whoops\Run::register ( )

Registers this instance as an error handler.

Returns
Run

Definition at line 107 of file Run.php.

108  {
109  if (!$this->isRegistered) {
110  // Workaround PHP bug 42098
111  // https://bugs.php.net/bug.php?id=42098
112  class_exists("\\Whoops\\Exception\\ErrorException");
113  class_exists("\\Whoops\\Exception\\FrameCollection");
114  class_exists("\\Whoops\\Exception\\Frame");
115  class_exists("\\Whoops\\Exception\\Inspector");
116 
117  set_error_handler(array($this, self::ERROR_HANDLER));
118  set_exception_handler(array($this, self::EXCEPTION_HANDLER));
119  register_shutdown_function(array($this, self::SHUTDOWN_HANDLER));
120 
121  $this->isRegistered = true;
122  }
123 
124  return $this;
125  }

◆ sendHttpCode()

Whoops\Run::sendHttpCode (   $code = null)

Definition at line 188 of file Run.php.

References $code.

189  {
190  if (func_num_args() == 0) {
191  return $this->sendHttpCode;
192  }
193 
194  if (!$code) {
195  return $this->sendHttpCode = false;
196  }
197 
198  if ($code === true) {
199  $code = 500;
200  }
201 
202  if ($code < 400 || 600 <= $code) {
203  throw new InvalidArgumentException(
204  "Invalid status code '$code', must be 4xx or 5xx"
205  );
206  }
207 
208  return $this->sendHttpCode = $code;
209  }
$code
Definition: example_050.php:99
sendHttpCode($code=null)
Definition: Run.php:188
$sendHttpCode
Definition: Run.php:30

◆ silenceErrorsInPaths()

Whoops\Run::silenceErrorsInPaths (   $patterns,
  $levels = 10240 
)

Silence particular errors in particular files.

Parameters
array | string$patternsList or a single regex pattern to match
int$levelsDefaults to E_STRICT | E_DEPRECATED
Returns

Definition at line 163 of file Run.php.

164  {
165  $this->silencedPatterns = array_merge(
166  $this->silencedPatterns,
167  array_map(
168  function ($pattern) use ($levels) {
169  return array(
170  "pattern" => $pattern,
171  "levels" => $levels,
172  );
173  },
174  (array) $patterns
175  )
176  );
177  return $this;
178  }

◆ unregister()

Whoops\Run::unregister ( )

Unregisters all handlers registered by this Whoops instance.

Returns
Run

Definition at line 131 of file Run.php.

132  {
133  if ($this->isRegistered) {
134  restore_exception_handler();
135  restore_error_handler();
136 
137  $this->isRegistered = false;
138  }
139 
140  return $this;
141  }

◆ writeToOutput()

Whoops\Run::writeToOutput (   $send = null)

Should Whoops push output directly to the client? If this is false, output will be returned by handleException.

Parameters
bool | int$send
Returns
bool

Definition at line 217 of file Run.php.

218  {
219  if (func_num_args() == 0) {
220  return $this->sendOutput;
221  }
222 
223  return $this->sendOutput = (bool) $send;
224  }
$sendOutput
Definition: Run.php:25

◆ writeToOutputNow()

Whoops\Run::writeToOutputNow (   $output)
private

Echo something to the browser.

Parameters
string$output
Returns
$this

Definition at line 370 of file Run.php.

371  {
372  if ($this->sendHttpCode() && \Whoops\Util\Misc::canSendHeaders()) {
373  $httpCode = $this->sendHttpCode();
374 
375  if (function_exists('http_response_code')) {
376  http_response_code($httpCode);
377  } else {
378  // http_response_code is added in 5.4.
379  // For compatibility with 5.3 we use the third argument in header call
380  // First argument must be a real header.
381  // If it is empty, PHP will ignore the third argument.
382  // If it is invalid, such as a single space, Apache will handle it well,
383  // but the PHP development server will hang.
384  // Setting a full status line would require us to hardcode
385  // string values for all different status code, and detect the protocol.
386  // which is an extra error-prone complexity.
387  header('X-Ignore-This: 1', true, $httpCode);
388  }
389  }
390 
391  echo $output;
392 
393  return $this;
394  }
sendHttpCode($code=null)
Definition: Run.php:188
Whoops - php errors for cool kids.

Field Documentation

◆ $allowQuit

Whoops\Run::$allowQuit = true
protected

Definition at line 24 of file Run.php.

◆ $canThrowExceptions

Whoops\Run::$canThrowExceptions = true
private

Definition at line 363 of file Run.php.

◆ $handlerStack

Whoops\Run::$handlerStack = array()
protected

Definition at line 35 of file Run.php.

◆ $isRegistered

Whoops\Run::$isRegistered
protected

Definition at line 23 of file Run.php.

◆ $sendHttpCode

Whoops\Run::$sendHttpCode = 500
protected

Definition at line 30 of file Run.php.

◆ $sendOutput

Whoops\Run::$sendOutput = true
protected

Definition at line 25 of file Run.php.

◆ $silencedPatterns

Whoops\Run::$silencedPatterns = array()
protected

Definition at line 37 of file Run.php.

◆ ERROR_HANDLER

const Whoops\Run::ERROR_HANDLER = "handleError"

Definition at line 20 of file Run.php.

◆ EXCEPTION_HANDLER

const Whoops\Run::EXCEPTION_HANDLER = "handleException"

Definition at line 19 of file Run.php.

◆ SHUTDOWN_HANDLER

const Whoops\Run::SHUTDOWN_HANDLER = "handleShutdown"

Definition at line 21 of file Run.php.


The documentation for this class was generated from the following file: