-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: STDIN Extractor / STDOUT Loader (#35)
- Loading branch information
Showing
22 changed files
with
373 additions
and
30 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
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
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Extractor; | ||
|
||
use BenTools\ETL\EtlState; | ||
use Iterator; | ||
use SplFileObject; | ||
|
||
/** | ||
* @implements Iterator<int, string> | ||
*/ | ||
final class STDINExtractor implements Iterator, ExtractorInterface | ||
{ | ||
private SplFileObject $stdIn; | ||
|
||
public function current(): string|false | ||
{ | ||
return $this->stdIn->current(); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
$this->stdIn->next(); | ||
} | ||
|
||
public function key(): int | ||
{ | ||
return $this->stdIn->key(); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return $this->stdIn->valid(); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->stdIn = new SplFileObject('php://stdin'); | ||
$this->stdIn->setFlags(SplFileObject::DROP_NEW_LINE); | ||
} | ||
|
||
public function extract(EtlState $state): iterable | ||
{ | ||
yield from $this; | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Loader; | ||
|
||
use BenTools\ETL\EtlState; | ||
use BenTools\ETL\Exception\LoadException; | ||
|
||
use function fclose; | ||
use function fopen; | ||
use function fwrite; | ||
use function get_debug_type; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
use const PHP_EOL; | ||
|
||
final readonly class STDOUTLoader implements LoaderInterface | ||
{ | ||
public function __construct( | ||
private string $eol = PHP_EOL, | ||
) { | ||
} | ||
|
||
public function load(mixed $item, EtlState $state): void | ||
{ | ||
if (!is_string($item)) { | ||
throw new LoadException(sprintf('Expected string, got %s.', get_debug_type($item))); | ||
} | ||
|
||
$state->context[__CLASS__]['pending'][] = $item; | ||
} | ||
|
||
public function flush(bool $isPartial, EtlState $state): int | ||
{ | ||
$pendingItems = $state->context[__CLASS__]['pending'] ?? []; | ||
$state->context[__CLASS__]['resource'] ??= fopen('php://stdout', 'wb+'); | ||
$state->context[__CLASS__]['nbWrittenBytes'] ??= 0; | ||
foreach ($pendingItems as $item) { | ||
$state->context[__CLASS__]['nbWrittenBytes'] += fwrite( | ||
$state->context[__CLASS__]['resource'], | ||
$item.$this->eol, | ||
); | ||
} | ||
|
||
$nbWrittenBytes = $state->context[__CLASS__]['nbWrittenBytes']; | ||
if (!$isPartial) { | ||
// fclose($state->context[__CLASS__]['resource']); | ||
unset($state->context[__CLASS__]); | ||
} | ||
|
||
return $nbWrittenBytes; | ||
} | ||
} |
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
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
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Tests\Stubs; | ||
|
||
use function file_exists; | ||
use function file_put_contents; | ||
use function min; | ||
use function stream_wrapper_register; | ||
use function stream_wrapper_restore; | ||
use function stream_wrapper_unregister; | ||
use function strlen; | ||
use function substr; | ||
|
||
/** | ||
* Inspired by @KEINOS. | ||
* | ||
* @see https://github.com/KEINOS/Practice_PHPUnit-test-of-STDIN | ||
*/ | ||
final class STDINStub | ||
{ | ||
private string $bufferFilename; | ||
private int $index; | ||
private int $length; | ||
private string $data = ''; | ||
public mixed $context; | ||
|
||
public function __construct() | ||
{ | ||
$this->bufferFilename = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_input.txt'; | ||
$this->index = 0; | ||
if (file_exists($this->bufferFilename)) { | ||
$this->data = file_get_contents($this->bufferFilename); | ||
} | ||
$this->length = strlen($this->data); | ||
} | ||
|
||
public function stream_open(): true | ||
{ | ||
return true; | ||
} | ||
|
||
public function url_stat(): false | ||
{ | ||
return false; | ||
} | ||
|
||
public function stream_close(): void | ||
{ | ||
} | ||
|
||
public function stream_stat(): false | ||
{ | ||
return false; | ||
} | ||
|
||
public function stream_flush(): true | ||
{ | ||
return true; | ||
} | ||
|
||
public function stream_read(int $count): string | ||
{ | ||
$length = min($count, $this->length - $this->index); | ||
$data = substr($this->data, $this->index); | ||
$this->index += $length; | ||
|
||
return $data; | ||
} | ||
|
||
public function stream_eof(): bool | ||
{ | ||
return $this->index >= $this->length; | ||
} | ||
|
||
public function stream_write(string $data): false|int | ||
{ | ||
return file_put_contents($this->bufferFilename, $data); | ||
} | ||
|
||
public static function emulate(string $stdInContent, callable $beforeRestore): mixed | ||
{ | ||
stream_wrapper_unregister('php'); | ||
stream_wrapper_register('php', __CLASS__); | ||
file_put_contents('php://stdin', $stdInContent); | ||
$result = $beforeRestore(); | ||
stream_wrapper_restore('php'); | ||
|
||
return $result; | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Tests\Stubs; | ||
|
||
use function fopen; | ||
use function stream_bucket_append; | ||
use function stream_bucket_make_writeable; | ||
use function stream_filter_append; | ||
use function stream_filter_register; | ||
|
||
final class STDOUTStub | ||
{ | ||
public string $filtername = 'intercept'; | ||
public ?array $params = null; // @phpstan-ignore-line | ||
private static string $storage = ''; | ||
|
||
// @phpstan-ignore-next-line | ||
public function filter($in, $out, &$consumed, bool $closing): int | ||
{ | ||
while ($bucket = stream_bucket_make_writeable($in)) { | ||
self::$storage .= $bucket->data; | ||
$consumed += $bucket->datalen; | ||
stream_bucket_append($out, $bucket); | ||
} | ||
|
||
return PSFS_PASS_ON; | ||
} | ||
|
||
public static function read(): string | ||
{ | ||
return self::$storage; | ||
} | ||
|
||
public static function emulate(callable $beforeRestore, string $filename = 'php://stdout'): string | ||
{ | ||
stream_filter_register('intercept', __CLASS__); | ||
$stdout = fopen($filename, 'wb+'); | ||
$filter = stream_filter_append($stdout, 'intercept'); | ||
$beforeRestore($stdout); | ||
$result = self::$storage; | ||
|
||
self::$storage = ''; | ||
|
||
return $result; | ||
} | ||
} |
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
Oops, something went wrong.