-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a generic input to read from any stream
- Loading branch information
Showing
3 changed files
with
178 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace League\CLImate\Util\Reader; | ||
|
||
use League\CLImate\Exceptions\RuntimeException; | ||
use Seld\CliPrompt\CliPrompt; | ||
|
||
class Stream implements ReaderInterface | ||
{ | ||
/** | ||
* @var string $filename The name of the file this stream represents. | ||
*/ | ||
private $filename; | ||
|
||
/** | ||
* @var resource $resource The underlying stream this object represents. | ||
*/ | ||
private $resource; | ||
|
||
|
||
/** | ||
* Create a new instance. | ||
* | ||
* @param string $filename The name of the file this stream represents | ||
*/ | ||
public function __construct($filename) | ||
{ | ||
$this->filename = $filename; | ||
} | ||
|
||
|
||
/** | ||
* Read a line from the stream | ||
* | ||
* @return string | ||
*/ | ||
public function line() | ||
{ | ||
return trim(fgets($this->getResource(), 1024)); | ||
} | ||
|
||
|
||
/** | ||
* Read from the stream until EOF (^D) is reached | ||
* | ||
* @return string | ||
*/ | ||
public function multiLine() | ||
{ | ||
return trim(stream_get_contents($this->getResource())); | ||
} | ||
|
||
|
||
/** | ||
* Read one character | ||
* | ||
* @param int $count | ||
* | ||
* @return string | ||
*/ | ||
public function char($count = 1) | ||
{ | ||
return fread($this->getResource(), $count); | ||
} | ||
|
||
|
||
/** | ||
* Read the line, but hide what the user is typing | ||
* | ||
* @return string | ||
*/ | ||
public function hidden() | ||
{ | ||
return CliPrompt::hiddenPrompt(); | ||
} | ||
|
||
|
||
/** | ||
* Return a valid resource, even if it previously EOF'ed. | ||
* | ||
* @return resource | ||
*/ | ||
private function getResource() | ||
{ | ||
if ($this->resource && !feof($this->resource)) { | ||
return $this->resource; | ||
} | ||
|
||
if ($this->resource !== null) { | ||
fclose($this->resource); | ||
} | ||
|
||
$this->resource = fopen($this->filename, "r"); | ||
if (!$this->resource) { | ||
throw new RuntimeException("Unable to read from {$this->filename}"); | ||
} | ||
|
||
return $this->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,75 @@ | ||
<?php | ||
|
||
namespace League\CLImate\Tests\Util\Reader; | ||
|
||
use League\CLImate\Tests\TestBase; | ||
use League\CLImate\Util\Reader\Stream; | ||
|
||
class StreamTest extends TestBase | ||
{ | ||
private $filename; | ||
private $stream; | ||
private $file; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->filename = tempnam(sys_get_temp_dir(), "climate_"); | ||
|
||
$this->stream = new Stream($this->filename); | ||
$this->file = new \SplFileObject($this->filename, "w"); | ||
} | ||
|
||
|
||
public function tearDown(): void | ||
{ | ||
unset($this->file); | ||
unlink($this->filename); | ||
} | ||
|
||
|
||
public function testLine() | ||
{ | ||
$this->file->fwrite("Line A\n"); | ||
$this->file->fwrite("Line B\n"); | ||
|
||
$response = $this->stream->line(); | ||
$this->assertSame("Line A", $response); | ||
|
||
$response = $this->stream->line(); | ||
$this->assertSame("Line B", $response); | ||
} | ||
|
||
|
||
public function testMutliLine() | ||
{ | ||
$this->file->fwrite("Line one\n"); | ||
$this->file->fwrite("Line two\n"); | ||
|
||
$response = $this->stream->multiLine(); | ||
|
||
$this->assertSame("Line one\nLine two", $response); | ||
} | ||
|
||
|
||
public function testChar() | ||
{ | ||
$this->file->fwrite("123456789"); | ||
|
||
$response = $this->stream->char(6); | ||
|
||
$this->assertSame("123456", $response); | ||
} | ||
|
||
|
||
public function testReopenStream() | ||
{ | ||
$this->file->fwrite("Line 1\n"); | ||
$this->file->fwrite("Line 2\n"); | ||
|
||
$response = $this->stream->multiLine(); | ||
$this->assertSame("Line 1\nLine 2", $response); | ||
|
||
$response = $this->stream->line(); | ||
$this->assertSame("Line 1", $response); | ||
} | ||
} |