Compare commits
No commits in common. "02c0b2d432d8f1b9bf7ff4b3f42b01f243a7a1f1" and "3bebb66ad4a54b39930d874b8a1809a0694a294a" have entirely different histories.
02c0b2d432
...
3bebb66ad4
@ -14,7 +14,7 @@ class Route implements \Notes\Annotation {
|
||||
*/
|
||||
public string $route;
|
||||
|
||||
public /*string*/ $method;
|
||||
public string $method;
|
||||
|
||||
public string $base;
|
||||
|
||||
@ -24,8 +24,6 @@ class Route implements \Notes\Annotation {
|
||||
|
||||
public string $classMethod;
|
||||
|
||||
public bool $currentRoute = false;
|
||||
|
||||
public function __construct($route = null, $name = null, $method = null)
|
||||
{
|
||||
if ( $route !== null ) {
|
||||
@ -57,8 +55,4 @@ class Route implements \Notes\Annotation {
|
||||
public function isRoute(string $name) {
|
||||
return $this->name === $name;
|
||||
}
|
||||
|
||||
public function currentRoute(bool|null $value = null ) {
|
||||
return is_null($value) ? $value : $this->currentRoute = $value;
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Notes\Route\Attribute\Method;
|
||||
|
||||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
|
||||
class Route implements \Notes\Attribute {
|
||||
public function __construct(
|
||||
public string $route,
|
||||
public string $name,
|
||||
public string|array $method = [ "GET", "POST" ],
|
||||
public string $base = "",
|
||||
public ? string $class = null,
|
||||
public ? string $classMethod = null,
|
||||
public bool $currentRoute = false,
|
||||
) {}
|
||||
|
||||
public function getRoute() : string
|
||||
{
|
||||
return rtrim("/" . trim(isset($this->base) ?
|
||||
"/" . trim($this->base, "/") . "/" . ltrim($this->route, "/")
|
||||
:
|
||||
"/" . ltrim($this->route, "/"), "/"), '/');
|
||||
}
|
||||
|
||||
public function matchRouteName(string $name) : bool
|
||||
{
|
||||
return strtolower($this->name) === strtolower($name);
|
||||
}
|
||||
|
||||
public function isRoute(string $name) {
|
||||
return $this->name === $name;
|
||||
}
|
||||
|
||||
public function currentRoute(bool|null $value = null ) {
|
||||
return is_null($value) ? $value : $this->currentRoute = $value;
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Notes\Route\Attribute\Object;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)]
|
||||
class Route implements \Notes\Annotation {
|
||||
public function __construct(
|
||||
public string|array $method = [ "GET", "POST" ],
|
||||
public string $base = "",
|
||||
){}
|
||||
}
|
@ -1,18 +1,11 @@
|
||||
<?php namespace Notes\Route;
|
||||
|
||||
use Kash\HandleCacheTrait;
|
||||
use Notes\ObjectReflection,
|
||||
Notes\ObjectResolver;
|
||||
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use RuntimeException, DirectoryIterator, Generator, Closure;
|
||||
|
||||
class RouteFetcher {
|
||||
use HandleCacheTrait;
|
||||
|
||||
public array $list = [];
|
||||
|
||||
protected bool $debug;
|
||||
|
||||
protected array $folderList;
|
||||
|
||||
@ -22,10 +15,8 @@ class RouteFetcher {
|
||||
|
||||
protected array $annotations;
|
||||
|
||||
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null)
|
||||
public function __construct(?Closure $callback = null, ?array $folderList = null, ?array $annotations = null)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
|
||||
if ($callback !== null) {
|
||||
$this->callback = $callback;
|
||||
}
|
||||
@ -39,8 +30,8 @@ class RouteFetcher {
|
||||
}
|
||||
else {
|
||||
$this->annotations = [
|
||||
'object' => [ Annotation\Object\Route::class, Attribute\Object\Route::class ],
|
||||
'method' => [ Annotation\Method\Route::class ],
|
||||
'object' => Annotation\Object\Route::class,
|
||||
'method' => Annotation\Method\Route::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -77,13 +68,8 @@ class RouteFetcher {
|
||||
}
|
||||
}
|
||||
|
||||
public function compile(null|array $annotations = null) : array
|
||||
public function compile() : Generator
|
||||
{
|
||||
$annotations ??= $this->annotations;
|
||||
|
||||
return $this->handleCaching(substr(md5(serialize($annotations)), 0, 7), function() use ($annotations) : array {
|
||||
$list = [];
|
||||
|
||||
foreach($this->scan() as $namespace => $file) {
|
||||
if ( $file->getExtension() !== "php" ) {
|
||||
continue;
|
||||
@ -93,29 +79,21 @@ class RouteFetcher {
|
||||
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
|
||||
$methods = $this->defaultMethods;
|
||||
|
||||
$objectResolver = new ObjectResolver($class, true, true, false, true, $this->cache);
|
||||
# Should generate an equivalent of Ulmus's object reflection here !
|
||||
$objectResolver = new ObjectResolver($class, true, true, false, true);
|
||||
|
||||
if ( isset($annotations['object']) ) {
|
||||
$object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false);
|
||||
|
||||
if ($object) {
|
||||
if ( null !== ( $object = $objectResolver->getAnnotationFromClassname( $this->annotations['object'] ) ) ) {
|
||||
if ( $object->methods ?? false ) {
|
||||
$methods = $object->methods;
|
||||
}
|
||||
elseif ($object->method ?? false ) {
|
||||
$methods = (array) $object->method;
|
||||
}
|
||||
|
||||
$base = $object->base ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset($annotations['method']) ) {
|
||||
$routeList = $objectResolver->getAnnotationListFromClassname( $annotations['method'], false );
|
||||
;
|
||||
$routeList = $objectResolver->getAnnotationListFromClassname( $this->annotations['method'] );
|
||||
|
||||
foreach($routeList as $func => $routes) {
|
||||
if (is_array($routes)) {
|
||||
foreach ($routes as $route) {
|
||||
foreach($routes as $route) {
|
||||
$route->base = $base;
|
||||
$route->class = $class;
|
||||
$route->classMethod = $func;
|
||||
@ -128,16 +106,11 @@ class RouteFetcher {
|
||||
call_user_func_array($this->callback, [$route]);
|
||||
}
|
||||
|
||||
$list[] = $route;
|
||||
yield $route;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
});
|
||||
}
|
||||
|
||||
protected function generateClassname($file, $namespace)
|
||||
{
|
||||
|
Reference in New Issue
Block a user