Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stream readers and writers for NdJson input
- Loading branch information
1 parent
f307b4f
commit cc87eee
Showing
6 changed files
with
145 additions
and
5 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,21 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cucumber\Messages\Streams; | ||
|
||
use Cucumber\Messages\Envelope; | ||
use Generator; | ||
|
||
final class NdJsonStreamReader implements StreamReader | ||
{ | ||
use WithFileHandleTrait; | ||
|
||
/** | ||
* @return Generator<int, Envelope> | ||
*/ | ||
public function envelopes() : Generator | ||
{ | ||
while (!feof($this->fileHandle) && ($line = fgets($this->fileHandle))) { | ||
yield Envelope::fromJson($line); | ||
} | ||
} | ||
} |
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,20 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cucumber\Messages\Streams; | ||
|
||
use Cucumber\Messages\Envelope; | ||
|
||
final class NdJsonStreamWriter implements StreamWriter | ||
{ | ||
use WithFileHandleTrait; | ||
|
||
/** | ||
* @param iterable<Envelope> $envelopes | ||
*/ | ||
public function writeEnvelopes(iterable $envelopes) : void | ||
{ | ||
foreach($envelopes as $envelope) { | ||
fputs($this->fileHandle, $envelope->asJson() . "\n"); | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cucumber\Messages\Streams; | ||
|
||
use Cucumber\Messages\Envelope; | ||
|
||
interface StreamReader | ||
{ | ||
/** | ||
* @return iterable<Envelope> | ||
*/ | ||
public function envelopes(): iterable; | ||
} |
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,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cucumber\Messages\Streams; | ||
|
||
use Cucumber\Messages\Envelope; | ||
|
||
interface StreamWriter | ||
{ | ||
/** | ||
* @param iterable<Envelope> $envelopes | ||
*/ | ||
public function writeEnvelopes(iterable $envelopes): void; | ||
} |
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,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cucumber\Messages\Streams; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait WithFileHandleTrait | ||
{ | ||
/** | ||
* @param resource $fileHandle | ||
*/ | ||
private function __construct( | ||
private mixed $fileHandle, | ||
){} | ||
|
||
/** | ||
* @param resource $fileHandle | ||
*/ | ||
public static function fromFileHandle($fileHandle): self | ||
{ | ||
return new self($fileHandle); | ||
} | ||
} |
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