diff --git a/src/Breadcrumb/Attribute/Method/Breadcrumb.php b/src/Breadcrumb/Attribute/Method/Breadcrumb.php new file mode 100644 index 0000000..643f530 --- /dev/null +++ b/src/Breadcrumb/Attribute/Method/Breadcrumb.php @@ -0,0 +1,24 @@ +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; + } + }; + + 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; + } +} diff --git a/src/Cli/Attribute/Command.php b/src/Cli/Attribute/Command.php new file mode 100644 index 0000000..63fd9c0 --- /dev/null +++ b/src/Cli/Attribute/Command.php @@ -0,0 +1,17 @@ +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, false, true, false, false); + + $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"; + } +} diff --git a/src/Cronard/Attribute/Method/Cronard.php b/src/Cronard/Attribute/Method/Cronard.php new file mode 100644 index 0000000..05b39ab --- /dev/null +++ b/src/Cronard/Attribute/Method/Cronard.php @@ -0,0 +1,11 @@ +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, true, true, false, true); + + foreach($objectResolver->getAttributeListFromClassname( $this->annotations['method'], 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"; + } +} diff --git a/src/Event/Attribute/Method/Event.php b/src/Event/Attribute/Method/Event.php new file mode 100644 index 0000000..b90f5e5 --- /dev/null +++ b/src/Event/Attribute/Method/Event.php @@ -0,0 +1,10 @@ +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->getAttributeListFromClassname( $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"; + } +} diff --git a/src/Route/Attribute/Method/Route.php b/src/Route/Attribute/Method/Route.php new file mode 100644 index 0000000..3452b7b --- /dev/null +++ b/src/Route/Attribute/Method/Route.php @@ -0,0 +1,37 @@ +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; + } +} diff --git a/src/Route/Attribute/Object/Route.php b/src/Route/Attribute/Object/Route.php new file mode 100644 index 0000000..508ae23 --- /dev/null +++ b/src/Route/Attribute/Object/Route.php @@ -0,0 +1,11 @@ +cache = $cache; + + if ($callback !== null) { + $this->callback = $callback; + } + + if ($folderList !== null) { + $this->folderList = $folderList; + } + + if ($annotations !== null) { + $this->annotations = $annotations; + } + else { + $this->annotations = [ + '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 $annotations = null) : array + { + $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; + } + + $base = ""; + $class = $this->generateClassname($file->getBasename(".php"), $namespace); + $methods = $this->defaultMethods; + + $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; + } + elseif ($object->method ?? false ) { + $methods = (array) $object->method; + } + + $base = $object->base ?? ""; + } + } + + if ( isset($annotations['method']) ) { + $routeList = $objectResolver->getAttributeListFromClassname( $annotations['method'], false ); + + foreach($routeList as $func => $routes) { + if (is_array($routes)) { + foreach ($routes as $route) { + $route->base = $base; + $route->class = $class; + $route->classMethod = $func; + + if (false === ($route->methods ?? false)) { + $route->methods = $methods; + } + + if (false !== ($this->callback ?? false)) { + call_user_func_array($this->callback, [$route]); + } + + $list[] = $route; + } + } + } + } + } + + return $list; + }); + } + + protected function generateClassname($file, $namespace) + { + return "\\$namespace\\$file"; + } +} diff --git a/src/Security/Attribute/Security.php b/src/Security/Attribute/Security.php new file mode 100644 index 0000000..2289d06 --- /dev/null +++ b/src/Security/Attribute/Security.php @@ -0,0 +1,11 @@ +redirectResponse = $redirectResponse; + $this->unauthorizeResponse = $unauthorizeResponse; + $this->taxus = $taxus; + } + + public function verify(string $className, string $methodName) : ? ResponseInterface + { + return $this->isLocked($className, $methodName) ? $this->redirectResponse : null; + } + + public function isLocked(string $className, string $methodName) : bool + { + if ( $security = $this->getClassAttributes(Attribute\Security::class, $className, $methodName) ) { + return array_pop($security)->locked; + } + + return true; + } + + public function taxus(string $className, string $methodName, object $user = null) : ? ResponseInterface + { + if ($taxus = $this->getClassAttributes(Attribute\Taxus::class, $className, $methodName)) { + if ($this->unauthorizeResponse) { + foreach ($taxus as $item) { + if (!isset($item->privilege) || $this->taxus->granted($item->privilege, $user, $item)) { + return null; + } + } + + return call_user_func_array($this->unauthorizeResponse, [ $user, $taxus, $className, $methodName ]); + } + else { + throw new \ErrorException("Unauthorized response given."); + } + } + + return null; + } + + protected function getClassAttributes(string $annotationClass, string $className, string $methodName)/* : \Notes\Attribute|array */ + { + $objectResolver = new ObjectResolver($className, true, true, false, true); + + try { + $method = $objectResolver->getAttributeListFromClassname( $annotationClass, false ); + } + catch(\Exception $e) { } + + if ( $method[$methodName] ?? false ) { + return $method[$methodName]; + } + else { + return array_filter($method, fn($e) => is_object($e)); + } + } +}