Skip to content

Commit

Permalink
Add common PHP I/O stream factories
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jan 22, 2025
1 parent 65cb294 commit 8c70a9c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Factory/CliStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Phpro\ResourceStream\Factory;

use Phpro\ResourceStream\ErrorHandling\SafeStreamAction;
use Phpro\ResourceStream\Exception\RuntimeException;
use Phpro\ResourceStream\ResourceStream;

final class CliStream
{
/**
* @throws RuntimeException
*
* @return ResourceStream<resource>
*/
public static function stdout(): ResourceStream
{
$resource = SafeStreamAction::run(
static fn () => fopen('php://stdout', 'w'),
'Unable to open file "php://stdout"'
);

return new ResourceStream($resource);
}

/**
* @throws RuntimeException
*
* @return ResourceStream<resource>
*/
public static function stdin(): ResourceStream
{
$resource = SafeStreamAction::run(
static fn () => fopen('php://stdin', 'r'),
'Unable to open file "php://stdin"'
);

return new ResourceStream($resource);
}

/**
* @throws RuntimeException
*
* @return ResourceStream<resource>
*/
public static function stderr(): ResourceStream
{
$resource = SafeStreamAction::run(
static fn () => fopen('php://stderr', 'w'),
'Unable to open file "php://stderr"'
);

return new ResourceStream($resource);
}
}
42 changes: 42 additions & 0 deletions src/Factory/IOStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Phpro\ResourceStream\Factory;

use Phpro\ResourceStream\ErrorHandling\SafeStreamAction;
use Phpro\ResourceStream\Exception\RuntimeException;
use Phpro\ResourceStream\ResourceStream;

final class IOStream
{
/**
* @throws RuntimeException
*
* @return ResourceStream<resource>
*/
public static function input(): ResourceStream
{
$resource = SafeStreamAction::run(
static fn () => fopen('php://input', FileStream::READ_MODE),
'Unable to open file "php://input"'
);

return new ResourceStream($resource);
}

/**
* @throws RuntimeException
*
* @return ResourceStream<resource>
*/
public static function output(): ResourceStream
{
$resource = SafeStreamAction::run(
static fn () => fopen('php://output', FileStream::WRITE_MODE),
'Unable to open file "php://output"'
);

return new ResourceStream($resource);
}
}
41 changes: 41 additions & 0 deletions tests/Unit/Factory/CliStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Unit\Factory;

use Phpro\ResourceStream\Factory\CliStream;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(CliStream::class)]
class CliStreamTest extends TestCase
{
#[Test]
public function it_can_open_input_cli_stream(): void
{
$stream = CliStream::stdin();

self::assertTrue($stream->isOpen());
self::assertStringContainsString('php://stdin', $stream->uri());
}

#[Test]
public function it_can_open_output_cli_stream(): void
{
$stream = CliStream::stdout();

self::assertTrue($stream->isOpen());
self::assertStringContainsString('php://stdout', $stream->uri());
}

#[Test]
public function it_can_open_error_cli_stream(): void
{
$stream = CliStream::stderr();

self::assertTrue($stream->isOpen());
self::assertStringContainsString('php://stderr', $stream->uri());
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Factory/IOStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Unit\Factory;

use Phpro\ResourceStream\Factory\IOStream;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(IOStream::class)]
class IOStreamTest extends TestCase
{
#[Test]
public function it_can_open_input_io_stream(): void
{
$stream = IOStream::input();

self::assertTrue($stream->isOpen());
self::assertStringContainsString('php://input', $stream->uri());
}

#[Test]
public function it_can_open_output_io_stream(): void
{
$stream = IOStream::output();

self::assertTrue($stream->isOpen());
self::assertStringContainsString('php://output', $stream->uri());
}
}

0 comments on commit 8c70a9c

Please sign in to comment.