Handle command not found/set workhorse

This commit is contained in:
Maarten 2020-02-27 20:06:12 +01:00
parent c5faeb0401
commit 57ec41dec1
3 changed files with 32 additions and 2 deletions

View file

@ -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); exit(0);
} }

View file

@ -36,6 +36,19 @@ class CommandsCollection
return self::$collection; 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 * @param null $name
* @return array|Command * @return array|Command

View file

@ -2,10 +2,12 @@
namespace Cli; namespace Cli;
use Cli\Assets\Console;
use Cli\Collection\CommandsCollection; use Cli\Collection\CommandsCollection;
class Factory class Factory
{ {
use Console;
/** /**
* @var array * @var array
@ -21,10 +23,21 @@ class Factory
$this->argv = $argv; $this->argv = $argv;
} }
/**
*
*/
public function handle() public function handle()
{ {
if(!isset($this->argv[1])) {
$this->terminate("Enter command to continue");
}
$command = $this->argv[1]; $command = $this->argv[1];
if(!CommandsCollection::exists($command)) {
$this->terminate(sprintf("Command '%s' does not exist", $command));
}
$executor = CommandsCollection::get($command); $executor = CommandsCollection::get($command);
$executor->handle(); $executor->handle();
} }