| 1 |
<?php |
| 2 |
|
| 3 |
namespace Shellbox\Command; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base class for things that execute BoxedCommands |
| 7 |
*/ |
| 8 |
abstract class BoxedExecutor { |
| 9 |
/** @var Validator|null */ |
| 10 |
protected $validator; |
| 11 |
|
| 12 |
/** |
| 13 |
* Execute a boxed command. |
| 14 |
* |
| 15 |
* @param BoxedCommand $command |
| 16 |
* @return BoxedResult |
| 17 |
*/ |
| 18 |
final public function execute( BoxedCommand $command ) { |
| 19 |
$this->assertIsValid( $command ); |
| 20 |
return $this->executeValid( $command ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Execute a BoxedCommand that has already been validated. |
| 25 |
* |
| 26 |
* @param BoxedCommand $command |
| 27 |
* @return BoxedResult |
| 28 |
*/ |
| 29 |
abstract public function executeValid( BoxedCommand $command ); |
| 30 |
|
| 31 |
/** |
| 32 |
* Create an empty command linked to this executor |
| 33 |
* |
| 34 |
* @return BoxedCommand |
| 35 |
*/ |
| 36 |
public function createCommand() { |
| 37 |
return new BoxedCommand( $this ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Set validation configuration |
| 42 |
* |
| 43 |
* @param array $config |
| 44 |
*/ |
| 45 |
public function setValidationConfig( $config ) { |
| 46 |
$this->validator = new Validator( $config ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Validate the command. If it is not valid, throw an exception |
| 51 |
* |
| 52 |
* @param BoxedCommand $command |
| 53 |
* @throws ValidationError |
| 54 |
*/ |
| 55 |
protected function assertIsValid( BoxedCommand $command ) { |
| 56 |
if ( !$this->validator ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
$this->validator->validate( $command ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Whether the executor can download input files and upload output files |
| 64 |
* specified with BoxedCommand::inputFileFromUrl and the like. |
| 65 |
* |
| 66 |
* @since 4.1.0 |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
abstract public function areUrlFilesAllowed(); |
| 70 |
} |