Skip to content

Commit

Permalink
Support saving and restoring state. (consolidation#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson authored Aug 30, 2020
1 parent 45842d0 commit cf58ea6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .scenarios.lock/symfony4/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"php": ">=7.1.3",
"consolidation/config": "^1.2.1|^2",
"consolidation/log": "^1.1.1|^2.0.1",
"consolidation/annotated-command": "^4.2",
"consolidation/annotated-command": "^4.2.1",
"consolidation/output-formatters": "^4.1.1",
"consolidation/self-update": "^1.2",
"league/container": "^2.4.1"
Expand Down
12 changes: 6 additions & 6 deletions .scenarios.lock/symfony4/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DEPENDENCY LICENSES:

Name Version License
consolidation/annotated-command 4.2.0 MIT
consolidation/annotated-command 4.2.1 MIT
consolidation/config 2.0.0 MIT
consolidation/log 2.0.1 MIT
consolidation/output-formatters 4.1.1 MIT
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"php": ">=7.1.3",
"consolidation/config": "^1.2.1|^2",
"consolidation/log": "^1.1.1|^2.0.1",
"consolidation/annotated-command": "^4.2",
"consolidation/annotated-command": "^4.2.1",
"consolidation/output-formatters": "^4.1.1",
"consolidation/self-update": "^1.2",
"league/container": "^2.4.1",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/Common/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Consolidation\AnnotatedCommand\State\SavableState;
use Consolidation\AnnotatedCommand\State\State;

trait IO
{
Expand All @@ -19,6 +21,39 @@ trait IO
*/
protected $io;

public function currentState()
{
return new class($this, $this->input, $this->output, $this->io) implements State {
protected $obj;
protected $input;
protected $output;
protected $io;

public function __construct($obj, $input, $output, $io)
{
$this->obj = $obj;
$this->input = $input;
$this->output = $output;
$this->io = $io;
}

public function restore()
{
$this->obj->restoreState($this->input, $this->output, $this->io);
}
};
}

// This should typically only be called by State::restore()
public function restoreState(InputInterface $input = null, OutputInterface $output = null, SymfonyStyle $io = null)
{
$this->setInput($input);
$this->setOutput($output);
$this->io = $io;

return $this;
}

public function setInput(InputInterface $input)
{
if ($input != $this->input) {
Expand Down
3 changes: 2 additions & 1 deletion src/Contract/IOAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
namespace Robo\Contract;

use Symfony\Component\Console\Input\InputAwareInterface;
use Consolidation\AnnotatedCommand\State\SavableState;

interface IOAwareInterface extends OutputAwareInterface, InputAwareInterface
interface IOAwareInterface extends OutputAwareInterface, InputAwareInterface, SavableState
{
}
2 changes: 1 addition & 1 deletion tests/integration/CommandTesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function setUp()
public function testInputApis()
{
list($tryInputOutput, $statusCode) = $this->executeCommand('try:input', ["I'm great!", "yes", "PHP", "1234"]);
$this->assertEquals(0, $statusCode);
$this->assertContains("I'm great!", $tryInputOutput);
$this->assertContains("PHP", $tryInputOutput);
$this->assertContains("1234", $tryInputOutput);
$this->assertEquals(0, $statusCode);
}
}
1 change: 1 addition & 0 deletions tests/integration/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function testCommandEventHook()

$expected = <<<EOT
This is the command-event hook for the test:command-event command.
This is the main method for the test:command-event command.
This is the post-command hook for the test:command-event command.
EOT;
Expand Down

0 comments on commit cf58ea6

Please sign in to comment.