-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import argument types from PhpGt/Installer
- Loading branch information
Showing
13 changed files
with
713 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
namespace Gt\Cli; | ||
|
||
use Gt\Installer\Argument\ArgumentList; | ||
|
||
class Application { | ||
protected $applicationName; | ||
protected $arguments; | ||
protected $commands; | ||
protected $stream; | ||
|
||
public function __construct( | ||
string $applicationName, | ||
ArgumentList $arguments, | ||
Command...$commands | ||
) { | ||
$this->applicationName = $applicationName; | ||
$this->arguments = $arguments; | ||
$this->commands = $commands; | ||
|
||
$this->commands []= new HelpCommand( | ||
$this->applicationName, | ||
$this->commands | ||
); | ||
|
||
$this->streams = new Stream( | ||
"php://stdin", | ||
"php://stdout", | ||
"php://stderr" | ||
); | ||
} | ||
|
||
public function setStreams($in, $out, $error) { | ||
$this->streams->setStreams($in, $out, $error); | ||
} | ||
|
||
public function run():void { | ||
$commandName = $this->arguments->getCommandName(); | ||
$command = $this->findCommandByName($commandName); | ||
$command->setStream($this->stream); | ||
$argumentValueList = $command->getArgumentValueList($this->arguments); | ||
|
||
try { | ||
$command->checkArguments($this->arguments); | ||
} | ||
catch(NotEnoughArgumentsException $exception) { | ||
$this->streams->writeLine( | ||
"Not enough arguments passed.", | ||
Stream::ERROR | ||
); | ||
$this->streams->writeLine( | ||
$command->getUsage(), | ||
Stream::ERROR | ||
); | ||
} | ||
catch(MissingRequiredParameterException $exception) { | ||
|
||
} | ||
catch(MissingRequiredParameterValueException $exception) { | ||
|
||
} | ||
|
||
$command->run($argumentValueList); | ||
} | ||
|
||
protected function findCommandByName(string $name):Command { | ||
foreach($this->commands as $command) { | ||
if($command->getName() !== $name) { | ||
continue; | ||
} | ||
|
||
return $command; | ||
} | ||
|
||
throw new InvalidCommandException($name); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
namespace Gt\Cli\Argument; | ||
|
||
abstract class Argument { | ||
protected $key; | ||
protected $value; | ||
|
||
public function __construct(string $rawKey, string $value = null) { | ||
$this->key = $this->processRawKey($rawKey); | ||
$this->value = $value; | ||
} | ||
|
||
abstract protected function processRawKey(string $rawKey):string; | ||
|
||
public function getKey():string { | ||
return $this->key; | ||
} | ||
|
||
public function getValue():?string { | ||
return $this->value; | ||
} | ||
} |
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,158 @@ | ||
<?php | ||
namespace Gt\Installer\Argument; | ||
|
||
use Iterator; | ||
|
||
class ArgumentList implements Iterator { | ||
const DEFAULT_COMMAND = "help"; | ||
|
||
protected $script; | ||
/** @var Argument[] */ | ||
protected $argumentList = []; | ||
protected $iteratorIndex; | ||
|
||
public function __construct(string $script, string...$arguments) { | ||
$this->script = $script; | ||
$this->buildArgumentList($arguments); | ||
} | ||
|
||
public function getCommandName():string { | ||
return $this->argumentList[0]->getValue(); | ||
} | ||
|
||
protected function buildArgumentList(array $arguments):void { | ||
$commandArgument = array_shift($arguments); | ||
$this->argumentList []= new CommandArgument( | ||
$commandArgument ?? self::DEFAULT_COMMAND | ||
); | ||
|
||
$skipNextArgument = false; | ||
|
||
foreach ($arguments as $i => $arg) { | ||
if($skipNextArgument) { | ||
$skipNextArgument = false; | ||
continue; | ||
} | ||
|
||
if ($arg[0] === "-") { | ||
if(strstr($arg, "=")) { | ||
$name = substr( | ||
$arg, | ||
0, | ||
strpos( | ||
$arg, | ||
"=" | ||
) | ||
); | ||
|
||
$value = substr( | ||
$arg, | ||
strpos( | ||
$arg, | ||
"=" | ||
) + 1 | ||
); | ||
} | ||
else { | ||
$skipNextArgument = true; | ||
$name = $arg; | ||
$value = $arguments[$i + 1]; | ||
} | ||
|
||
if ($arg[1] === "-") { | ||
$this->argumentList []= new LongOptionArgument( | ||
$name, | ||
$value | ||
); | ||
} | ||
else { | ||
$this->argumentList []= new ShortOptionArgument( | ||
$arg, | ||
$value | ||
); | ||
} | ||
} else { | ||
$this->argumentList []= new NamedArgument($arg); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @link http://php.net/manual/en/iterator.current.php | ||
*/ | ||
public function current():Argument { | ||
return $this->argumentList[$this->iteratorIndex]; | ||
} | ||
|
||
/** | ||
* @link http://php.net/manual/en/iterator.next.php | ||
*/ | ||
public function next():void { | ||
$this->iteratorIndex++; | ||
} | ||
|
||
/** | ||
* @link http://php.net/manual/en/iterator.key.php | ||
*/ | ||
public function key():int { | ||
return $this->iteratorIndex; | ||
} | ||
|
||
/** | ||
* @link http://php.net/manual/en/iterator.valid.php | ||
*/ | ||
public function valid():bool { | ||
return isset($this->argumentList[$this->iteratorIndex]); | ||
} | ||
|
||
/** | ||
* @link http://php.net/manual/en/iterator.rewind.php | ||
*/ | ||
public function rewind() { | ||
$this->iteratorIndex = 0; | ||
} | ||
|
||
public function contains(Parameter $parameter):bool { | ||
$longOption = $parameter->getLongOption(); | ||
$shortOption = $parameter->getShortOption(); | ||
|
||
foreach($this->argumentList as $argument) { | ||
$key = $argument->getKey(); | ||
|
||
if($argument instanceof LongOptionArgument) { | ||
if($key === $longOption) { | ||
return true; | ||
} | ||
} | ||
elseif($argument instanceof ShortOptionArgument) { | ||
if($key === $shortOption) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getValueForParameter(Parameter $parameter):?string { | ||
$longOption = $parameter->getLongOption(); | ||
$shortOption = $parameter->getShortOption(); | ||
|
||
foreach($this->argumentList as $argument) { | ||
$key = $argument->getKey(); | ||
|
||
if($argument instanceof LongOptionArgument) { | ||
if($key === $longOption) { | ||
return $argument->getValue(); | ||
} | ||
} | ||
elseif($argument instanceof ShortOptionArgument) { | ||
if($key === $shortOption) { | ||
return $argument->getValue(); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
namespace Gt\Cli\Argument; | ||
|
||
class ArgumentValueList { | ||
protected $valueMap = []; | ||
|
||
public function set(string $key, string $value):void { | ||
$this->valueMap[$key] = $value; | ||
} | ||
|
||
public function get(string $key):string { | ||
return $this->valueMap[$key]; | ||
} | ||
} |
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 Gt\Cli\Argument; | ||
|
||
class CommandArgument extends Argument { | ||
public function __construct(string $commandName) { | ||
parent::__construct("", $commandName); | ||
} | ||
|
||
protected function processRawKey(string $rawKey): string { | ||
return ""; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
namespace Gt\Cli\Argument; | ||
|
||
class LongOptionArgument extends Argument { | ||
protected function processRawKey(string $rawKey):string { | ||
return substr($rawKey, 2); | ||
} | ||
} |
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 Gt\Cli\Argument; | ||
|
||
class NamedArgument extends Argument { | ||
public function __construct($name) { | ||
parent::__construct("", $name); | ||
} | ||
|
||
protected function processRawKey(string $rawKey): string { | ||
return ""; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
namespace Gt\Cli\Argument; | ||
|
||
class ShortOptionArgument extends Argument { | ||
protected function processRawKey(string $rawKey):string { | ||
return substr($rawKey, 1); | ||
} | ||
} |
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,6 @@ | ||
<?php | ||
namespace Gt\Cli; | ||
|
||
use RuntimeException; | ||
|
||
class CliException extends RuntimeException {} |
Oops, something went wrong.