From 57ec41dec1fb3af4d0eab66cac920bb4efb555bf Mon Sep 17 00:00:00 2001 From: Maarten Date: Thu, 27 Feb 2020 20:06:12 +0100 Subject: [PATCH] Handle command not found/set workhorse --- src/Cli/Assets/Console.php | 8 ++++++-- src/Cli/Collection/CommandsCollection.php | 13 +++++++++++++ src/Cli/Factory.php | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Cli/Assets/Console.php b/src/Cli/Assets/Console.php index 2597712..42d481c 100644 --- a/src/Cli/Assets/Console.php +++ b/src/Cli/Assets/Console.php @@ -69,10 +69,14 @@ trait Console } /** - * Terminate console + * @param string $message */ - public function terminate() + public function terminate($message = "") { + if($message != "") { + $this->error($message); + } + exit(0); } diff --git a/src/Cli/Collection/CommandsCollection.php b/src/Cli/Collection/CommandsCollection.php index a832373..4453836 100644 --- a/src/Cli/Collection/CommandsCollection.php +++ b/src/Cli/Collection/CommandsCollection.php @@ -36,6 +36,19 @@ class CommandsCollection return self::$collection; } + /** + * @param $name + * @return bool + */ + public static final function exists($name) + { + if(self::$collection == null) { + self::collect(); + } + + return isset(self::$collection[$name]); + } + /** * @param null $name * @return array|Command diff --git a/src/Cli/Factory.php b/src/Cli/Factory.php index d025991..cecbcf4 100644 --- a/src/Cli/Factory.php +++ b/src/Cli/Factory.php @@ -2,10 +2,12 @@ namespace Cli; +use Cli\Assets\Console; use Cli\Collection\CommandsCollection; class Factory { + use Console; /** * @var array @@ -21,10 +23,21 @@ class Factory $this->argv = $argv; } + /** + * + */ public function handle() { + if(!isset($this->argv[1])) { + $this->terminate("Enter command to continue"); + } + $command = $this->argv[1]; + if(!CommandsCollection::exists($command)) { + $this->terminate(sprintf("Command '%s' does not exist", $command)); + } + $executor = CommandsCollection::get($command); $executor->handle(); }