Skip to content

Commit

Permalink
Fixing Coding Standards
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Feb 8, 2023
1 parent d264520 commit 8daf01b
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 199 deletions.
13 changes: 8 additions & 5 deletions example/OpenFileCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use webfiori\cli\CLICommand;

class OpenFileCommand extends CLICommand {
Expand All @@ -13,24 +14,26 @@ public function __construct() {

public function exec(): int {
$path = $this->getArgValue('path');

if ($path === null) {
$path = $this->getInput('Give me file path:');
}

if (!file_exists($path)) {
$this->error('File not found: '.$path);

return -1;
}
$resource = fopen($path, 'r');
$ch = '';
while($ch !== false) {

while ($ch !== false) {
$ch = fgetc($resource);
$this->prints($ch);
}

fclose($resource);

return 1;
}

}
142 changes: 73 additions & 69 deletions webfiori/cli/CLICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public function addArgs(array $arr) {
$this->commandArgs = [];

foreach ($arr as $optionName => $options) {

if ($options instanceof CommandArgument) {
$this->addArgument($options);
} else {
Expand Down Expand Up @@ -761,6 +760,34 @@ public function prints(string $str, ...$_) {
public function read(int $bytes = 1) : string {
return $this->getInputStream()->read($bytes);
}
/**
* Reads and validates class name.
*
* @param string|null $suffix An optional string to append to class name.
*
* @param string $prompt The text that will be shown to the user as prompt for
* class name.
*
* @param string $errMsg A string to show in case provided class name is
* not valid.
*
* @return string A string that represents a valid class name. If suffix is
* not null, the method will return the name with the suffix included.
*/
public function readClassName(string $prompt, string $suffix = null, string $errMsg = 'Invalid class name is given.') {
return $this->getInput($prompt, null, new InputValidator(function (&$className, $suffix)
{
if ($suffix !== null) {
$subSuffix = substr($className, strlen($className) - strlen($suffix));

if ($subSuffix != $suffix) {
$className .= $suffix;
}
}

return InputValidator::isValidClassName($className);
}, $errMsg, [$suffix]));
}

/**
* Reads a value as float.
Expand All @@ -780,24 +807,6 @@ public function readFloat(string $prompt, float $default = null) : float {
return InputValidator::isFloat($val);
}, 'Provided value is not a floating number!'));
}
/**
* Reads a value as an integer.
*
* @param string $prompt The string that will be shown to the user. The
* string must be non-empty.
*
* @param int $default An optional default value to use in case the user
* hit "Enter" without entering any value. If null is passed, no default
* value will be set.
*
* @return int
*/
public function readInteger(string $prompt, int $default = null) : int {
return $this->getInput($prompt, $default, new InputValidator(function ($val)
{
return InputValidator::isInt($val);
}, 'Provided value is not an integer!'));
}
/**
* Reads the namespace of class and return an instance of it.
*
Expand All @@ -810,43 +819,50 @@ public function readInteger(string $prompt, int $default = null) : int {
* @return object The method will return an instance of the class.
*/
public function readInstance(string $prompt, string $errMsg = 'Invalid Class!', $constructorArgs = []) {
$clazzNs = $this->getInput($prompt, null, new InputValidator(function ($input) {
$clazzNs = $this->getInput($prompt, null, new InputValidator(function ($input)
{
if (InputValidator::isClass($input)) {
return true;
}

return false;
}, $errMsg));

$reflection = new \ReflectionClass($clazzNs);

return $reflection->newInstanceArgs($constructorArgs);
}
/**
* Reads and validates class name.
* Reads a value as an integer.
*
* @param string|null $suffix An optional string to append to class name.
* @param string $prompt The string that will be shown to the user. The
* string must be non-empty.
*
* @param string $prompt The text that will be shown to the user as prompt for
* class name.
* @param int $default An optional default value to use in case the user
* hit "Enter" without entering any value. If null is passed, no default
* value will be set.
*
* @param string $errMsg A string to show in case provided class name is
* not valid.
* @return int
*/
public function readInteger(string $prompt, int $default = null) : int {
return $this->getInput($prompt, $default, new InputValidator(function ($val)
{
return InputValidator::isInt($val);
}, 'Provided value is not an integer!'));
}
/**
* Reads one line from input stream.
*
* @return string A string that represents a valid class name. If suffix is
* not null, the method will return the name with the suffix included.
* The method will continue to read from input stream till it finds end of
* line character "\n".
*
* @return string The method will return the string which was taken from
* input stream without the end of line character.
*
* @since 1.0
*/
public function readClassName(string $prompt, string $suffix = null, string $errMsg = 'Invalid class name is given.') {
return $this->getInput($prompt, null, new InputValidator(function (&$className, $suffix) {
if ($suffix !== null) {
$subSuffix = substr($className, strlen($className) - strlen($suffix));

if ($subSuffix != $suffix) {
$className .= $suffix;
}
}

return InputValidator::isValidClassName($className);
}, $errMsg, [$suffix]));
public function readln() : string {
return $this->getInputStream()->readLine();
}

/**
Expand All @@ -870,28 +886,15 @@ public function readNamespace(string $prompt, string $defaultNs = null, string $
if ($defaultNs !== null && !InputValidator::isValidNamespace($defaultNs)) {
throw new IOException('Provided default namespace is not valid.');
}
return $this->getInput($prompt, $defaultNs, new InputValidator(function ($input) {


return $this->getInput($prompt, $defaultNs, new InputValidator(function ($input)
{
if (InputValidator::isValidNamespace($input)) {
return true;
}

return false;
}, $errMsg));

}
/**
* Reads one line from input stream.
*
* The method will continue to read from input stream till it finds end of
* line character "\n".
*
* @return string The method will return the string which was taken from
* input stream without the end of line character.
*
* @since 1.0
*/
public function readln() : string {
return $this->getInputStream()->readLine();
}
/**
* Removes an argument from the command given its name.
Expand Down Expand Up @@ -1045,16 +1048,6 @@ public function setName(string $name) : bool {

return false;
}
/**
* Sets the runner that owns the command.
*
* The runner is the instance that will execute the command.
*
* @param Runner $owner
*/
public function setOwner(Runner $owner = null) {
$this->owner = $owner;
}
/**
* Sets the stream at which the command will send output to.
*
Expand All @@ -1065,6 +1058,16 @@ public function setOwner(Runner $owner = null) {
public function setOutputStream(OutputStream $stream) {
$this->outputStream = $stream;
}
/**
* Sets the runner that owns the command.
*
* The runner is the instance that will execute the command.
*
* @param Runner $owner
*/
public function setOwner(Runner $owner = null) {
$this->owner = $owner;
}
/**
* Display a message that represents a success status.
*
Expand Down Expand Up @@ -1136,7 +1139,7 @@ private function _checkSelectedChoice($choices, $defaultIndex, $input) {
//Selected option is an index. Search for it and return its value.
$retVal = $this->_getChoiceAtIndex($choices, $input);
}

if ($retVal === null) {
$this->error('Invalid answer.');
}
Expand Down Expand Up @@ -1252,6 +1255,7 @@ private function getInputHelper(&$input, InputValidator $validator = null, strin
}
}
$retVal['value'] = $input;

return $retVal;
}
private function printMsg(string $msg, string $prefix, string $color) {
Expand Down
3 changes: 2 additions & 1 deletion webfiori/cli/CommandArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ public static function create(string $name, array $options) {
*/
public static function extractValue(string $argName, Runner $runner = null) {
$trimmedOptName = trim($argName);

if ($runner !== null) {
$argsV = $runner->getArgsVector();
} else {
$argsV = $_SERVER['argv'];
}

foreach ($argsV as $option) {
$optionClean = filter_var($option, FILTER_DEFAULT);
$optExpl = explode('=', $optionClean);
Expand Down
Loading

0 comments on commit 8daf01b

Please sign in to comment.