Skip to content

Commit 7e83187

Browse files
committed
StreamContainerInterface
1 parent 5adfd1e commit 7e83187

File tree

9 files changed

+83
-19
lines changed

9 files changed

+83
-19
lines changed

Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cs: composer.lock
1414
stan: composer.lock
1515
./vendor/bin/phpstan analyse --memory-limit 256M
1616

17-
coverage: composer.lock
17+
coverage: composer.lock clean-coverage
1818
./vendor/bin/phpunit --coverage-clover coverage/clover.xml --coverage-html=coverage -d --min-coverage=100
1919

2020
composer.phar:
@@ -25,7 +25,10 @@ composer.lock: composer.phar
2525

2626
vendor/bin/phpunit: install
2727

28-
clean:
29-
rm composer.lock
30-
rm -r vendor
31-
rm -r coverage
28+
clean: clean-coverage
29+
rm -f composer.lock
30+
rm -rf vendor
31+
rm -rf coverage
32+
33+
clean-coverage:
34+
rm -rf coverage

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Install with [Composer](https://getcomposer.org/);
1717
composer require phrity/net-stream
1818
```
1919

20+
## Interfaces
21+
22+
* [StreamInterface](docs/StreamInterface.md) - Interface indicating a Stream implementation
23+
* [StreamContainerInterface](docs/StreamContainerInterface.md) - Interface indicating a class containg a Stream
24+
2025
## Included classes
2126

2227
* [Stream](docs/Stream.md) - PSR-7 StreamFactory compatible Stream class
@@ -33,6 +38,7 @@ composer require phrity/net-stream
3338

3439
| Version | PHP | |
3540
| --- | --- | --- |
41+
| `2.4` | `^8.1` | StreamInterface, StreamContainerInterface |
3642
| `2.3` | `^8.1` | Float timeout, hasContents method, Listeners |
3743
| `2.2` | `^8.1` | Improved context handling |
3844
| `2.1` | `^8.0` | Set context on server |

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"psr/http-message": "^1.1 | ^2.0"
3030
},
3131
"require-dev": {
32-
"php-coveralls/php-coveralls": "^2.0",
3332
"phpstan/phpstan": "^2.0",
3433
"phpunit/phpunit": "^10.0 | ^11.0 | ^12.0",
3534
"phrity/net-uri": "^2.0",

docs/Stream.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Stream implements StreamInterface, Stringable
4343

4444
// Additional methods
4545

46-
// Get stream context
46+
// StreamInterface methods
4747
public function getContext(): Context;
4848
public function getResource(): resource;
4949
}

docs/StreamContainerInterface.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[Documentation](../README.md) / StreamContainerInterface
2+
3+
# StreamContainerInterface
4+
5+
Interface indicating a class containg a Stream.
6+
7+
## Synopsis
8+
9+
```php
10+
namespace Phrity\Net;
11+
12+
interface StreamContainerInterface
13+
{
14+
// Methods
15+
16+
public function getStream(): StreamInterface;
17+
}
18+
```

docs/StreamInterface.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Documentation](../README.md) / StreamInterface
2+
3+
# StreamInterface
4+
5+
Interface indicating a Stream implementation.
6+
7+
## Synopsis
8+
9+
```php
10+
namespace Phrity\Net;
11+
12+
interface StreamInterface extends Psr\Http\Message\StreamInterface
13+
{
14+
// Methods
15+
16+
public function getContext(): Context;
17+
public function getResource(): resource;
18+
}
19+
```

src/Stream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function __toString(): string
263263
}
264264

265265

266-
// ---------- Extended methods ------------------------------------------------------------------------------------
266+
// ---------- StreamInterface methods -----------------------------------------------------------------------------
267267

268268
/**
269269
* Return context for stream.

src/StreamCollection.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class StreamCollection implements Countable, Iterator
1616
{
1717
protected ErrorHandler $handler;
18-
/** @var array<string, StreamInterface> */
18+
/** @var array<string, StreamInterface|StreamContainerInterface> */
1919
private array $streams = [];
2020

2121
/**
@@ -31,12 +31,12 @@ public function __construct()
3131

3232
/**
3333
* Attach stream to collection.
34-
* @param StreamInterface $attach Stream to attach.
34+
* @param StreamInterface|StreamContainerInterface $attach Stream to attach.
3535
* @param string|null $key Definable name of stream.
3636
* @return string Name of stream.
3737
* @throws StreamException If already attached.
3838
*/
39-
public function attach(StreamInterface $attach, string|null $key = null): string
39+
public function attach(StreamInterface|StreamContainerInterface $attach, string|null $key = null): string
4040
{
4141
if ($key && array_key_exists($key, $this->streams)) {
4242
throw new StreamException(StreamException::COLLECT_KEY_CONFLICT, ['key' => $key]);
@@ -48,10 +48,10 @@ public function attach(StreamInterface $attach, string|null $key = null): string
4848

4949
/**
5050
* Detach stream from collection.
51-
* @param StreamInterface|string $detach Stream or name of stream to detach.
51+
* @param StreamInterface|StreamContainerInterface|string $detach Stream or name of stream to detach.
5252
* @return bool If a stream was detached.
5353
*/
54-
public function detach(StreamInterface|string $detach): bool
54+
public function detach(StreamInterface|StreamContainerInterface|string $detach): bool
5555
{
5656
if (is_string($detach)) {
5757
if (array_key_exists($detach, $this->streams)) {
@@ -78,7 +78,7 @@ public function getReadable(): self
7878
{
7979
$readables = new self();
8080
foreach ($this->streams as $key => $stream) {
81-
if ($stream->isReadable()) {
81+
if ($this->getStream($stream)->isReadable()) {
8282
$readables->attach($stream, $key);
8383
}
8484
}
@@ -93,7 +93,7 @@ public function getWritable(): self
9393
{
9494
$writables = new self();
9595
foreach ($this->streams as $key => $stream) {
96-
if ($stream->isWritable()) {
96+
if ($this->getStream($stream)->isWritable()) {
9797
$writables->attach($stream, $key);
9898
}
9999
}
@@ -116,8 +116,8 @@ public function waitRead(int|float $timeout = 60): self
116116

117117
$read = [];
118118
foreach ($this->streams as $key => $stream) {
119-
if ($stream->isReadable()) {
120-
$read[$key] = $stream->getResource();
119+
if ($this->getStream($stream)->isReadable()) {
120+
$read[$key] = $this->getStream($stream)->getResource();
121121
}
122122
}
123123
if (empty($read)) {
@@ -157,9 +157,9 @@ public function count(): int
157157

158158
/**
159159
* Return the current stream.
160-
* @return StreamInterface|null Current stream.
160+
* @return StreamInterface|StreamContainerInterface|null Current stream.
161161
*/
162-
public function current(): StreamInterface|null
162+
public function current(): StreamInterface|StreamContainerInterface|null
163163
{
164164
return current($this->streams) ?: null;
165165
}
@@ -212,4 +212,9 @@ protected function createKey(): string
212212
} while (array_key_exists($key, $this->streams));
213213
return $key;
214214
}
215+
216+
protected function getStream(StreamInterface|StreamContainerInterface $stream): StreamInterface
217+
{
218+
return $stream instanceof StreamInterface ? $stream : $stream->getStream();
219+
}
215220
}

src/StreamContainerInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Phrity\Net;
4+
5+
/**
6+
* StreamContainerInterface.
7+
*/
8+
interface StreamContainerInterface
9+
{
10+
/**
11+
* @return StreamInterface
12+
*/
13+
public function getStream(): StreamInterface;
14+
}

0 commit comments

Comments
 (0)