* @copyright 2014 Fabian Grutschus. All rights reserved. * @license BSD * @link http://github.com/fabiang/xmpp */ namespace Fabiang\Xmpp\Util; use Fabiang\Xmpp\Exception\InvalidArgumentException; use Fabiang\Xmpp\Exception\ErrorException; /** * XML utility methods. * * @package Xmpp\Util */ class ErrorHandler { /** * Method to be called. * * @var callable */ protected $method; /** * Arguments for method. * * @var array */ protected $arguments = array(); public function __construct($method) { if (!is_callable($method)) { throw new InvalidArgumentException('Argument 1 of "' . __METHOD__ . '" must be a callable'); } $arguments = func_get_args(); array_shift($arguments); $this->method = $method; $this->arguments = $arguments; } /** * Execute a function and handle all types of errors. * * @param string $file * @param int $line * @return mixed * @throws ErrorException */ public function execute($file, $line) { set_error_handler(function ($errno, $errstr) use ($file, $line) { throw new ErrorException($errstr, 0, $errno, $file, $line); }); try { $value = call_user_func_array($this->method, $this->arguments); restore_error_handler(); return $value; } catch (ErrorException $exception) { restore_error_handler(); throw $exception; } } }