Skip to content

Commit 8c70a9c

Browse files
committed
Add common PHP I/O stream factories
1 parent 65cb294 commit 8c70a9c

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

src/Factory/CliStream.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\ResourceStream\Factory;
6+
7+
use Phpro\ResourceStream\ErrorHandling\SafeStreamAction;
8+
use Phpro\ResourceStream\Exception\RuntimeException;
9+
use Phpro\ResourceStream\ResourceStream;
10+
11+
final class CliStream
12+
{
13+
/**
14+
* @throws RuntimeException
15+
*
16+
* @return ResourceStream<resource>
17+
*/
18+
public static function stdout(): ResourceStream
19+
{
20+
$resource = SafeStreamAction::run(
21+
static fn () => fopen('php://stdout', 'w'),
22+
'Unable to open file "php://stdout"'
23+
);
24+
25+
return new ResourceStream($resource);
26+
}
27+
28+
/**
29+
* @throws RuntimeException
30+
*
31+
* @return ResourceStream<resource>
32+
*/
33+
public static function stdin(): ResourceStream
34+
{
35+
$resource = SafeStreamAction::run(
36+
static fn () => fopen('php://stdin', 'r'),
37+
'Unable to open file "php://stdin"'
38+
);
39+
40+
return new ResourceStream($resource);
41+
}
42+
43+
/**
44+
* @throws RuntimeException
45+
*
46+
* @return ResourceStream<resource>
47+
*/
48+
public static function stderr(): ResourceStream
49+
{
50+
$resource = SafeStreamAction::run(
51+
static fn () => fopen('php://stderr', 'w'),
52+
'Unable to open file "php://stderr"'
53+
);
54+
55+
return new ResourceStream($resource);
56+
}
57+
}

src/Factory/IOStream.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\ResourceStream\Factory;
6+
7+
use Phpro\ResourceStream\ErrorHandling\SafeStreamAction;
8+
use Phpro\ResourceStream\Exception\RuntimeException;
9+
use Phpro\ResourceStream\ResourceStream;
10+
11+
final class IOStream
12+
{
13+
/**
14+
* @throws RuntimeException
15+
*
16+
* @return ResourceStream<resource>
17+
*/
18+
public static function input(): ResourceStream
19+
{
20+
$resource = SafeStreamAction::run(
21+
static fn () => fopen('php://input', FileStream::READ_MODE),
22+
'Unable to open file "php://input"'
23+
);
24+
25+
return new ResourceStream($resource);
26+
}
27+
28+
/**
29+
* @throws RuntimeException
30+
*
31+
* @return ResourceStream<resource>
32+
*/
33+
public static function output(): ResourceStream
34+
{
35+
$resource = SafeStreamAction::run(
36+
static fn () => fopen('php://output', FileStream::WRITE_MODE),
37+
'Unable to open file "php://output"'
38+
);
39+
40+
return new ResourceStream($resource);
41+
}
42+
}

tests/Unit/Factory/CliStreamTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Unit\Factory;
6+
7+
use Phpro\ResourceStream\Factory\CliStream;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(CliStream::class)]
13+
class CliStreamTest extends TestCase
14+
{
15+
#[Test]
16+
public function it_can_open_input_cli_stream(): void
17+
{
18+
$stream = CliStream::stdin();
19+
20+
self::assertTrue($stream->isOpen());
21+
self::assertStringContainsString('php://stdin', $stream->uri());
22+
}
23+
24+
#[Test]
25+
public function it_can_open_output_cli_stream(): void
26+
{
27+
$stream = CliStream::stdout();
28+
29+
self::assertTrue($stream->isOpen());
30+
self::assertStringContainsString('php://stdout', $stream->uri());
31+
}
32+
33+
#[Test]
34+
public function it_can_open_error_cli_stream(): void
35+
{
36+
$stream = CliStream::stderr();
37+
38+
self::assertTrue($stream->isOpen());
39+
self::assertStringContainsString('php://stderr', $stream->uri());
40+
}
41+
}

tests/Unit/Factory/IOStreamTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Unit\Factory;
6+
7+
use Phpro\ResourceStream\Factory\IOStream;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(IOStream::class)]
13+
class IOStreamTest extends TestCase
14+
{
15+
#[Test]
16+
public function it_can_open_input_io_stream(): void
17+
{
18+
$stream = IOStream::input();
19+
20+
self::assertTrue($stream->isOpen());
21+
self::assertStringContainsString('php://input', $stream->uri());
22+
}
23+
24+
#[Test]
25+
public function it_can_open_output_io_stream(): void
26+
{
27+
$stream = IOStream::output();
28+
29+
self::assertTrue($stream->isOpen());
30+
self::assertStringContainsString('php://output', $stream->uri());
31+
}
32+
}

0 commit comments

Comments
 (0)