lock = $lock; try { $stream = fopen($path, $mode); } catch(ErrorException $ex) { throw new IOException('An error occurred while trying to open a file.', $ex->getCode(), $ex); } if($stream === false) throw new IOException('An unhandled error occurred while trying to open a file. Exceptions::convertErrors() has probably not been called.'); parent::__construct($stream); if($this->lock & self::LOCK_WRITE) flock($this->stream, LOCK_EX); elseif($this->lock & self::LOCK_READ) flock($this->stream, LOCK_SH); } public function close(): void { if($this->lock !== self::LOCK_NONE) flock($this->stream, LOCK_UN); parent::close(); } public static function openRead(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::OPEN_READ, $lock); } public static function openReadWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::OPEN_READ_WRITE, $lock); } public static function newWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::NEW_WRITE, $lock); } public static function newReadWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::NEW_READ_WRITE, $lock); } public static function appendWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::APPEND_WRITE, $lock); } public static function appendReadWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::APPEND_READ_WRITE, $lock); } public static function createWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::CREATE_WRITE, $lock); } public static function createReadWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::CREATE_READ_WRITE, $lock); } public static function openOrCreateWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::OPEN_OR_CREATE_WRITE, $lock); } public static function openOrCreateReadWrite(string $path, int $lock = self::LOCK_NONE): FileStream { return new FileStream($path, self::OPEN_OR_CREATE_READ_WRITE, $lock); } }