From 19aa25df1b1994156dd507afec1b956f70fa5411 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Sun, 9 Jul 2023 19:11:19 -0400 Subject: [PATCH] - First commit --- LICENSE | 21 ++++++++++ composer.json | 18 +++++++++ src/CliMiddleware.php | 91 +++++++++++++++++++++++++++++++++++++++++++ src/CliRequest.php | 59 ++++++++++++++++++++++++++++ src/Command.php | 20 ++++++++++ src/CommandStack.php | 34 ++++++++++++++++ src/Option.php | 21 ++++++++++ 7 files changed, 264 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/CliMiddleware.php create mode 100644 src/CliRequest.php create mode 100644 src/Command.php create mode 100644 src/CommandStack.php create mode 100644 src/Option.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cb3012e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2019 Dave Mc Nicoll + +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. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9dc23ff --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "mcnd/cli", + "description": "A middleware which captures CLI commands and executes them based on a preset definition.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "info@mcnd.ca" + } + ], + "require": {}, + "autoload": { + "psr-4": { + "Mcnd\\CLI\\": "src/" + } + } +} diff --git a/src/CliMiddleware.php b/src/CliMiddleware.php new file mode 100644 index 0000000..6aa3533 --- /dev/null +++ b/src/CliMiddleware.php @@ -0,0 +1,91 @@ +defaultResponse = $defaultResponseInterface; + $this->container = $container; + $this->fetcher = $fetcher; + $this->commands = $stack ?? new CommandStack(); + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface + { + if (php_sapi_name() === 'cli' OR defined('STDIN')) { + if ($this->fetcher) { + $this->getCommandsFromAttributes(); + } + + $cliRequest = new CliRequest($request); + + # $cliRequest->setOptions($commands->getOptions()); + + if ( $command = $this->commands->matchCommand($cliRequest->command, $cliRequest->options) ) { + if ($command->callback) { + if (is_string($command->callback)) { + list($class, $method) = explode('::', $command->callback); + + return $this->container->make($class)->{$method}($request->withAttribute('cli.command', $command)->withAttribute('cli.request', $cliRequest), []); + } + } + else { + return $this->printCommand($command); + } + } + + + return call_user_func($this->defaultResponse); + } + + return $handler->handle($request); + } + + public function getCommandsFromAttributes() : self + { + $this->commands->merge($this->fetcher->compile()); + + return $this; + } + + private function printCommand(Command $command) : ResponseInterface + { + $text = [ + $command->description, "", + "Usage:", + " command [options] [arguments]", "", + "Options:", + " OPTIONS HERE", "", + "Available commands:", + " COMMANDS HERE", "", + ]; + + return new TextResponse( implode(PHP_EOL, $text) ); + } +} + diff --git a/src/CliRequest.php b/src/CliRequest.php new file mode 100644 index 0000000..5528658 --- /dev/null +++ b/src/CliRequest.php @@ -0,0 +1,59 @@ +parseCommandLine(); + } + + public function matchOptions(string ... $options) : bool + { + foreach($options as $opt) { + if ( $this->matchOption($opt) ) { + return true; + } + } + + return false; + } + + public function matchOption(string $option) : bool + { + return true; + } + + protected function parseCommandLine() : void + { + $executionString = $this->argv(); + + # Removing execution's path + array_shift($executionString); + + foreach($executionString as $cmd) { + $this->command[] = $cmd; + } + + dump($executionString); + + $this->command = $executionString; + } + + protected function argv() : array + { + return $this->request->getServerParams()['argv']; + } +} \ No newline at end of file diff --git a/src/Command.php b/src/Command.php new file mode 100644 index 0000000..3f4905f --- /dev/null +++ b/src/Command.php @@ -0,0 +1,20 @@ +options[] = array_merge($this->options, $option); + } +} \ No newline at end of file diff --git a/src/CommandStack.php b/src/CommandStack.php new file mode 100644 index 0000000..9f4d6a0 --- /dev/null +++ b/src/CommandStack.php @@ -0,0 +1,34 @@ +commands = array_merge($this->commands, $stack->commands); + } + + public function append(Command $command) : void + { + $this->commands[] = $command; + } + + public function matchCommand(array $commands, array $options) : null|Command + { + $cmd = implode(' ', $commands); + + foreach($this->commands as $command) { + if ($command->name === $cmd) { + return $command; + } + } + + return null; + } +} \ No newline at end of file diff --git a/src/Option.php b/src/Option.php new file mode 100644 index 0000000..7020b3d --- /dev/null +++ b/src/Option.php @@ -0,0 +1,21 @@ +