- WIP on attributes ; on testing phase
This commit is contained in:
		
							parent
							
								
									b9aadd41ee
								
							
						
					
					
						commit
						02c0b2d432
					
				| @ -2,11 +2,11 @@ | ||||
| 
 | ||||
| namespace Notes\Route\Attribute\Method; | ||||
| 
 | ||||
| #[\Attribute]
 | ||||
| #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
 | ||||
| class Route implements \Notes\Attribute { | ||||
|     public function __construct( | ||||
|         public string $name, | ||||
|         public string $route, | ||||
|         public string $name, | ||||
|         public string|array $method = [ "GET", "POST" ], | ||||
|         public string $base = "", | ||||
|         public ? string $class = null, | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
| 
 | ||||
| namespace Notes\Route\Attribute\Object; | ||||
| 
 | ||||
| #[\Attribute]
 | ||||
| #[\Attribute(\Attribute::TARGET_CLASS)]
 | ||||
| class Route implements \Notes\Annotation { | ||||
|     public function __construct( | ||||
|         public string|array $method = [ "GET", "POST" ], | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| <?php namespace Notes\Route; | ||||
| 
 | ||||
| use Kash\HandleCacheTrait; | ||||
| use Notes\ObjectReflection, | ||||
|     Notes\ObjectResolver; | ||||
| 
 | ||||
| @ -7,6 +8,7 @@ use Psr\SimpleCache\CacheInterface; | ||||
| use RuntimeException, DirectoryIterator, Generator, Closure; | ||||
| 
 | ||||
| class RouteFetcher { | ||||
|     use HandleCacheTrait; | ||||
| 
 | ||||
|     public array $list = []; | ||||
| 
 | ||||
| @ -20,11 +22,7 @@ class RouteFetcher { | ||||
| 
 | ||||
|     protected array $annotations; | ||||
| 
 | ||||
|     protected CacheInterface $cache; | ||||
| 
 | ||||
|     protected bool $forceCacheWrite; | ||||
| 
 | ||||
|     public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null, bool $forceCacheWrite = false) | ||||
|     public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null) | ||||
|     { | ||||
|         $this->cache = $cache; | ||||
| 
 | ||||
| @ -45,8 +43,6 @@ class RouteFetcher { | ||||
|                 'method' => [ Annotation\Method\Route::class ], | ||||
|             ]; | ||||
|         } | ||||
| 
 | ||||
|         $this->forceCacheWrite = $forceCacheWrite; | ||||
|     } | ||||
| 
 | ||||
|     public function addFolder($folder) : void | ||||
| @ -97,11 +93,12 @@ class RouteFetcher { | ||||
|                 $class = $this->generateClassname($file->getBasename(".php"), $namespace); | ||||
|                 $methods = $this->defaultMethods; | ||||
| 
 | ||||
|                 $objectResolver = new ObjectResolver($class, true, true, false, true); | ||||
|                 $objectResolver = new ObjectResolver($class, true, true, false, true, $this->cache); | ||||
| 
 | ||||
|                 if ( isset($annotations['object']) ) { | ||||
|                     $object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false); | ||||
| 
 | ||||
|                     if ($object) { | ||||
|                         if ( $object->methods ?? false ) { | ||||
|                             $methods = $object->methods; | ||||
|                         } | ||||
| @ -111,6 +108,7 @@ class RouteFetcher { | ||||
| 
 | ||||
|                         $base = $object->base ?? ""; | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 if ( isset($annotations['method']) ) { | ||||
|                     $routeList = $objectResolver->getAnnotationListFromClassname( $annotations['method'], false ); | ||||
| @ -145,21 +143,4 @@ class RouteFetcher { | ||||
|     { | ||||
|         return "\\$namespace\\$file"; | ||||
|     } | ||||
| 
 | ||||
|     protected function handleCaching(string $key, callable $callback, null|int|\DateInterval $ttl = null) : mixed | ||||
|     { | ||||
|         static $internalCache = []; | ||||
| 
 | ||||
|         if ( isset($internalCache[$key]) ) { | ||||
|             return $internalCache[$key]; | ||||
|         } | ||||
| 
 | ||||
|         if ($this->forceCacheWrite || null === ( $internalCache[$key] = $this->cache->get($key) )) { | ||||
|             $internalCache[$key] = call_user_func($callback); | ||||
| 
 | ||||
|             $this->cache->set($key, $internalCache[$key], $ttl); | ||||
|         } | ||||
| 
 | ||||
|         return $internalCache[$key]; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user