155 lines
4.1 KiB
PHP
155 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* FuzeWorks CLIComponent
|
|
*
|
|
* The FuzeWorks PHP FrameWork
|
|
*
|
|
* Copyright (C) 2013-2021 i15
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @author i15
|
|
* @copyright Copyright (c) 2013 - 2021, i15. (https://i15.nl)
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*
|
|
* @since Version 1.2.0
|
|
*
|
|
* @version Version 1.2.0
|
|
*/
|
|
|
|
namespace FuzeWorks\CLI;
|
|
|
|
|
|
class Input
|
|
{
|
|
|
|
protected array $arguments = [];
|
|
|
|
public function init()
|
|
{
|
|
// Collect arguments
|
|
global $argv;
|
|
$this->arguments = $argv;
|
|
}
|
|
|
|
/**
|
|
* @param array $shortOpt
|
|
* @param array $longOpt
|
|
* @return false|false[]|string[]
|
|
*/
|
|
public function option(array $shortOpt = [], array $longOpt = [])
|
|
{
|
|
$shortOpt = implode('', $shortOpt);
|
|
return getopt($shortOpt, $longOpt);
|
|
}
|
|
|
|
public function arguments(): array
|
|
{
|
|
return $this->arguments;
|
|
}
|
|
|
|
public function line(): string
|
|
{
|
|
return trim(fgets(STDIN));
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* @todo Implement multiline input from pipe ('|')
|
|
*/
|
|
public function multiLine(): string
|
|
{
|
|
$input = '';
|
|
|
|
// While an input is coming, keep going until a break is passed
|
|
while ($line = $this->line())
|
|
{
|
|
// If the line does not end with '\', save the input and break the multiline
|
|
if (substr(rtrim($line), -2, 2) != ' \\')
|
|
{
|
|
$input .= $line;
|
|
break;
|
|
}
|
|
// If the line ends with '\', save the line and ask for the next one;
|
|
else
|
|
{
|
|
$input .= substr(rtrim($line), 0 , -1);
|
|
echo "\r" . ' > ';
|
|
}
|
|
}
|
|
|
|
// And finally output the input
|
|
return $input;
|
|
}
|
|
|
|
/**
|
|
* Asks the user a question to which the user responds. Input will be of type multiline
|
|
*
|
|
* @param string $question
|
|
* @param string $response_type
|
|
* @param bool $optional
|
|
* @return string
|
|
*/
|
|
public function dialog(string $question, string $response_type, bool $optional = false): string
|
|
{
|
|
// First ask the dialog question
|
|
echo $question . " > ";
|
|
|
|
// Retrieve the input using a multiline
|
|
$input = $this->multiLine();
|
|
|
|
// If the input is empty and not optional, ask again
|
|
if (empty($input) && !$optional)
|
|
{
|
|
echo "Input may is not optional!\n";
|
|
$input = $this->dialog($question, $response_type, $optional);
|
|
}
|
|
|
|
// Verify whether the input matches the requested type
|
|
// @todo
|
|
|
|
return $input;
|
|
}
|
|
|
|
/**
|
|
* Can be used for things like pipe. Might need some rework though...
|
|
* @todo Rework, implement safe_feof()
|
|
* @see https://www.php.net/manual/en/function.feof.php
|
|
*
|
|
* @return string
|
|
*/
|
|
public function stdin(): string
|
|
{
|
|
$lines = $this->stdinArray();
|
|
return implode("\n", $lines);
|
|
}
|
|
|
|
public function stdinArray(): array
|
|
{
|
|
$lines = [];
|
|
while(!feof(STDIN))
|
|
$lines[] = fgets(STDIN);
|
|
|
|
return $lines;
|
|
}
|
|
|
|
|
|
|
|
|
|
} |