- First commit
This commit is contained in:
		
						commit
						d30843dbfd
					
				
							
								
								
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							@ -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.
 | 
			
		||||
							
								
								
									
										26
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							@ -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": "info@mcnd.ca"
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "require": {
 | 
			
		||||
        "mcnd/notes": "master-dev"
 | 
			
		||||
    },
 | 
			
		||||
    "repositories": [
 | 
			
		||||
        {
 | 
			
		||||
            "type": "vcs",
 | 
			
		||||
            "url": "https://github.com/mcNdave/notes.git"
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "autoload": {
 | 
			
		||||
        "psr-4": {
 | 
			
		||||
            "Notes\\Cronard\\": "src/"
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								src/Attribute/Method/Event.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/Attribute/Method/Event.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Notes\Event\Attribute\Method;
 | 
			
		||||
 | 
			
		||||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
 | 
			
		||||
class Event implements \Notes\Attribute {
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        public string $name,
 | 
			
		||||
    ) {}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								src/Attribute/Object/Event.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/Attribute/Object/Event.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Notes\Event\Attribute\Object;
 | 
			
		||||
 | 
			
		||||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)]
 | 
			
		||||
class Event implements \Notes\Attribute {
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        public string $name,
 | 
			
		||||
    ) {}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										99
									
								
								src/EventFetcher.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								src/EventFetcher.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,99 @@
 | 
			
		||||
<?php namespace Notes\Event;
 | 
			
		||||
 | 
			
		||||
use Kash\HandleCacheTrait;
 | 
			
		||||
use Notes\ObjectResolver;
 | 
			
		||||
 | 
			
		||||
use Psr\SimpleCache\CacheInterface;
 | 
			
		||||
use RuntimeException, DirectoryIterator, Generator, Closure;
 | 
			
		||||
 | 
			
		||||
class EventFetcher {
 | 
			
		||||
    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\Event::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->getAnnotationListFromClassname( $this->annotations['method'], false ) as $func => $events) {
 | 
			
		||||
                    foreach($events as $ev) {
 | 
			
		||||
                        $list[] = new \Mcnd\Event\Event(name: $ev->name, class: $class, method: $func, attribute: $ev);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            return $list ?? [];
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function generateClassname($file, $namespace)
 | 
			
		||||
    {
 | 
			
		||||
        return "\\$namespace\\$file";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user