From a3d07a60843658b4df8c55061d8d8519222da651 Mon Sep 17 00:00:00 2001 From: ElvenSpellmaker Date: Sat, 23 Jan 2016 16:12:51 +0000 Subject: [PATCH] Implements the ability to run `x` times. * Implements the ability to run `x` times. * Implements the ability to keep running the scheduler as many times as you like. (Excellent for keeping track of long-running or infinite-running command chains.) * Implements and closes #14. --- examples/GrepExample.php | 1 + examples/LargeToSmallHeadExample.php | 1 + examples/SmallToLargeHeadExample.php | 1 + examples/YesHeadExample.php | 1 + examples/YesHeadYesExample.php | 56 ++++++++++++++++++++++++++++ examples/YesYesHeadExample.php | 1 + src/PipeSys/Scheduler.php | 35 ++++++++++++++++- tests/TestRig.php | 0 8 files changed, 94 insertions(+), 2 deletions(-) mode change 100644 => 100755 examples/GrepExample.php mode change 100644 => 100755 examples/LargeToSmallHeadExample.php mode change 100644 => 100755 examples/SmallToLargeHeadExample.php mode change 100644 => 100755 examples/YesHeadExample.php create mode 100755 examples/YesHeadYesExample.php mode change 100644 => 100755 examples/YesYesHeadExample.php mode change 100644 => 100755 tests/TestRig.php diff --git a/examples/GrepExample.php b/examples/GrepExample.php old mode 100644 new mode 100755 index 26210a4..1ff3702 --- a/examples/GrepExample.php +++ b/examples/GrepExample.php @@ -1,3 +1,4 @@ +#!/usr/bin/env php addCommand(new Command\Yes); +$c->addCommand(new Command\Head(1)); +$c->addCommand(new Command\Yes); + +$output = ''; + +if ($count = count($c->run(3)) !== 3) +{ + $output .= "Expecting 3 elements, received $count\n"; +} + +$expected = [ + 0 => 'ElvenSpellmaker\PipeSys\Command\Yes', + 2 => 'ElvenSpellmaker\PipeSys\Command\Yes', +]; +$received = array_map('get_class', $c->run(1)); + +if ($received !== $expected) +{ + echo '
';
+	var_dump($expected, $received);
+	exit;
+
+	$output .= "Expecting the head to be missing!\n";
+}
+
+$expected = [2 => 'ElvenSpellmaker\PipeSys\Command\Yes'];
+$received = array_map('get_class', $c->run(1));
+
+if ($received !== $expected)
+{
+	$output .= "Expecting the last yes to be present only!\n";
+}
+
+if (! count($c->run(5)))
+{
+	$output .= "Command has terminated sometime before 10 runs!\n";
+}
+
+echo $output;
diff --git a/examples/YesYesHeadExample.php b/examples/YesYesHeadExample.php
old mode 100644
new mode 100755
index 2cfab78..d5ff556
--- a/examples/YesYesHeadExample.php
+++ b/examples/YesYesHeadExample.php
@@ -1,3 +1,4 @@
+#!/usr/bin/env php
 connector->connect($this->commands);
+		if ($runs !== true && ! is_integer($runs))
+		{
+			$runs = (int)$runs;
+		}
+
+		if (! $this->started)
+		{
+			$this->started = true;
+			$this->connector->connect($this->commands);
+		}
 
 		while (count($this->commands))
 		{
+			if ($runs-- <= 0)
+			{
+				break;
+			}
+
 			foreach ($this->commands as $key => $command)
 			{
 				$result = $command->runOnce();
@@ -63,5 +82,17 @@ public function run()
 				}
 			}
 		}
+
+		return $this->commands;
+	}
+
+	/**
+	 * Has the scheduler already been run before?
+	 *
+	 * @return boolean
+	 */
+	public function isStarted()
+	{
+		return $this->started;
 	}
 }
diff --git a/tests/TestRig.php b/tests/TestRig.php
old mode 100644
new mode 100755