Skip to content

Commit

Permalink
Import argument types from PhpGt/Installer
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Sep 8, 2018
1 parent 6019a35 commit 71963f9
Show file tree
Hide file tree
Showing 13 changed files with 713 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Application.php
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);
}
}
22 changes: 22 additions & 0 deletions src/Argument/Argument.php
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;
}
}
158 changes: 158 additions & 0 deletions src/Argument/ArgumentList.php
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;
}
}
14 changes: 14 additions & 0 deletions src/Argument/ArgumentValueList.php
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];
}
}
12 changes: 12 additions & 0 deletions src/Argument/CommandArgument.php
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 "";
}
}
8 changes: 8 additions & 0 deletions src/Argument/LongOptionArgument.php
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);
}
}
12 changes: 12 additions & 0 deletions src/Argument/NamedArgument.php
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 "";
}
}
8 changes: 8 additions & 0 deletions src/Argument/ShortOptionArgument.php
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);
}
}
6 changes: 6 additions & 0 deletions src/CliException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Gt\Cli;

use RuntimeException;

class CliException extends RuntimeException {}
Loading

0 comments on commit 71963f9

Please sign in to comment.