- WIP on attributes ; on testing phase
This commit is contained in:
		
							parent
							
								
									b9aadd41ee
								
							
						
					
					
						commit
						02c0b2d432
					
				| @ -2,11 +2,11 @@ | |||||||
| 
 | 
 | ||||||
| namespace Notes\Route\Attribute\Method; | namespace Notes\Route\Attribute\Method; | ||||||
| 
 | 
 | ||||||
| #[\Attribute]
 | #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
 | ||||||
| class Route implements \Notes\Attribute { | class Route implements \Notes\Attribute { | ||||||
|     public function __construct( |     public function __construct( | ||||||
|         public string $name, |  | ||||||
|         public string $route, |         public string $route, | ||||||
|  |         public string $name, | ||||||
|         public string|array $method = [ "GET", "POST" ], |         public string|array $method = [ "GET", "POST" ], | ||||||
|         public string $base = "", |         public string $base = "", | ||||||
|         public ? string $class = null, |         public ? string $class = null, | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| namespace Notes\Route\Attribute\Object; | namespace Notes\Route\Attribute\Object; | ||||||
| 
 | 
 | ||||||
| #[\Attribute]
 | #[\Attribute(\Attribute::TARGET_CLASS)]
 | ||||||
| class Route implements \Notes\Annotation { | class Route implements \Notes\Annotation { | ||||||
|     public function __construct( |     public function __construct( | ||||||
|         public string|array $method = [ "GET", "POST" ], |         public string|array $method = [ "GET", "POST" ], | ||||||
|  | |||||||
| @ -1,5 +1,6 @@ | |||||||
| <?php namespace Notes\Route; | <?php namespace Notes\Route; | ||||||
| 
 | 
 | ||||||
|  | use Kash\HandleCacheTrait; | ||||||
| use Notes\ObjectReflection, | use Notes\ObjectReflection, | ||||||
|     Notes\ObjectResolver; |     Notes\ObjectResolver; | ||||||
| 
 | 
 | ||||||
| @ -7,6 +8,7 @@ use Psr\SimpleCache\CacheInterface; | |||||||
| use RuntimeException, DirectoryIterator, Generator, Closure; | use RuntimeException, DirectoryIterator, Generator, Closure; | ||||||
| 
 | 
 | ||||||
| class RouteFetcher { | class RouteFetcher { | ||||||
|  |     use HandleCacheTrait; | ||||||
| 
 | 
 | ||||||
|     public array $list = []; |     public array $list = []; | ||||||
| 
 | 
 | ||||||
| @ -20,11 +22,7 @@ class RouteFetcher { | |||||||
| 
 | 
 | ||||||
|     protected array $annotations; |     protected array $annotations; | ||||||
| 
 | 
 | ||||||
|     protected CacheInterface $cache; |     public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null) | ||||||
| 
 |  | ||||||
|     protected bool $forceCacheWrite; |  | ||||||
| 
 |  | ||||||
|     public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null, bool $forceCacheWrite = false) |  | ||||||
|     { |     { | ||||||
|         $this->cache = $cache; |         $this->cache = $cache; | ||||||
| 
 | 
 | ||||||
| @ -45,8 +43,6 @@ class RouteFetcher { | |||||||
|                 'method' => [ Annotation\Method\Route::class ], |                 'method' => [ Annotation\Method\Route::class ], | ||||||
|             ]; |             ]; | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|         $this->forceCacheWrite = $forceCacheWrite; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public function addFolder($folder) : void |     public function addFolder($folder) : void | ||||||
| @ -97,19 +93,21 @@ class RouteFetcher { | |||||||
|                 $class = $this->generateClassname($file->getBasename(".php"), $namespace); |                 $class = $this->generateClassname($file->getBasename(".php"), $namespace); | ||||||
|                 $methods = $this->defaultMethods; |                 $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']) ) { |                 if ( isset($annotations['object']) ) { | ||||||
|                     $object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false); |                     $object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false); | ||||||
| 
 | 
 | ||||||
|                     if ( $object->methods ?? false ) { |                     if ($object) { | ||||||
|                         $methods = $object->methods; |                         if ( $object->methods ?? false ) { | ||||||
|                     } |                             $methods = $object->methods; | ||||||
|                     elseif ($object->method ?? false ) { |                         } | ||||||
|                         $methods = (array) $object->method; |                         elseif ($object->method ?? false ) { | ||||||
|                     } |                             $methods = (array) $object->method; | ||||||
|  |                         } | ||||||
| 
 | 
 | ||||||
|                     $base = $object->base ?? ""; |                         $base = $object->base ?? ""; | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 if ( isset($annotations['method']) ) { |                 if ( isset($annotations['method']) ) { | ||||||
| @ -145,21 +143,4 @@ class RouteFetcher { | |||||||
|     { |     { | ||||||
|         return "\\$namespace\\$file"; |         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