- md5
- 0b9d326f4d3a0a53c4914142c29ca322
- sha1
- 013f98b92cd85943ad0a3979a71ef18553b3b258
- sha256
- 181e0faed1d5a9bdb945096581531f46dc9545390b711ed367793a55cac6341d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
<?php
declare(strict_types=1);
namespace Laminas\Diactoros;
use Psr\Http\Message\StreamInterface;
use Stringable;
use function array_key_exists;
use const SEEK_SET;
/**
* Implementation of PSR HTTP streams
*/
class CallbackStream implements StreamInterface, Stringable
{
/** @var callable|null */
protected $callback;
/**
* @throws Exception\InvalidArgumentException
*/
public function __construct(callable $callback)
{
$this->attach($callback);
}
/**
* {@inheritdoc}
*/
public function __toString(): string
{
return $this->getContents();
}
/**
* {@inheritdoc}
*/
public function close(): void
{
$this->callback = null;
}
/**
* {@inheritdoc}
*
* @return null|callable
*/
public function detach(): ?callable
{
$callback = $this->callback;
$this->callback = null;
return $callback;
}
/**
* Attach a new callback to the instance.
*/
public function attach(callable $callback): void
{
$this->callback = $callback;
}
/**
* {@inheritdoc}
*/
public function getSize(): ?int
{
return null;
}
/**
* {@inheritdoc}
*/
public function tell(): int
{
throw Exception\UntellableStreamException::forCallbackStream();
}
/**
* {@inheritdoc}
*/
public function eof(): bool
{
return empty($this->callback);
}
/**
* {@inheritdoc}
*/
public function isSeekable(): bool
{
return false;
}
/**
* {@inheritdoc}
*
* @param int $offset
* @param int $whence
* @return void
*/
public function seek($offset, $whence = SEEK_SET)
{
throw Exception\UnseekableStreamException::forCallbackStream();
}
/**
* {@inheritdoc}
*/
public function rewind(): void
{
throw Exception\UnrewindableStreamException::forCallbackStream();
}
/**
* {@inheritdoc}
*/
public function isWritable(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function write($string): void
{
throw Exception\UnwritableStreamException::forCallbackStream();
}
/**
* {@inheritdoc}
*/
public function isReadable(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function read($length): string
{
throw Exception\UnreadableStreamException::forCallbackStream();
}
/**
* {@inheritdoc}
*/
public function getContents(): string
{
$callback = $this->detach();
$contents = $callback ? $callback() : '';
return (string) $contents;
}
/**
* {@inheritdoc}
*/
public function getMetadata($key = null)
{
$metadata = [
'eof' => $this->eof(),
'stream_type' => 'callback',
'seekable' => false,
];
if (null === $key) {
return $metadata;
}
if (! array_key_exists($key, $metadata)) {
return null;
}
return $metadata[$key];
}
}
The same content is in 109 releases, Joomla 5.0.0-alpha1 to 6.0.0-alpha3. See them all, or every version of this file.