Files
cunkebao_v3/Server/vendor/guzzlehttp/psr7/src/LazyOpenStream.php

50 lines
1.1 KiB
PHP
Raw Normal View History

2025-03-12 12:21:57 +08:00
<?php
2025-03-24 14:59:19 +08:00
declare(strict_types=1);
2025-03-12 12:21:57 +08:00
namespace GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
/**
* Lazily reads or writes to a file that is opened only after an IO operation
* take place on the stream.
*/
2025-03-24 14:59:19 +08:00
final class LazyOpenStream implements StreamInterface
2025-03-12 12:21:57 +08:00
{
use StreamDecoratorTrait;
2025-03-24 14:59:19 +08:00
/** @var string */
2025-03-12 12:21:57 +08:00
private $filename;
/** @var string */
private $mode;
2025-03-24 14:59:19 +08:00
/**
* @var StreamInterface
*/
private $stream;
2025-03-12 12:21:57 +08:00
/**
* @param string $filename File to lazily open
* @param string $mode fopen mode to use when opening the stream
*/
2025-03-24 14:59:19 +08:00
public function __construct(string $filename, string $mode)
2025-03-12 12:21:57 +08:00
{
$this->filename = $filename;
$this->mode = $mode;
2025-03-24 14:59:19 +08:00
// unsetting the property forces the first access to go through
// __get().
unset($this->stream);
2025-03-12 12:21:57 +08:00
}
/**
* Creates the underlying stream lazily when required.
*/
2025-03-24 14:59:19 +08:00
protected function createStream(): StreamInterface
2025-03-12 12:21:57 +08:00
{
return Utils::streamFor(Utils::tryFopen($this->filename, $this->mode));
}
}