Skip to content

Commit dfc2cab

Browse files
Merge pull request #24 from ElvenSpellmaker/fix/zero-read-bug
Fixes a bug whereby `0` is interpreted as falsy
2 parents 02feb75 + 361a516 commit dfc2cab

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/bin/sca/phpcs.txt
44
/bin/sca/phpmd.html
55
composer.lock
6+
/.vscode

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ language: php
33
php:
44
- '5.6'
55
- '7.0'
6+
- '7.1'
7+
- '7.2'
8+
- '7.3'
9+
- '7.4'
610
- hhvm
711
- nightly
8-
12+
913
before_install:
1014
- git config --global github.accesstoken $GITHUB_OAUTH_TOKEN
1115
- composer config github-oauth.github.com $GITHUB_OAUTH_TOKEN

bin/runtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
BASEDIR=$(dirname $0)
44

5-
php BASEDIR/../tests/TestRig.php
5+
php $BASEDIR/../tests/TestRig.php

examples/LoopExample.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* @title Shows handling of falsy data such as `0` in a loop.
5+
* @command yes 0 | grep 0 | head
6+
* @expected 0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n
7+
*/
8+
9+
error_reporting(E_ALL);
10+
11+
include __DIR__ . '/../vendor/autoload.php';
12+
13+
use ElvenSpellmaker\PipeSys as PS;
14+
use ElvenSpellmaker\PipeSys\Command as Command;
15+
use ElvenSpellmaker\PipeSys\IO as IO;
16+
17+
class LoopSystem extends Command\AbstractCommand
18+
{
19+
private $i = 5;
20+
21+
public function getCommand()
22+
{
23+
while($this->i--)
24+
{
25+
yield new IO\OutputIntent('0');
26+
echo (yield new IO\ReadIntent), "\n";
27+
}
28+
}
29+
}
30+
31+
$loopBuffer = new IO\QueueBuffer;
32+
33+
$start = new LoopSystem;
34+
$end = new LoopSystem;
35+
36+
$start->setStdIn($loopBuffer);
37+
$end->setStdOut($loopBuffer);
38+
39+
$c = new PS\Scheduler(new Command\StandardConnector);
40+
$c->addCommand($start);
41+
$c->addCommand($end);
42+
$c->run();

src/PipeSys/Command/AbstractCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ElvenSpellmaker\PipeSys\Command\CommandInterface;
66
use ElvenSpellmaker\PipeSys\IO\BufferInterface;
7+
use ElvenSpellmaker\PipeSys\IO\EOF;
78
use ElvenSpellmaker\PipeSys\IO\InvalidBufferException;
89
use ElvenSpellmaker\PipeSys\IO\IOableTrait;
910
use ElvenSpellmaker\PipeSys\IO\OutputIntent;
@@ -89,7 +90,7 @@ private function attemptRun($firstRun)
8990
$this->generator->next();
9091
}
9192

92-
$genResponse = $data
93+
$genResponse = $data !== null
9394
? $this->generator->send($data)
9495
: $this->generator->current();
9596

src/PipeSys/IO/IOableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function attemptRead()
9393
{
9494
$data = $this->read($this->readIntent->getChannel());
9595

96-
if ($data)
96+
if ($data !== null)
9797
{
9898
$this->readIntent = null;
9999
}

src/PipeSys/IO/QueueBuffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function read()
9898
}
9999
catch (RuntimeException $e)
100100
{
101-
return false;
101+
return null;
102102
}
103103

104104
if (isset($this->block))

0 commit comments

Comments
 (0)