-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |