/** * Class constructor. */ public function __construct() { $this->errorCatcher = new ErrorCatcher(function (\ErrorException $e) { if ($this->throw) { throw FileSystemException::wrap($e); } }); }
/** * Class constructor. * * @param string $path The file path. * @param string $mode The open mode. See the documentation for fopen() for available modes. * * @throws FileSystemException If the file cannot be open. */ public function __construct($path, $mode) { $this->errorHandler = new ErrorCatcher(function (\ErrorException $e) { if ($this->throw) { throw FileSystemException::wrap($e); } }); $this->throw = true; $this->errorHandler->swallow(E_WARNING, function () use($path, $mode) { $this->handle = fopen($path, $mode); }); }
/** * @param integer $severity The severity of the errors to catch. * @param boolean $throw Whether to throw exceptions or silently ignore errors. * @param callable $function The function to call. * * @return mixed The return value of the called function. * * @throws FileSystemException If an error is caught and $throw is true. */ private function swallow($severity, $throw, callable $function) { if (Path::$errorHandler === null) { Path::$errorHandler = new ErrorCatcher(function (\ErrorException $e) { if (Path::$throw) { throw FileSystemException::wrap($e); } }); } Path::$throw = $throw; return Path::$errorHandler->swallow($severity, $function); }