/** * Construct stream. * * @param string|resource $streamOrPath A resource or path. * @param string $mode Stream mode, see {@see fopen}. * @throws InvalidArgumentException If the stream can not be opened. */ public function __construct($streamOrPath, $mode = 'rb') { if (is_resource($streamOrPath)) { $this->stream = $streamOrPath; } else { \Jivoo\Assume::isString($streamOrPath); $error = ErrorHandler::detect(function () use($streamOrPath, $mode) { $this->stream = fopen($streamOrPath, $mode); }); if ($error or $this->stream === false) { throw new InvalidArgumentException('Could not open stream', 0, $error); } } }
public static function dumpException(\Exception $exception, $stream = STDERR) { if ($exception instanceof \ErrorException) { $title = 'Fatal error (' . ErrorHandler::toString($exception->getSeverity()) . ')'; } else { $title = get_class($exception); } fwrite($stream, $title . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine() . PHP_EOL . PHP_EOL); fwrite($stream, 'Stack trace:' . PHP_EOL); $trace = $exception->getTrace(); foreach ($trace as $i => $call) { $message = ' ' . sprintf('% 2d', $i) . '. '; if (isset($call['file'])) { $message .= $call['file'] . ':'; $message .= $call['line'] . ' '; } if (isset($call['class'])) { $message .= $call['class'] . '::'; } $message .= $call['function'] . '('; $arglist = array(); if (isset($call['args'])) { foreach ($call['args'] as $arg) { $arglist[] = is_scalar($arg) ? var_export($arg, true) : gettype($arg); } $message .= implode(', ', $arglist); } $message .= ')' . PHP_EOL; fwrite($stream, $message); } $previous = $exception->getPrevious(); if (isset($previous)) { fwrite($stream, 'Caused by:' . PHP_EOL); self::dumpException($previous); } fflush($stream); }