From f8a845527e6f57c46563c384d7703b1b4c2e1a01 Mon Sep 17 00:00:00 2001 From: Maarten Date: Tue, 24 Dec 2019 02:18:07 +0100 Subject: [PATCH] Add console features --- src/Cli/Assets/Colors.php | 2 +- src/Cli/Assets/Console.php | 70 +++++++++++++++++++++++++++++++++++ src/Cli/Commands/Commands.php | 7 ++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Cli/Assets/Console.php diff --git a/src/Cli/Assets/Colors.php b/src/Cli/Assets/Colors.php index 33f9074..163c5fc 100644 --- a/src/Cli/Assets/Colors.php +++ b/src/Cli/Assets/Colors.php @@ -17,7 +17,7 @@ class Colors { /** * Colors constructor. */ - public static function setColors() + private static function setColors() { self::$foreground_colors['black'] = '0;30'; self::$foreground_colors['dark_gray'] = '1;30'; diff --git a/src/Cli/Assets/Console.php b/src/Cli/Assets/Console.php new file mode 100644 index 0000000..606bec1 --- /dev/null +++ b/src/Cli/Assets/Console.php @@ -0,0 +1,70 @@ +print($message, $color, $background); + echo "\n"; + } + + /** + * @param string $message + */ + public function success(string $message) + { + $this->printLn($message, 'green'); + } + + /** + * @param $question + * @return string + */ + public function ask($question) + { + $this->success($question); + $this->print("\n > ", 'yellow'); + + return $this->getLine(); + } + + /** + * Terminate console + */ + public function terminate() + { + exit(0); + } + + /** + * @return string + */ + private function getLine() + { + if( $this->handle === false) { + $this->handle = fopen("php://stdin","r"); + } + + return trim(fgets($this->handle)); + } + +} \ No newline at end of file diff --git a/src/Cli/Commands/Commands.php b/src/Cli/Commands/Commands.php index 23ba810..aae8e9c 100644 --- a/src/Cli/Commands/Commands.php +++ b/src/Cli/Commands/Commands.php @@ -2,8 +2,11 @@ namespace Cli\Commands; +use Cli\Assets\Console; + class Commands { + use Console; /** * @var array @@ -21,7 +24,11 @@ class Commands public function handle() { + $name = $this->ask('What is your name? '); + $this->success('Hello ' . $name); + + $this->terminate(); } } \ No newline at end of file