commit 8e750da7a7c771753ca918968fc8d75e5104b159 Author: Dave Mc Nicoll Date: Sun Jul 9 19:12:46 2023 -0400 - First commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68594ef --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 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..bf39dfc --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "mcnd/notes-cli", + "description": "List every runnable method from the CLI within attributes from set of files.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "info@mcnd.ca" + } + ], + "require": { + "mcnd/notes": "master-dev" + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/mcNdave/notes.git" + } + ], + "autoload": { + "psr-4": { + "Notes\\CLI\\": "src/" + } + } +} diff --git a/src/Attribute/Command.php b/src/Attribute/Command.php new file mode 100644 index 0000000..63fd9c0 --- /dev/null +++ b/src/Attribute/Command.php @@ -0,0 +1,17 @@ +cache = $cache; + + if ($folderList !== null) { + $this->folderList = $folderList; + } + + if ($annotations !== null) { + $this->annotations = $annotations; + } + else { + $this->annotations = [ + 'object' => [ Attribute\Command::class, Attribute\Option::class ], + 'method' => [ Attribute\Command::class, Attribute\Option::class ], + ]; + } + } + + public function addFolder($folder) : void + { + $this->folderList[] = $folder; + } + + public function setFolderList(array $list) : void + { + $this->folderList = $list; + } + + public function scan(? array $folders = null) : Generator + { + foreach($folders ?: $this->folderList as $namespace => $folder) { + if ( ! file_exists($folder) ) { + throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder)); + } + + foreach (new DirectoryIterator($folder) as $fileinfo) { + if ( ! $fileinfo->isDot() ) { + if ( $fileinfo->isDir() ) { + foreach($this->scan([ "{$namespace}\\" . $fileinfo->getBasename() => $fileinfo->getPathname() ]) as $ns2 => $fi2) { + yield $ns2 => $fi2; + } + } + else { + yield $namespace => $fileinfo; + } + } + } + } + } + + public function compile() : CommandStack + { + return $this->handleCaching(substr(md5(serialize($this->annotations)), 0, 7), function() : CommandStack { + $stack = new CommandStack(); + + $classCmd = []; + + foreach($this->scan() as $namespace => $file) { + if ( $file->getExtension() !== "php" ) { + continue; + } + + $basename = $file->getBasename(".php"); + + $class = $this->generateClassname($basename, $namespace); + + $objectResolver = new ObjectResolver($class, false, true, false, false); + + $attributes = $objectResolver->getAttributeListFromClassname( $this->annotations['object'], false ) ; + + $commandList = array_filter($attributes, fn($e) => $e instanceof Attribute\Command); + + ksort($commandList); + + if ($commandList) { + $fullCommand = []; + + # Build full command token + foreach($commandList as $cmd) { + if ($cmd->command) { + $fullCommand[] = $cmd->command; + } + } + + $fullCommand = implode(' ', $fullCommand); + + $commandHeader = new Command(name: $fullCommand, description: end($commandList)->description); + $commandHeader->pushOption(array_filter($attributes, fn($e) => $e instanceof Attribute\Option)); + + $stack->append($commandHeader); + + foreach($objectResolver->getAttributeListFromClassname( $this->annotations['method'], false ) as $method => $commands) { + if (is_array($commands) ) { + $commandAttribute = array_filter($commands, fn($e) => $e instanceof Attribute\Command)[0] ?? null; + $name = implode(' ', array_filter( [ $fullCommand, $commandAttribute->command ?? $method ])); + $commandFunction = new Command(name: $name, description: $commandAttribute->description, parent: $commandHeader, callback: "$class::$method"); + $stack->append($commandFunction); + + $commandFunction->pushOption(array_filter($commands, fn($e) => $e instanceof Attribute\Option)); + } + } + } + } + + return $stack; + }); + } + + protected function generateClassname($file, $namespace) + { + return "\\$namespace\\$file"; + } +}