From dbe88b7f2b703831b4a8ee732925e396a364d07c Mon Sep 17 00:00:00 2001 From: Maarten Date: Tue, 7 Jan 2020 22:15:50 +0100 Subject: [PATCH] Update command factory. Update command collection --- app/Console/Commands/TestCommand.php | 21 +++++-- src/Cli/Collection/CommandsCollection.php | 56 +++++++++++++++++ src/Cli/Commands/Command.php | 76 +++++++++++++++++++++++ src/Cli/Commands/Commands.php | 37 ----------- src/Cli/Factory.php | 22 ++++++- src/Runtime/Contracts/Console/Command.php | 23 +++++++ src/Runtime/Helpers/helpers.php | 35 ++++++++++- 7 files changed, 225 insertions(+), 45 deletions(-) create mode 100644 src/Cli/Collection/CommandsCollection.php create mode 100644 src/Cli/Commands/Command.php delete mode 100644 src/Cli/Commands/Commands.php create mode 100644 src/Runtime/Contracts/Console/Command.php diff --git a/app/Console/Commands/TestCommand.php b/app/Console/Commands/TestCommand.php index 80ebedb..b9d07f9 100644 --- a/app/Console/Commands/TestCommand.php +++ b/app/Console/Commands/TestCommand.php @@ -2,12 +2,16 @@ namespace App\Console\Commands; -class TestCommand { +use Cli\Commands\Command; +use Runtime\Contracts\Console\Command as CommandInterface; + +class TestCommand extends Command implements CommandInterface +{ /** * @var string */ - public $signature = 'test'; + public $signature = 'test:command'; /** * @var string @@ -16,17 +20,26 @@ class TestCommand { /** * TestCommand constructor. + * + * @return void */ public function __construct() { - + parent::__construct(); } /** - * @return mixed; + * @return mixed */ public function handle() { + $name = $this->ask('What is your name? '); + + $this->printLn($this->argument('test')); + + $this->success('Succcess: Hello ' . $name); + $this->warning('Warning: Hello ' . $name); + $this->error('Error: Hello ' . $name); return true; } diff --git a/src/Cli/Collection/CommandsCollection.php b/src/Cli/Collection/CommandsCollection.php new file mode 100644 index 0000000..a85c045 --- /dev/null +++ b/src/Cli/Collection/CommandsCollection.php @@ -0,0 +1,56 @@ +signature] = $executor; + } + } + } + + return self::$collection; + } + + /** + * @param null $name + * @return array|Command + */ + public static function get($name = null) + { + if(self::$collection == null) { + self::collect(); + } + + if($name != null) { + return self::$collection[$name]; + } + + return self::$collection; + } + +} \ No newline at end of file diff --git a/src/Cli/Commands/Command.php b/src/Cli/Commands/Command.php new file mode 100644 index 0000000..c062b4e --- /dev/null +++ b/src/Cli/Commands/Command.php @@ -0,0 +1,76 @@ +args = $this->getArgs(); + } + + /** + * @return array + */ + private function getArgs() + { + global $argv; + + $args = []; + for ($i = 1; $i < count($argv); $i++) { + if (preg_match('/^--([^=]+)=(.*)/', $argv[$i], $match)) { + $args[$match[1]] = $match[2]; + } + } + + return $args; + } + + /** + * @param $name + * @return string + */ + public function argument($name) + { + return $this->args[$name]; + } + + /** + * @return string + */ + public function getName() + { + return $this->signature; + } + + /** + * @return string + */ + public function getDescription() + { + return $this->description; + } + +} \ No newline at end of file diff --git a/src/Cli/Commands/Commands.php b/src/Cli/Commands/Commands.php deleted file mode 100644 index db8ab1d..0000000 --- a/src/Cli/Commands/Commands.php +++ /dev/null @@ -1,37 +0,0 @@ -args = $args; - } - - /** - * Handle console command - */ - public function handle() - { - $name = $this->ask('What is your name? '); - - $this->success('Succcess: Hello ' . $name); - $this->warning('Warning: Hello ' . $name); - $this->error('Error: Hello ' . $name); - } - -} \ No newline at end of file diff --git a/src/Cli/Factory.php b/src/Cli/Factory.php index 60fe608..d025991 100644 --- a/src/Cli/Factory.php +++ b/src/Cli/Factory.php @@ -2,15 +2,31 @@ namespace Cli; -use Cli\Commands\Commands; +use Cli\Collection\CommandsCollection; class Factory { - public function handle() + /** + * @var array + */ + private $argv; + + /** + * Factory constructor. + */ + public function __construct() { global $argv; - app(Commands::class, [$argv])->handle(); + $this->argv = $argv; + } + + public function handle() + { + $command = $this->argv[1]; + + $executor = CommandsCollection::get($command); + $executor->handle(); } } \ No newline at end of file diff --git a/src/Runtime/Contracts/Console/Command.php b/src/Runtime/Contracts/Console/Command.php new file mode 100644 index 0000000..d1493dc --- /dev/null +++ b/src/Runtime/Contracts/Console/Command.php @@ -0,0 +1,23 @@ +