Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give command handlers the ability to save and restore their state (#208)
* Give command handlers the ability to save and restore their state before injecting input / output objects. Do not inject into objects that do not do this. * Looks like I may have removed some injection points that are actually necessary.
- Loading branch information
1 parent
4f7a58b
commit 30734aa
Showing
11 changed files
with
169 additions
and
76 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
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
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
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
<?php | ||
namespace Consolidation\AnnotatedCommand; | ||
|
||
interface State | ||
{ | ||
/** | ||
* Restore state to a previously cached value. | ||
*/ | ||
public function restore(); | ||
} |
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,12 @@ | ||
<?php | ||
namespace Consolidation\AnnotatedCommand\State; | ||
|
||
interface SavableState | ||
{ | ||
/** | ||
* Return the current state of this object. | ||
* | ||
* @return State | ||
*/ | ||
public function currentState(); | ||
} |
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,10 @@ | ||
<?php | ||
namespace Consolidation\AnnotatedCommand\State; | ||
|
||
interface State | ||
{ | ||
/** | ||
* Restore state to a previously cached value. | ||
*/ | ||
public function restore(); | ||
} |
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,77 @@ | ||
<?php | ||
namespace Consolidation\AnnotatedCommand\State; | ||
|
||
use Consolidation\AnnotatedCommand\Output\OutputAwareInterface; | ||
use Consolidation\AnnotatedCommand\State\SavableState; | ||
use Consolidation\AnnotatedCommand\State\State; | ||
use Symfony\Component\Console\Input\InputAwareInterface; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class StateHelper | ||
{ | ||
/** | ||
* Inject $input and $output into the command instance if it is set up to receive them. | ||
* | ||
* @param Callable|object $callback | ||
* @param OutputInterface $output | ||
* @return State | ||
*/ | ||
public static function injectIntoCallbackObject($callback, InputInterface $input, OutputInterface $output = null) | ||
{ | ||
return static::inject(static::recoverCallbackObject($callback), $input, $output); | ||
} | ||
|
||
/** | ||
* Inject $input and $output into the command instance if it is set up to receive them. | ||
* | ||
* @param Callable|object $callback | ||
* @param OutputInterface $output | ||
* @return State | ||
*/ | ||
public static function inject($target, InputInterface $input, OutputInterface $output = null) | ||
{ | ||
// Do not allow injection unless the target can save its state | ||
if (!$target || !($target instanceof SavableState)) { | ||
return new class implements State { | ||
public function restore() | ||
{ | ||
} | ||
}; | ||
} | ||
|
||
$state = $target->currentState(); | ||
|
||
if ($target instanceof InputAwareInterface) { | ||
$target->setInput($input); | ||
} | ||
if (isset($output) && $target instanceof OutputAwareInterface) { | ||
$target->setOutput($output); | ||
} | ||
|
||
return $state; | ||
} | ||
|
||
/** | ||
* If the command callback is a method of an object, return the object. | ||
* | ||
* @param Callable|object $callback | ||
* @return object|bool | ||
*/ | ||
protected static function recoverCallbackObject($callback) | ||
{ | ||
if (is_object($callback)) { | ||
return $callback; | ||
} | ||
|
||
if (!is_array($callback)) { | ||
return false; | ||
} | ||
|
||
if (!is_object($callback[0])) { | ||
return false; | ||
} | ||
|
||
return $callback[0]; | ||
} | ||
} |