Skip to content

Commit 70c41d7

Browse files
Merge pull request #16 from ElvenSpellmaker/feature/run-x-times
Implements the ability to run `x` times.
2 parents 96e1004 + a3d07a6 commit 70c41d7

File tree

8 files changed

+94
-2
lines changed

8 files changed

+94
-2
lines changed

examples/GrepExample.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* @title Basic example to show grepping.

examples/LargeToSmallHeadExample.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* @title Example to show large head into smaller head.

examples/SmallToLargeHeadExample.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* @title Example to show small head into larger head.

examples/YesHeadExample.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* @title Basic example to show that `head` terminates `yes`.

examples/YesHeadYesExample.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* @title Tests two infintie commands sandwiched by a terminating command.
5+
* @command yes | head | yes
6+
* @expected y\ny\ny\ny\ny\ny\ny\ny\ny\ny\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+
16+
$c = new PS\Scheduler(new Command\StandardConnector);
17+
$c->addCommand(new Command\Yes);
18+
$c->addCommand(new Command\Head(1));
19+
$c->addCommand(new Command\Yes);
20+
21+
$output = '';
22+
23+
if ($count = count($c->run(3)) !== 3)
24+
{
25+
$output .= "Expecting 3 elements, received $count\n";
26+
}
27+
28+
$expected = [
29+
0 => 'ElvenSpellmaker\PipeSys\Command\Yes',
30+
2 => 'ElvenSpellmaker\PipeSys\Command\Yes',
31+
];
32+
$received = array_map('get_class', $c->run(1));
33+
34+
if ($received !== $expected)
35+
{
36+
echo '<pre>';
37+
var_dump($expected, $received);
38+
exit;
39+
40+
$output .= "Expecting the head to be missing!\n";
41+
}
42+
43+
$expected = [2 => 'ElvenSpellmaker\PipeSys\Command\Yes'];
44+
$received = array_map('get_class', $c->run(1));
45+
46+
if ($received !== $expected)
47+
{
48+
$output .= "Expecting the last yes to be present only!\n";
49+
}
50+
51+
if (! count($c->run(5)))
52+
{
53+
$output .= "Command has terminated sometime before 10 runs!\n";
54+
}
55+
56+
echo $output;

examples/YesYesHeadExample.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* @title Tests two infinite commands and a terminating command.

src/PipeSys/Scheduler.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Scheduler
2121
*/
2222
private $connector;
2323

24+
/**
25+
* @var boolean
26+
*/
27+
private $started;
28+
2429
/**
2530
* @param ConnectorInterface $connector The connector to connect commands
2631
* together.
@@ -47,12 +52,26 @@ public function addCommand(AbstractCommand $command)
4752
/**
4853
* Runs all the commands added to the scheduler.
4954
*/
50-
public function run()
55+
public function run($runs = true)
5156
{
52-
$this->connector->connect($this->commands);
57+
if ($runs !== true && ! is_integer($runs))
58+
{
59+
$runs = (int)$runs;
60+
}
61+
62+
if (! $this->started)
63+
{
64+
$this->started = true;
65+
$this->connector->connect($this->commands);
66+
}
5367

5468
while (count($this->commands))
5569
{
70+
if ($runs-- <= 0)
71+
{
72+
break;
73+
}
74+
5675
foreach ($this->commands as $key => $command)
5776
{
5877
$result = $command->runOnce();
@@ -63,5 +82,17 @@ public function run()
6382
}
6483
}
6584
}
85+
86+
return $this->commands;
87+
}
88+
89+
/**
90+
* Has the scheduler already been run before?
91+
*
92+
* @return boolean
93+
*/
94+
public function isStarted()
95+
{
96+
return $this->started;
6697
}
6798
}

tests/TestRig.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)