- Merged multiple sub-libraries
This commit is contained in:
1 parent
767152e0c3
commit
cee97fca25
17 files changed
+787
No files matched your search
@@ -0,0 +1,99 @@
|
||||
<?php namespace Notes\Cronard;
|
||||
|
||||
use Kash\HandleCacheTrait;
|
||||
use Notes\ObjectResolver;
|
||||
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use RuntimeException, DirectoryIterator, Generator, Closure;
|
||||
|
||||
class TaskFetcher {
|
||||
use HandleCacheTrait;
|
||||
|
||||
protected array $folderList;
|
||||
|
||||
protected Closure $callback;
|
||||
|
||||
public array $defaultMethods = [ 'GET', 'POST' ];
|
||||
|
||||
protected array $annotations;
|
||||
|
||||
public function __construct( ?array $folderList = null, ?array $annotations = null, ? CacheInterface $cache = null)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
|
||||
if ($folderList !== null) {
|
||||
$this->folderList = $folderList;
|
||||
}
|
||||
|
||||
if ($annotations !== null) {
|
||||
$this->annotations = $annotations;
|
||||
}
|
||||
else {
|
||||
$this->annotations = [
|
||||
'method' => [ Attribute\Method\Cronard::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() : array
|
||||
{
|
||||
return $this->handleCaching(substr(md5(serialize($this->annotations)), 0, 7), function() : array {
|
||||
foreach($this->scan() as $namespace => $file) {
|
||||
if ( $file->getExtension() !== "php" ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
|
||||
|
||||
# Should generate an equivalent of Ulmus's object reflection here !
|
||||
$objectResolver = new ObjectResolver($class, true, true, false, true);
|
||||
|
||||
foreach($objectResolver->getAttributeListFromClassname( $this->annotations['method'], false ) as $func => $cronard) {
|
||||
foreach($cronard as $task) {
|
||||
$list[] = [ 'class' => $class, 'method' => $func, 'annotation' => $task ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $list ?? [];
|
||||
});
|
||||
}
|
||||
|
||||
protected function generateClassname($file, $namespace)
|
||||
{
|
||||
return "\\$namespace\\$file";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user