Compare commits

..

No commits in common. "master" and "attributes" have entirely different histories.

32 changed files with 508 additions and 1452 deletions

@ -3,7 +3,7 @@
array_shift($argv);
$opt = getopt('c::f:v', [ 'folder:', 'confirm', 'verbose', 'clean', 'old' ]);
$opt = getopt('c::f:v', [ 'folder:', 'confirm', 'verbose' ]);
if ( ! $opt ) {
exit("Bad arguments provided, you must specify a fullpath using the -f or --folder option\n");
@ -30,80 +30,71 @@ foreach ($iterator as $info) {
$content = file_get_contents($info->getPathname());
if ( isset($opt['clean']) ) {
foreach (explode(PHP_EOL, $content) as $lineIdx => $line) {
if ( $pos = strpos($line, '# migrated from: ') ) {
$attr = true;
foreach(explode(PHP_EOL, $content) as $lineIdx => $line) {
$tline = trim($line);
$newContent[] = $newLine = substr($line, 0, $pos);
echo sprintf("Changed line '%s' \n", trim($newLine));
}
else {
$newContent[] = $line;
}
if ($tline === "/**") {
$opened = true;
}
elseif ($tline === '*/') {
$opened = false;
}
elseif (substr($tline, 0, 1) === '*') {
$annotation = trim(substr($tline, 1));
}
else {
foreach (explode(PHP_EOL, $content) as $lineIdx => $line) {
$tline = trim($line);
if (strpos($annotation, '@') === 0 ) {
$argpos = strpos($annotation, '(');
if ($tline === "/**") {
$opened = true;
} elseif ($tline === '*/') {
$opened = false;
} elseif (substr($tline, 0, 1) === '*') {
$annotation = trim(substr($tline, 1));
if ($argpos !== false) {
$name = substr($annotation, 1, $argpos - 1);
$args = substr($annotation, $argpos + 1, strrpos($annotation, ')') - $argpos - 1);
if (strpos($annotation, '@') === 0) {
$argpos = strpos($annotation, '(');
try{
$array = eval("return [{$args}];");
if ($argpos !== false) {
$name = substr($annotation, 1, $argpos - 1);
$args = substr($annotation, $argpos + 1, strrpos($annotation, ')') - $argpos - 1);
$renderedArgs = [];
try {
$array = eval("return [{$args}];");
foreach($array as $key => $argument) {
$renderedArgs = [];
foreach ($array as $key => $argument) {
if (is_array($argument)) {
$argument = var_export($argument, true);
} elseif (is_bool($argument)) {
$argument = $argument ? 'true' : 'false';
} elseif (!is_numeric($argument)) {
$argument = "\"$argument\"";
}
if (is_numeric($key)) {
$renderedArgs[] = $argument;
} else {
$renderedArgs[] = "$key: $argument";
}
if ( is_array($argument) ) {
$argument = var_export($argument, true);
}
elseif ( is_bool($argument) ) {
$argument = $argument ? 'true' : 'false';
}
elseif (! is_numeric($argument) ) {
$argument = "\"$argument\"";
}
$renderedArgs = implode(', ', $renderedArgs);
} catch (\Throwable $e) {
echo sprintf("!!! WARNING : %s in file (%s) line %d ; using previous notation.\n", $e->getMessage(), $info->getPathname(), $lineIdx + 1);
$renderedArgs = str_replace(' => ', ': ', $args);
if (is_numeric($key)) {
$renderedArgs[] = $argument;
}
else {
$renderedArgs[] = "$key: $argument";
}
}
} else {
$name = substr($annotation, 1);
$renderedArgs = "";
$renderedArgs = implode(', ', $renderedArgs);
}
catch(\Throwable $e) {
echo sprintf("!!! WARNING : %s in file (%s) line %d ; using previous notation.\n", $e->getMessage(), $info->getPathname(), $lineIdx + 1);
$attr[] = $attribute = sprintf("%s#[%s%s]%s", str_repeat(" ", strlen($line) - strlen(ltrim($line)) >= 4 ? 4 : 0), $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? ( isset($opt['old']) ? " # migrated from: $args" : "" ) : "");
$newContent[] = $attribute;
$renderedArgs = str_replace(' => ', ': ', $args);
}
}
} else {
$newContent[] = $line;
else {
$name = substr($annotation, 1);
$renderedArgs = "";
}
$attr[] = $attribute = sprintf("%s#[%s%s]%s", str_repeat(" ", strlen($line) - strlen(ltrim($line)) >= 4 ? 4 : 0), $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? " # migrated from: $args" : "");
$newContent[] = $attribute;
}
}
else {
$newContent[] = $line;
}
}
$newContent = implode(PHP_EOL, $newContent);

@ -6,12 +6,10 @@
"authors": [
{
"name": "Dave Mc Nicoll",
"email": "info@mcnd.ca"
"email": "mcndave@gmail.com"
}
],
"require": {
"mcnd/kash" : "dev-master"
},
"require": {},
"autoload": {
"psr-4": {
"Notes\\": "src/"

@ -34,12 +34,12 @@ class AnnotationReader
return $this->parseDocComment($method);
}
protected function parseDocComment(\Reflector $reflect)
protected function parseDocComment(Reflector $reflect)
{
$tags = [];
if ( $attributes = $reflect->getAttributes() ) {
foreach($attributes as $attr) {
if ( $reflect->getAttributes() ) {
foreach($reflect->getAttributes() as $attr) {
try {
$tags[] = [
'tag' => substr(strrchr($attr->getName(), "\\"), 1),
@ -99,8 +99,6 @@ class AnnotationReader
case $reflect instanceof ReflectionClass :
return $reflect->name;
}
return "#UNKNOWN#";
}
protected function getObjectNamespace(Reflector $reflect) : string

@ -1,59 +0,0 @@
<?php
namespace Notes;
use Notes\Common\ReflectedAttribute;
use Reflector, ReflectionClass, ReflectionProperty, ReflectionMethod;
class AttributeReader
{
const PHP_TYPES = [ "string", "int", "float", "object", "double", "closure", ];
public static function reflectAttributes(\Reflector $reflect) : array
{
$list = [];
foreach($reflect->getAttributes() as $attribute) {
try {
$list[] = new ReflectedAttribute(
tag: substr(strrchr($attribute->getName(), "\\"), 1),
object: $attribute->newInstance(),
arguments: $attribute->getArguments(),
);
}
catch(\Throwable $e) {
throw new \Exception(sprintf("%s for %s in file '%s'", $e->getMessage(), $reflect, static::getObjectName($reflect)));
}
}
return $list;
}
protected static function getObjectName(Reflector $reflect) : string
{
switch(true) {
case $reflect instanceof ReflectionMethod :
case $reflect instanceof ReflectionProperty :
return $reflect->class . "::" . $reflect->name;
case $reflect instanceof ReflectionClass :
return $reflect->name;
}
throw new \InvalidArgumentException("Given reflector is not supported within getObjectName().");
}
protected static function getObjectNamespace(Reflector $reflect) : string
{
switch(true) {
case $reflect instanceof ReflectionMethod :
case $reflect instanceof ReflectionProperty :
return $reflect->getDeclaringClass()->getNamespaceName();
case $reflect instanceof ReflectionClass :
return $reflect->getNamespaceName();
}
throw new \InvalidArgumentException("Given reflector is not supported within getObjectNamespace().");
}
}

@ -1,24 +0,0 @@
<?php
namespace Notes\Breadcrumb\Attribute\Method;
use Notes\Route\Attribute\Method\Route;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Breadcrumb implements \Notes\Attribute {
public Route $routeAnnotation;
public bool $current = false;
public function __construct(
public string $parent = "",
public string $icon = "",
public string $lang = "",
public string $route = "",
public array $methods = [],
public string $class = "",
public string $classMethod = "",
) {}
}

@ -1,75 +0,0 @@
<?php namespace Notes\Breadcrumb;
use Notes\ObjectReflection,
Notes\ObjectResolver;
use Notes\Route\{ RouteFetcher, Attribute as RouteAttribute };
use Psr\Http\Message\ServerRequestInterface;
use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator;
class Breadcrumb extends RouteFetcher {
public bool $ignoreSoleRoute = true;
public function getRouteTree(\Notes\Route\Attribute\Method\Route $route) : array
{
$tree = [];
$routes = $this->compile([
'object' => [ RouteAttribute\Object\Route::class ],
'method' => [ RouteAttribute\Method\Route::class ],
]);
$crumbs = $this->compile([
'method' => [ Attribute\Method\Breadcrumb::class ],
]);
while( $route ) {
$crumb = $this->getBreadcrumbAttribute($crumbs, $route);
if ($crumb) {
empty($tree) && $crumb->current = true;
$tree[$route->name] = $crumb;
$route = $this->getRouteAttribute($routes, $crumb);
}
else {
$route = null;
}
}
if ($this->ignoreSoleRoute && count($tree) === 1) {
return [];
}
return array_reverse($tree);
}
protected function getRouteAttribute(array $list, Attribute\Method\Breadcrumb $crumb) : null|RouteAttribute\Method\Route
{
if ($crumb->parent ?? null) {
foreach ($list as $route) {
if ($crumb->parent === $route->name) {
return $route;
}
}
}
return null;
}
protected function getBreadcrumbAttribute(array $list, RouteAttribute\Method\Route $route) : null|Attribute\Method\Breadcrumb
{
foreach($list as $crumb) {
if ($crumb->class === $route->class && $crumb->classMethod === $route->classMethod) {
$crumb->routeAnnotation = $route;
return $crumb;
}
}
return null;
}
}

@ -1,17 +0,0 @@
<?php
namespace Notes\CLI\Attribute;
use Attribute;
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
class Command implements \Notes\Attribute {
public function __construct(
public string $description,
public null|string $command = null,
public string|\Closure|null $default = null,
public null|string $version = null,
) {}
}

@ -1,10 +0,0 @@
<?php
namespace Notes\CLI\Attribute;
use Attribute;
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
class Option extends \Mcnd\CLI\Option implements \Notes\Attribute {
}

@ -1,136 +0,0 @@
<?php namespace Notes\CLI;
use Kash\HandleCacheTrait;
use Mcnd\CLI\Command;
use Mcnd\CLI\CommandStack;
use Mcnd\CLI\Option;
use Notes\ObjectResolver;
use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator, Closure;
class CommandFetcher {
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 = [
'object' => [ Attribute\Command::class, Attribute\Option::class ],
'method' => [ Attribute\Command::class, Attribute\Option::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() : CommandStack
{
return $this->handleCaching(substr(md5(serialize($this->annotations)), 0, 7), function() : CommandStack {
$stack = new CommandStack();
$classCmd = [];
foreach($this->scan() as $namespace => $file) {
if ( $file->getExtension() !== "php" ) {
continue;
}
$basename = $file->getBasename(".php");
$class = $this->generateClassname($basename, $namespace);
$objectResolver = new ObjectResolver($class, $this->cache);
$attributes = $objectResolver->getAttributeListFromClassname( $this->annotations['object'], false ) ;
$commandList = array_filter($attributes, fn($e) => $e instanceof Attribute\Command);
ksort($commandList);
if ($commandList) {
$fullCommand = [];
# Build full command token
foreach($commandList as $cmd) {
if ($cmd->command) {
$fullCommand[] = $cmd->command;
}
}
$fullCommand = implode(' ', $fullCommand);
$commandHeader = new Command(name: $fullCommand, description: end($commandList)->description);
$commandHeader->pushOption(array_filter($attributes, fn($e) => $e instanceof Attribute\Option));
$stack->append($commandHeader);
foreach($objectResolver->getAttributeListFromClassname( $this->annotations['method'], false ) as $method => $commands) {
if (is_array($commands) ) {
$commandAttribute = array_filter($commands, fn($e) => $e instanceof Attribute\Command)[0] ?? null;
$name = implode(' ', array_filter( [ $fullCommand, $commandAttribute->command ?? $method ]));
$commandFunction = new Command(name: $name, description: $commandAttribute->description, parent: $commandHeader, callback: "$class::$method");
$stack->append($commandFunction);
$commandFunction->pushOption(array_filter($commands, fn($e) => $e instanceof Attribute\Option));
}
}
}
}
return $stack;
});
}
protected function generateClassname($file, $namespace)
{
return "\\$namespace\\$file";
}
}

@ -1,82 +0,0 @@
<?php
namespace Notes\Common;
use Notes\Attribute\Ignore;
abstract class Reflected
{
public function hasIgnoreAttribute() : bool
{
return [] !== array_filter($this->attributes, fn($e) => $e->object instanceof Ignore);
}
public function allowsNull() : bool
{
return empty($this->type) || $this->expectType("null");
}
public function expectType(string $type) : bool
{
$type = strtolower($type);
foreach($this->getTypes() as $item) {
if ($type === "null") {
if ($item->type === "null" || $item->nullable) {
return true;
}
}
elseif ($type === $item->type) {
return true;
}
}
return false;
}
public function getTypes() : array
{
return is_array($this->type) ? $this->type : [ $this->type ];
}
public function typeFromReflection(\ReflectionProperty|\ReflectionParameter $property) : void
{
if ( $property->hasType() ) {
$type = $property->getType();
if ($type instanceof \ReflectionUnionType ) {
foreach($type->getTypes() as $item) {
$this->type[] = new ReflectedPropertyType($item->getName(), $item->isBuiltIn(), $item->allowsNull());
}
}
else {
$this->type = new ReflectedPropertyType($type->getName(), $type->isBuiltIn(), $type->allowsNull());
}
}
}
public function getAttributes(null|string|array $attributeType = null): array
{
if ($attributeType) {
$list = [];
foreach($this->attributes as $attribute) {
foreach((array) $attributeType as $type) {
if ($attribute->object instanceof $type) {
$list[] = $attribute;
}
}
}
return array_reverse($list);
}
return array_reverse($this->attributes);
}
public function getAttribute(string|array $attributeType): ?object
{
return $this->getAttributes($attributeType)[0] ?? null;
}
}

@ -1,12 +0,0 @@
<?php
namespace Notes\Common;
class ReflectedAttribute
{
public function __construct(
public mixed $tag,
public object $object,
public array $arguments = [],
){}
}

@ -1,183 +0,0 @@
<?php
namespace Notes\Common;
class ReflectedClass implements ReflectedInterface
{
public function __construct(
public string $name,
public false|ReflectedClass $parent = false,
public array $methods = [],
public array $properties = [],
public array $attributes = [],
public array $interfaces = [],
public array $traits = [],
) {}
public function getClassName() : string
{
return end(explode('\\', $this->name));
}
public function getProperties(bool $deep = true) : array
{
return $deep ? array_replace(
$this->parent ? $this->parent->getProperties(true) : [],
$this->properties
) : $this->properties;
}
public function getMethods(bool $deep = true) : array
{
$m = array_merge(...array_map(fn($e) => $e->getMethods(true), $this->traits));
return $deep ? array_replace(
$this->parent ? $this->parent->getMethods(true) : [],
array_merge(...array_map(fn($e) => $e->getMethods(true), $this->interfaces)),
array_merge(...array_map(fn($e) => $e->getMethods(true), $this->traits)),
$this->methods
) : $this->methods;
}
public function getAttributes(bool $deep = true, ? string $attributeType = null) : array
{
return array_reverse($this->getOrderedAttributes($deep, $attributeType));
}
protected function getOrderedAttributes(bool $deep = true, ? string $attributeType = null) : array
{
$attributes = $deep ? array_merge(
$this->parent ? $this->parent->getOrderedAttributes(true) : [],
array_merge(...array_map(fn($e) => $e->getOrderedAttributes(true), $this->interfaces)),
array_merge(...array_map(fn($e) => $e->getOrderedAttributes(true), $this->traits)),
$this->attributes
) : $this->attributes;
if ($attributeType) {
$list = [];
foreach($attributes as $attribute) {
if ($attribute->object instanceof $attributeType) {
$list[] = $attribute;
}
}
return $list;
}
return $attributes;
}
# Get Attributes based on Native attributes's target
public function getClassAttributeList(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
{
$list = [];
foreach((array) $className as $class) {
$reflection = new \ReflectionClass($class);
$attributes = $reflection->getAttributes();
foreach($attributes as $attribute) {
# Native PHP Attribute
if ($attribute->getName() === 'Attribute') {
if ($attribute->getTarget() && \Attribute::TARGET_CLASS) {
$list = $this->getClassAttributeListFromClassname($class, $deepSearch, false);
}
if ($attribute->getTarget() && \Attribute::TARGET_METHOD) {
$list = array_merge($list, $this->getMethodAttributeListFromClassname($class, $deepSearch, false));
}
if ($attribute->getTarget() && \Attribute::TARGET_PROPERTY) {
$list = array_merge($list, $this->getPropertyAttributeListFromClassname($class, $deepSearch, false));
}
}
}
}
if ( empty($list) && $throwOnError ) {
$className = implode(',', (array) $className);
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within class {$this->name} (or it's parents / traits)");
}
return $list;
}
public function getClassAttributeListFromClassname(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
{
$list = [];
foreach((array) $className as $class) {
foreach ($this->getAttributes($deepSearch) as $item) {
if ($item->object instanceof $class) {
$list[] = $item->object;
}
}
}
if ( empty($list) && $throwOnError ) {
$className = implode(',', (array) $className);
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within class {$this->name} (or it's parents / traits)");
}
return $list;
}
public function getPropertyAttributeListFromClassname(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
{
$list = [];
foreach((array) $className as $class) {
foreach ($this->getProperties($deepSearch) as $property) {
foreach ($property->attributes as $item) {
if ($item->object instanceof $class) {
$list[$property->name] ??= [];
$list[$property->name][] = $item->object;
}
}
}
}
if ( empty($list) && $throwOnError ) {
$className = implode(',', (array) $className);
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within properties of {$this->name} (or it's parents / traits)");
}
return $list;
}
public function getMethodAttributeListFromClassname(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
{
$list = [];
foreach((array) $className as $class) {
foreach ($this->getMethods($deepSearch) as $method) {
foreach ($method->attributes as $item) {
if ($item->object instanceof $class) {
$list[$method->name] ??= [];
$list[$method->name][] = $item->object;
}
}
}
}
if ( empty($list) && $throwOnError ) {
$className = implode(',', (array) $className);
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within methods of {$this->name} (or it's parents / traits)");
}
return $list;
}
public function getAttribute(string $attributeType): ?object
{
foreach($this->getAttributes(true, $attributeType) as $attribute) {
if ($attribute->object instanceof $attributeType) {
return $attribute;
}
}
return null;
}
}

@ -1,8 +0,0 @@
<?php
namespace Notes\Common;
interface ReflectedInterface
{
public function getAttributes() : array;
}

@ -1,18 +0,0 @@
<?php
namespace Notes\Common;
class ReflectedMethod extends Reflected implements ReflectedInterface
{
public function __construct(
public string $name,
public string $classname,
public false|ReflectedMethodType|\ReflectionNamedType|\ReflectionUnionType|array $type = false,
public bool $isConstructor = false,
public bool $isDestructor = false,
public bool $isAbstract = false,
public array $attributes = [],
public array $parameters = [],
) {}
}

@ -1,12 +0,0 @@
<?php
namespace Notes\Common;
class ReflectedMethodProperty extends ReflectedProperty
{
public int $position;
public bool $optional = false;
public bool $byReference = false;
}

@ -1,8 +0,0 @@
<?php
namespace Notes\Common;
class ReflectedMethodType extends ReflectedPropertyType
{
}

@ -1,41 +0,0 @@
<?php
namespace Notes\Common;
use Notes\Attribute\Ignore;
class ReflectedProperty extends Reflected implements ReflectedInterface, \ArrayAccess
{
public mixed $value;
public function __construct(
public string $name,
public false|ReflectedPropertyType|array $type = false,
public array $attributes = [],
) {}
## BACKWARD COMPATIBILITY ONLY To be removed really soon !
public function offsetExists(mixed $offset): bool
{
return isset($this->$offset);
}
public function offsetGet(mixed $offset): mixed
{
if ($offset === 'tags') {
return $this->attributes;
}
return $this->$offset;
}
public function offsetSet(mixed $offset, mixed $value): void
{
$this->$offset = $value;
}
public function offsetUnset(mixed $offset): void
{
unset($this->$offset);
}
}

@ -1,21 +0,0 @@
<?php
namespace Notes\Common;
class ReflectedPropertyType
{
public function __construct(
public string $type,
public bool $builtIn,
public bool $nullable,
) {}
public function isType(string $type) : bool
{
if ($type === "null" || $this->nullable) {
return true;
}
return $this->type === $type;
}
}

@ -1,11 +0,0 @@
<?php
namespace Notes\Cronard\Attribute\Method;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Cronard implements \Notes\Attribute {
public function __construct(
public string $cron,
public null|string $method = null,
) {}
}

@ -1,11 +0,0 @@
<?php
namespace Notes\Cronard\Attribute\Object;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)]
class Cronard implements \Notes\Attribute {
public function __construct(
public string $cron,
public string $method = "__invoke",
) {}
}

@ -1,99 +0,0 @@
<?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, $this->cache);
foreach($objectResolver->reflectedClass->getMethodAttributeListFromClassname( $this->annotations['method'], false, 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";
}
}

@ -1,10 +0,0 @@
<?php
namespace Notes\Event\Attribute\Method;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Event implements \Notes\Attribute {
public function __construct(
public string $name,
) {}
}

@ -1,10 +0,0 @@
<?php
namespace Notes\Event\Attribute\Object;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)]
class Event implements \Notes\Attribute {
public function __construct(
public string $name,
) {}
}

@ -1,99 +0,0 @@
<?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, $this->cache ?? null);
foreach($objectResolver->reflectedClass->getMethodAttributeListFromClassname($this->annotations['method'], false, 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";
}
}

@ -4,11 +4,6 @@ namespace Notes;
use Kash\HandleCacheTrait;
use Notes\Attribute\Ignore;
use Notes\Common\ReflectedClass;
use Notes\Common\ReflectedMethod;
use Notes\Common\ReflectedMethodProperty;
use Notes\Common\ReflectedProperty;
use Notes\Common\ReflectedPropertyType;
use Psr\SimpleCache\CacheInterface;
use Reflector, ReflectionClass, ReflectionProperty, ReflectionMethod, ReflectionUnionType, ReflectionNamedType, ReflectionParameter;
@ -19,118 +14,289 @@ class ObjectReflection {
protected ReflectionClass $classReflection;
public function __construct(ReflectionClass|string $class, ? CacheInterface $cache = null) {
$this->classname = ltrim($class instanceof ReflectionClass ? $class->getName() : $class, '\\');
public AnnotationReader $annotationReader;
public function __construct($class, AnnotationReader $annotationReader = null, ? CacheInterface $cache = null) {
$this->classname = ltrim($class, '\\');
$this->cache = $cache;
$this->classReflection = $class instanceof ReflectionClass ? $class : new ReflectionClass($class);
#if ( ! $this->cache || ! $this->cache->has($class) ) {
$this->classReflection = $class instanceof ReflectionClass ? $class : new ReflectionClass($class);
$this->annotationReader = $annotationReader ?: AnnotationReader::fromClass($class);
# }
}
public static function fromClass(ReflectionClass|string $class, ? CacheInterface $cache = null) : self
public static function fromClass($class, ? CacheInterface $cache = null) : self
{
return new static($class, $cache);
return new static($class, null, $cache);
}
public function getClassReflection() : ReflectionClass
public function read(bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true) : array
{
return $this->classReflection;
return $this->handleCaching(implode('', [ $this->classname, (int)$fullObject, (int) $fullMethod, (int) $fullProperty ]), fn() => [
'uses' => $this->gatherUses($fullUses),
'class' => $this->gatherClass($fullObject),
'method' => $this->gatherMethods($fullMethod),
'property' => $this->gatherProperties($fullProperty),
]);
}
public function reflectClass() : ReflectedClass
public function gatherUses(bool $full = true) : array
{
$parentClass = $this->classReflection->getParentClass();
if ( $full ) {
if ( $parentClass = $this->classReflection->getParentClass() ) {
$list = static::fromClass($parentClass)->gatherUses(true);
}
return new ReflectedClass(
name: $this->classReflection->getName(),
parent: $parentClass ? static::fromClass($parentClass)->reflectClass() : false,
methods: $this->reflectMethods(),
properties: $this->reflectProperties(),
attributes: AttributeReader::reflectAttributes($this->classReflection),
interfaces: array_map(fn($interface) => static::fromClass($interface)->reflectClass(), array_keys($this->classReflection->getInterfaces())),
traits: array_map(fn($trait) => static::fromClass($trait)->reflectClass(), array_keys($this->classReflection->getTraits())),
);
foreach($this->classReflection->getTraits() as $trait) {
$list = array_replace(static::fromClass($trait->name)->gatherUses(true), $list ?? []);
}
}
return array_replace($list ?? [], $this->getUsesStatements());
}
public function reflectProperties(int $filter =
ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED |
ReflectionProperty::IS_PRIVATE
public function gatherClass(bool $full = true) : array
{
if ( $full ) {
if ( $parentClass = $this->classReflection->getParentClass() ) {
$class = static::fromClass($parentClass)->gatherClass(true);
}
if ( $traits = $this->classReflection->getTraits() ) {
foreach($traits as $key => $value) {
$traitTags = static::fromClass($key)->gatherClass(true);
}
}
if ( $interfaces = $this->classReflection->getInterfaces() ) {
foreach($interfaces as $key => $value) {
$interfaceTags = static::fromClass($key)->gatherClass(true);
}
}
$itemName = function($item) {
return $item->getName();
};
}
return array_merge_recursive($class ?? [], $traitTags ?? [], $interfaceTags ?? [], [
'tags' => $this->annotationReader->getClass($this->classReflection)
] + ( ! $full ? [] : [
'traits' => array_map($itemName, $traits),
'interfaces' => array_map($itemName, $interfaces),
] ));
}
public function gatherProperties(bool $full = true, int $filter =
ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED |
ReflectionProperty::IS_PRIVATE
) : array
{
$defaultValues = $this->classReflection->getDefaultProperties();
if ( $full ) {
if ( $parentClass = $this->classReflection->getParentClass() ) {
$properties = static::fromClass($parentClass)->gatherProperties($full, $filter);
}
}
$list = [];
foreach($this->classReflection->getProperties($filter) as $property) {
$reflected = new ReflectedProperty($property->getName());
$reflected->attributes = AttributeReader::reflectAttributes($property);
$current = [
'name' => $property->getName()
];
if ( $reflected->hasIgnoreAttribute() ) {
# Default value can be 'null', so isset() it not suitable here
if ( array_key_exists($current['name'], $defaultValues) ) {
$current['value'] = $defaultValues[ $current['name'] ];
}
if ( $property->hasType() ) {
$current['type'] = $property->getType()->getName();
$current['builtin'] = $property->getType()->isBuiltIn();
$current['nullable'] = $property->getType()->allowsNull();
}
$current['tags'] = $this->annotationReader->getProperty($property);
if ( $this->ignoreElementAnnotation($current['tags']) ) {
continue;
}
# Default value can be 'null', so isset() it not suitable here
if ( array_key_exists($reflected->name, $defaultValues) ) {
$reflected->value = $defaultValues[ $reflected->name ];
}
$reflected->typeFromReflection($property);
$list[ $reflected->name ] = $reflected;
$list[ $current['name'] ] = $current;
}
return array_merge($properties ?? [], $list);
}
public function reflectMethods(int $filter =
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
ReflectionMethod::IS_PRIVATE | ReflectionMethod::IS_STATIC |
ReflectionMethod::IS_FINAL
public function gatherMethods(bool $full = true, int $filter =
ReflectionMethod::IS_PUBLIC |
ReflectionMethod::IS_PROTECTED |
ReflectionMethod::IS_PRIVATE |
ReflectionMethod::IS_STATIC
) : array
{
$list = [];
foreach($this->classReflection->getMethods($filter) as $method) {
# Skipping parent's methods, we'll retrieve them in its own reflection
if ( $method->class !== $this->classname ) {
continue;
if ( $full ) {
if ( $parentClass = $this->classReflection->getParentClass() ) {
$methods = static::fromClass($parentClass)->gatherMethods($full, $filter);
}
$reflectedMethod = new ReflectedMethod(
name: $method->getName(),
classname: $method->class,
type: $method->hasReturnType() ? $method->getReturnType() : false,
isConstructor: $method->isConstructor(),
isDestructor: $method->isDestructor(),
isAbstract: $method->isAbstract(),
attributes: AttributeReader::reflectAttributes($method),
);
if ( $reflectedMethod->hasIgnoreAttribute() ) {
continue;
}
foreach($method->getParameters() as $parameter) {
$reflectedParameter = new ReflectedMethodProperty(
name: $parameter->getName(),
attributes: AttributeReader::reflectAttributes($parameter),
);
if ( $reflectedParameter->hasIgnoreAttribute() ) {
continue;
}
$reflectedParameter->position = $parameter->getPosition();
$reflectedParameter->typeFromReflection($parameter);
$reflectedParameter->byReference = $parameter->isPassedByReference();
$reflectedParameter->optional = $parameter->isOptional();
$reflectedMethod->parameters[$reflectedParameter->name] = $reflectedParameter;
}
$list[$reflectedMethod->name] = $reflectedMethod;
}
return $list;
foreach($this->classReflection->getMethods($filter) as $method) {
if ( ! $full && ( $method->class !== $this->classname ) ) {
continue;
}
$parameters = [];
foreach($method->getParameters() as $parameter) {
$parameters[$parameter->getName()] = [
'null' => $parameter->allowsNull(),
'position' => $parameter->getPosition(),
'type' => $parameter->hasType() && $parameter->getType() instanceof \ReflectionNamedType ? $parameter->getType()->getName() : false,
'array' => $this->isType('array', $parameter),
'callable' => $this->isType('callable', $parameter),
'optional' => $parameter->isOptional(),
'byReference' => $parameter->isPassedByReference(),
];
}
$current = [
'name' => $method->getName(),
'type' => $method->hasReturnType() && $method->getReturnType() instanceof \ReflectionNamedType ? $method->getReturnType()->getName() : false,
'constructor' => $method->isConstructor(),
'destructor' => $method->isDestructor(),
'parameters' => $parameters,
];
$current['tags'] = $this->annotationReader->getMethod($method);
if ( $this->ignoreElementAnnotation($current['tags']) ) {
continue;
}
$list[ $current['name'] ] = $current;
}
return array_merge($methods ?? [], $list);
}
protected function ignoreElementAnnotation($tags) : bool
{
return [] !== array_filter($tags, fn($e) => ( $e['object'] ?? null ) instanceof Ignore);
}
protected function readCode() : string
{
static $code = [];
$fileName = $this->classReflection->getFilename();
return $code[$fileName] ?? $code[$fileName] = file_get_contents($fileName);
}
# From https://www.php.net/manual/en/reflectionparameter.isarray.php
public static function isType(string $type, ReflectionParameter $reflectionParameter) : bool
{
if ( $reflectionType = $reflectionParameter->getType() ) {
$types = $reflectionType instanceof ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType];
return in_array($type, array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
}
return false;
}
protected function getUsesStatements() : array
{
$uses = [];
$tokens = token_get_all( $c = $this->readCode() );
while ( $token = array_shift($tokens) ) {
if ( is_array($token) ) {
list($token, $value) = $token;
}
switch ($token) {
case T_CLASS:
case T_TRAIT:
case T_INTERFACE:
break 2;
case T_USE:
$isUse = true;
break;
case T_NS_SEPARATOR:
$isNamespace = $isUse;
break;
case T_NAME_QUALIFIED:
case T_STRING:
if ( $isNamespace && $latestString ) {
$statement[] = $latestString;
}
$latestString = $value;
break;
case T_AS:
# My\Name\Space\aClassHere `as` ClassAlias;
$replacedClass = implode("\\", array_merge($statement, [ $latestString ]));
$latestString = null;
break;
case T_WHITESPACE:
case T_COMMENT:
case T_DOC_COMMENT:
break;
case '{':
# opening a sub-namespace -> \My\Name\Space\`{`OneItem, AnotherItem}
if ( $isNamespace ) {
$inNamespace = true;
}
break;
case ';';
case ',':
case '}':
if ( $isUse ) {
$clsName = ltrim(substr($latestString, strrpos($latestString, "\\") ), '\\');
if ( $replacedClass ) {
$uses[$replacedClass] = $clsName;
$replacedClass = "";
}
elseif ( $latestString ) {
$uses[implode("\\", array_merge($statement, [ $latestString ]))] = $clsName;
}
}
if ( $inNamespace ) {
$latestString = "";
# \My\Name\Space\{OneItem, AnotherItem`}` <- closing a sub-namespace
if ( $token !== "}" ) {
break;
}
}
case T_OPEN_TAG:
default:
$statement = [];
$latestString = "";
$replacedClass = null;
$isNamespace = $inNamespace = false;
$isUse = ( $isUse ?? false ) && ( $token === ',' );
break;
}
}
return $uses;
}
}

@ -2,13 +2,16 @@
namespace Notes;
use Notes\Common\ReflectedClass;
use Psr\SimpleCache\CacheInterface;
class ObjectResolver {
const KEY_ENTITY_NAME = 01;
const KEY_COLUMN_NAME = 02;
public string $objectClass;
public ReflectedClass $reflectedClass;
public array $uses;
public array $class;
@ -16,11 +19,52 @@ class ObjectResolver {
public array $methods;
public function __construct(string $objectClass, ? CacheInterface $cache = null)
public function __construct(string $objectClass, bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true, ? CacheInterface $cache = null)
{
$this->objectClass = $objectClass;
$this->reflectedClass = ObjectReflection::fromClass($objectClass, $cache)->reflectClass();
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
ObjectReflection::fromClass($objectClass, $cache)->read($fullUses, $fullObject, $fullMethod, $fullProperty)
);
$this->resolveAnnotations();
}
/**
* Transform an annotation into it's object's counterpart
*/
public function getAttributeFromClassname(array|string $className, bool $throwOnError = true) : object|bool
{
foreach((array) $className as $class) {
foreach (array_reverse($this->class['tags']) as $item) {
if ($item['object'] instanceof $class) {
return $this->instanciateAnnotationObject($item);
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $tag) {
if ($item['object'] instanceof $class) {
return $this->instanciateAnnotationObject($tag);
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $tag) {
if ($item['object'] instanceof $class) {
return $this->instanciateAnnotationObject($tag);
}
}
}
}
}
if ($throwOnError) {
throw new \Exception(sprintf("Annotation `%s` could not be found within your object `%s`.", implode(', ', (array)$className), $this->objectClass));
}
return false;
}
/**
@ -31,26 +75,27 @@ class ObjectResolver {
$list = [];
foreach((array) $className as $class) {
foreach ($this->reflectedClass->getAttributes() as $item) {
if ($item->object instanceof $class) {
$list[] = $item->object;
foreach ($this->class['tags'] as $item) {
if ($item['object'] instanceof $class) {
$list[] = $this->instanciateAnnotationObject($item);
}
}
foreach ($this->reflectedClass->getProperties(true) as $property) {
foreach ($property->attributes as $item) {
if ($item->object instanceof $class) {
$list[$property->name] ??= [];
$list[$property->name][] = $item->object;
foreach ($this->properties as $property) {
foreach ($property['tags'] as $item) {
if ($item['object'] instanceof $class) {
$list[$property['name']] ??= [];
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
}
}
}
foreach ($this->reflectedClass->getMethods(true) as $method) {
foreach ($method->attributes as $item) {
if ($item->object instanceof $class) {
$list[$method->name] ??= [];
$list[$method->name][] = $item->object;
foreach ($this->methods as $method) {
foreach ($method['tags'] as $item) {
if ($item['object'] instanceof $class) {
$list[$method['name']] ??= [];
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
}
}
}
@ -62,4 +107,148 @@ class ObjectResolver {
return $list;
}
/**
* @deprecated Will soon be deprecated in favour of PHP's native attributes
* Transform an annotation into it's object's counterpart
*/
public function getAnnotationFromClassname(array|string $className, bool $throwOnError = true) : object|bool
{
foreach((array) $className as $class) {
if ( $name = $this->uses[$class] ?? false) {
foreach (array_reverse($this->class['tags']) as $item) {
if ($item['tag'] === $name) {
return $this->instanciateAnnotationObject($item);
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $item) {
if ($item['tag'] === $name) {
return $this->instanciateAnnotationObject($item);
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $item) {
if ($item['tag'] === $name) {
return $this->instanciateAnnotationObject($item);
}
}
}
}
}
}
if ($throwOnError) {
throw new \Exception(sprintf("Annotation `%s` could not be found within your object `%s`.", implode(', ', (array)$className), $this->objectClass));
}
return false;
}
/**
* Transform an annotation into it's object's counterpart
*/
public function getAnnotationListFromClassname(array|string $className, bool $throwOnError = true) : array
{
$list = [];
foreach((array) $className as $class) {
if ($name = $this->uses[$class] ?? false) {
foreach ($this->class['tags'] as $item) {
if ($item['tag'] === $name) {
$list[] = $this->instanciateAnnotationObject($item);
}
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $item) {
if ($item['tag'] === $name) {
$list[$property['name']] ??= [];
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $item) {
if ($item['tag'] === $name) {
$list[$method['name']] ??= [];
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
}
}
}
}
}
if ( empty($list) ) {
if ($throwOnError) throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
}
return $list;
}
public function instanciateAnnotationObject(array $tagDefinition) : Annotation
{
if (isset($tagDefinition['object']) && $tagDefinition['object'] instanceof \Attribute) {
return $tagDefinition['object'];
}
$arguments = $this->extractArguments($tagDefinition['arguments']);
if ( false === $class = array_search($tagDefinition['tag'], $this->uses) ) {
return new class() implements Annotation {};
# throw new \InvalidArgumentException("Annotation class `{$tagDefinition['tag']}` was not found within {$this->objectClass} uses statement (or it's children / traits)");
}
$obj = new $class(... $arguments['constructor']);
foreach($arguments['setter'] as $key => $value) {
$obj->$key = $value;
}
return $obj;
}
/**
* Extracts arguments from an Annotation definition, easing object's declaration.
*/
protected function extractArguments(array $arguments) : array
{
$list = [
'setter' => [],
'constructor' => [],
];
ksort($arguments);
foreach($arguments as $key => $value) {
$list[ is_int($key) ? 'constructor' : 'setter' ][$key] = $value;
}
return $list;
}
protected function resolveAnnotations()
{
foreach($this->class['tags'] as $key => &$tag) {
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
foreach($this->properties as &$property) {
foreach($property['tags'] as &$tag){
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
}
foreach($this->methods as &$method) {
foreach($method['tags'] as &$tag){
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
}
}
}

@ -1,57 +0,0 @@
<?php
namespace Notes\Route\Attribute\Method;
use Psr\Http\Message\ServerRequestInterface;
#[\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 string $description = "",
) {}
public function getRoute() : string
{
return rtrim("/" . trim(isset($this->base) ?
"/" . trim($this->base, "/") . "/" . ltrim($this->route, "/")
:
"/" . ltrim($this->route, "/"), "/"), '/');
}
public function getRegistrableRoute() : string
{
return preg_replace('/(\=.*)(?=\})/i', '', $this->getRoute());
}
public function matchRouteName(string $name) : bool
{
return strtolower($this->name) === strtolower($name);
}
public function match(mixed $name) : bool
{
foreach((array) $name as $item) {
if ( fnmatch($item, $this->name) ) {
return true;
}
}
return false;
}
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,12 +0,0 @@
<?php
namespace Notes\Route\Attribute\Object;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Route implements \Notes\Attribute {
public function __construct(
public string|array $method = [ "GET", "POST" ],
# NULL will fallback on nearest base
public null|string $base = null,
){}
}

@ -1,147 +0,0 @@
<?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;
protected Closure $callback;
public array $defaultMethods = [ 'GET', 'POST' ];
protected array $attributes;
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $attributes = null, ? CacheInterface $cache = null)
{
$this->cache = $cache;
if ($callback !== null) {
$this->callback = $callback;
}
if ($folderList !== null) {
$this->folderList = $folderList;
}
if ($attributes !== null) {
$this->attributes = $attributes;
}
else {
$this->attributes = [
'object' => [ Attribute\Object\Route::class ],
'method' => [ Attribute\Method\Route::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(null|array $attributes = null) : array
{
$attributes ??= $this->attributes;
return $this->handleCaching(substr(md5(serialize($attributes)), 0, 7), function() use ($attributes) : array {
$list = [];
foreach($this->scan() as $namespace => $file) {
if ( $file->getExtension() !== "php" ) {
continue;
}
$base = null;
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
$methods = null;
$objectResolver = new ObjectResolver($class, $this->cache);
if ( isset($attributes['object']) ) {
$objects = $objectResolver->reflectedClass->getClassAttributeListFromClassname($attributes['object'], true, false);
foreach($objects as $object) {
if ($object->method ?? false ) {
$methods ??= (array) $object->method;
}
$base ??= $object->base ?? null;
}
}
if ( isset($attributes['method']) ) {
$routeList = $objectResolver->reflectedClass->getMethodAttributeListFromClassname( $attributes['method'], false, false );
foreach($routeList as $func => $routes) {
if (is_array($routes)) {
foreach ($routes as $route) {
if ($route instanceof \Notes\Route\Attribute\Method\Route) {
$route->base = $route->base ?: $base;
}
$route->class = $class;
$route->classMethod = $func;
if (false === ($route->methods ?? false)) {
$route->methods = $methods ?? $this->defaultMethods;
}
if (false !== ($this->callback ?? false)) {
call_user_func_array($this->callback, [$route]);
}
$list[] = $route;
}
}
}
}
}
return $list;
});
}
protected function generateClassname($file, $namespace)
{
return "\\$namespace\\$file";
}
}

@ -1,11 +0,0 @@
<?php
namespace Notes\Security\Attribute;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS)]
class Security implements \Notes\Attribute {
public function __construct(
public null|bool $locked = null,
public null|string $realm = null,
) {}
}

@ -1,12 +0,0 @@
<?php
namespace Notes\Security\Attribute;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS)]
class Taxus implements \Notes\Attribute {
public function __construct(
public null|string|\BackedEnum $privilege = null,
public null|string $module = null,
public string|array $method = "*",
) {}
}

@ -1,101 +0,0 @@
<?php declare(strict_types = 1);
namespace Notes\Security;
use Notes\Common\ReflectedAttribute;
use Taxus\Taxus;
use Psr\Http\Message\ResponseInterface;
use Notes\ObjectResolver;
use Taxus\TaxusGrantEnum;
class SecurityHandler {
protected ResponseInterface $redirectResponse;
protected ? \Closure $unauthorizeResponse;
protected ? Taxus $taxus;
public function __construct(ResponseInterface $redirectResponse, ? \Closure $unauthorizeResponse = null, ? Taxus $taxus = null) {
$this->redirectResponse = $redirectResponse;
$this->unauthorizeResponse = $unauthorizeResponse;
$this->taxus = $taxus;
}
public function verify(string $className, string $methodName) : ? ResponseInterface
{
return $this->isLocked($className, $methodName) ? $this->redirectResponse : null;
}
# @TODO Must HANDLE REALM !
public function isLocked(string $className, string $methodName) : bool
{
# Searching method first and fallbacking on object if none found
if ( $list = $this->findAttributes(Attribute\Security::class, $className, $methodName) ) {
return array_shift($list)->locked;
}
return true;
}
public function taxus(string $className, string $methodName, object $user = null) : ? ResponseInterface
{
$fromObject = $this->findAttributes(Attribute\Taxus::class, $className);
$fromMethod = $this->findAttributes(Attribute\Taxus::class, $className, $methodName);
if ($fromMethod || $fromObject) {
if ( $this->taxusGrantPermission($fromMethod, $user) || $this->taxusGrantPermission($fromObject, $user) ) {
return null;
}
if ($this->unauthorizeResponse) {
return call_user_func_array($this->unauthorizeResponse, [ $user, ['method' => $fromMethod, 'object' => $fromObject ], $className, $methodName ]);
}
else {
throw new \ErrorException("Unauthorized response given.");
}
}
return null;
}
protected function findAttributes(string $attribute, string $class, ? string $method = null) : array
{
$objectResolver = new ObjectResolver($class);
if ($method) {
$fromMethod = $objectResolver->reflectedClass->getMethods( false );
if (isset($fromMethod[$method])) {
$attributeList = $fromMethod[$method]->getAttributes($attribute);
if ($attributeList) {
return array_map(fn(ReflectedAttribute $ref) => $ref->object, $attributeList);
}
}
}
$attributeList = $objectResolver->reflectedClass->getAttributes(false, $attribute) ?: $objectResolver->reflectedClass->getAttributes(true, $attribute);
if ($attributeList) {
return array_map(fn(ReflectedAttribute $ref) => $ref->object, $attributeList);
}
return [];
}
protected function taxusGrantPermission(array $attributeList, object $user = null) : bool
{
foreach ($attributeList as $item) {
if ( $grant = $this->taxus->granted($item->privilege, $user, $item) ) {
if (is_bool($grant) ? $grant : $grant === TaxusGrantEnum::Authorized) {
return true;
}
}
}
return false;
}
}