2020-05-13 17:15:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Internal;
|
|
|
|
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Stream;
|
|
|
|
|
|
|
|
/**
|
2020-05-28 19:59:55 +02:00
|
|
|
* Wraps a Stream instance that has public methods to emit, complete, and fail into an object that only allows
|
2020-05-13 17:15:21 +02:00
|
|
|
* access to the public API methods and sets $disposed to true when the object is destroyed.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @template-covariant TValue
|
|
|
|
* @template-implements Stream<TValue>
|
|
|
|
*/
|
2020-05-19 17:49:40 +02:00
|
|
|
final class AutoDisposingStream implements Stream
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-28 19:59:55 +02:00
|
|
|
/** @var EmitSource<TValue, null> */
|
2020-05-19 17:49:40 +02:00
|
|
|
private $source;
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
public function __construct(EmitSource $source)
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
$this->source = $source;
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
$this->source->dispose();
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-14 21:14:54 +02:00
|
|
|
* @inheritDoc
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
|
|
|
public function continue(): Promise
|
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
return $this->source->continue();
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
2020-05-13 21:59:31 +02:00
|
|
|
|
|
|
|
/**
|
2020-05-14 21:14:54 +02:00
|
|
|
* @inheritDoc
|
2020-05-13 21:59:31 +02:00
|
|
|
*/
|
|
|
|
public function dispose()
|
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
$this->source->dispose();
|
2020-05-13 21:59:31 +02:00
|
|
|
}
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|