From 6f8d554eebbf90422ae269adee8a675fd02777db Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 19 Oct 2021 12:49:51 +0000 Subject: [PATCH] - First commit on WIP of this project --- LICENSE | 21 ++++++ composer.json | 26 ++++++++ src/Annotation/Method/Cronard.php | 16 +++++ src/Annotation/Object/Cronard.php | 20 ++++++ src/TaskFetcher.php | 103 ++++++++++++++++++++++++++++++ 5 files changed, 186 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/Annotation/Method/Cronard.php create mode 100644 src/Annotation/Object/Cronard.php create mode 100644 src/TaskFetcher.php 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..8f21ee0 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "mcnd/notes-cronard", + "description": "List every runnable cron method from your controller using annotations.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "mcndave@gmail.com" + } + ], + "require": { + "mcnd/notes": "master-dev" + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/mcNdave/notes.git" + } + ], + "autoload": { + "psr-4": { + "Notes\\Cronard\\": "src/" + } + } +} diff --git a/src/Annotation/Method/Cronard.php b/src/Annotation/Method/Cronard.php new file mode 100644 index 0000000..a3a533d --- /dev/null +++ b/src/Annotation/Method/Cronard.php @@ -0,0 +1,16 @@ +cron = $cron; + } +} diff --git a/src/Annotation/Object/Cronard.php b/src/Annotation/Object/Cronard.php new file mode 100644 index 0000000..e157128 --- /dev/null +++ b/src/Annotation/Object/Cronard.php @@ -0,0 +1,20 @@ +cron = $cron; + + $this->method = "__invoke"; + } +} diff --git a/src/TaskFetcher.php b/src/TaskFetcher.php new file mode 100644 index 0000000..28dfd5b --- /dev/null +++ b/src/TaskFetcher.php @@ -0,0 +1,103 @@ +folderList = $folderList; + } + + if ($annotations !== null) { + $this->annotations = $annotations; + } + else { + $this->annotations = [ + #'object' => Annotation\Object\Cronard::class, + 'method' => Annotation\Method\Route::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() : Generator + { + foreach($this->scan() as $namespace => $file) { + if ( $file->getExtension() !== "php" ) { + continue; + } + + $base = ""; + $class = $this->generateClassname($file->getBasename(".php"), $namespace); + $methods = $this->defaultMethods; + + # Should generate an equivalent of Ulmus's object reflection here ! + $objectResolver = new ObjectResolver($class, true, true, false, true); + + if ( null !== ( $object = $objectResolver->getAnnotationFromClassname( $this->annotations['object'] ) ) ) { + if ( $object->methods ?? false ) { + $methods = $object->methods; + } + + $base = $object->base ?? ""; + } + + $taskList = $objectResolver->getAnnotationListFromClassname( $this->annotations['method'] ); + + foreach($taskList as $func => $cronard) { + foreach($cronard as $task) { + yield $task; + } + } + } + } + + protected function generateClassname($file, $namespace) + { + return "\\$namespace\\$file"; + } +}