From c4d22bbe7d48e75b6e742efa3b6291f4f84155b5 Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Mon, 7 Sep 2020 05:10:21 -0500 Subject: [PATCH] Fix error in Router; implement new annotation lookup in App. --- src/App.php | 98 ++++--------------------------------------- src/Router/Router.php | 13 ++++-- 2 files changed, 16 insertions(+), 95 deletions(-) diff --git a/src/App.php b/src/App.php index 8d1c082..c7dde36 100644 --- a/src/App.php +++ b/src/App.php @@ -490,7 +490,6 @@ class App } } - // @todo MB: This entire function is refactor-bait. protected function interrogateControllers(): void { if ($this->interrogateControllersComplete) { @@ -505,96 +504,13 @@ class App return; } - $controllerPaths = [ - APP_ROOT.'/src/Controllers', - ]; - - foreach ($controllerPaths as $controllerPath) { - if ((new Filesystem())->exists($controllerPath)) { - foreach (new \DirectoryIterator($controllerPath) as $controllerFile) { - if (!$controllerFile->isDot() && $controllerFile->isFile() && $controllerFile->isReadable()) { - $appClass = new \ReflectionClass(get_called_class()); - $expectedClasses = [ - $appClass->getNamespaceName().'\\Controllers\\'.str_replace('.php', '', $controllerFile->getFilename()), - 'Benzine\\Controllers\\'.str_replace('.php', '', $controllerFile->getFilename()), - ]; - foreach ($expectedClasses as $expectedClass) { - if (class_exists($expectedClass)) { - $rc = new \ReflectionClass($expectedClass); - if (!$rc->isAbstract()) { - foreach ($rc->getMethods() as $method) { - /** @var \ReflectionMethod $method */ - if (true || ResponseInterface::class == ($method->getReturnType() instanceof \ReflectionType ? $method->getReturnType()->getName() : null)) { - $docBlock = $method->getDocComment(); - $newRoute = new Route($this->logger); - foreach (explode("\n", $docBlock) as $docBlockRow) { - if (false !== stripos($docBlockRow, '@route')) { - $route = trim(substr( - $docBlockRow, - (stripos($docBlockRow, '@route') + strlen('@route')) - )); - - @list($httpMethods, $path, $extra) = explode(' ', $route, 3); - $httpMethods = explode(',', strtoupper($httpMethods)); - - $options = []; - $defaultOptions = [ - 'access' => Route::ACCESS_PUBLIC, - 'weight' => 100, - ]; - - // @todo MB: The following if-statement in its entirity needs rewriting - // because only god and me knew what I was thinking when I wrote it. - // And now god only knows. - if (isset($extra)) { - foreach (explode(' ', $extra) as $item) { - @list($extraK, $extraV) = explode('=', $item, 2); - if (!isset($extraV)) { - $extraV = true; - } - $options[$extraK] = $extraV; - } - } - - $options = array_merge($defaultOptions, $options); - foreach ($httpMethods as $httpMethod) { - $newRoute - ->setHttpMethod($httpMethod) - ->setRouterPattern('/'.ltrim($path, '/')) - ->setCallback($method->class.':'.$method->name) - ; - - foreach ($options as $key => $value) { - $keyMethod = 'set'.ucfirst($key); - if (method_exists($newRoute, $keyMethod)) { - $newRoute->{$keyMethod}($value); - } else { - $newRoute->setArgument($key, $value); - } - } - - $this->router->addRoute($newRoute); - } - } elseif (false !== stripos($docBlockRow, '@domains')) { - $domains = explode(' ', trim(substr( - $docBlockRow, - (stripos($docBlockRow, '@domains') + strlen('@domains')) - ))); - foreach ($domains as $domain) { - $newRoute->addValidDomain($domain); - } - $newRoute->setWeight($newRoute->getWeight() - 10); - } - } - } - } - } - } - } - } - } - } - } + $appClass = new \ReflectionClass(get_called_class()); + $this->router->loadRoutesFromAnnotations( + [ + APP_ROOT.'/src/Controllers', + ], + $appClass->getNamespaceName() + ); $this->router ->weighRoutes() diff --git a/src/Router/Router.php b/src/Router/Router.php index 7311246..bb324c0 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -33,11 +33,16 @@ class Router $reader = new AnnotationReader(); foreach ($controllerPaths as $controllerPath) { - foreach (new \RecursiveDirectoryIterator($controllerPath) as $controllerFile) { - if ($controllerFile->isDot() || !$controllerFile->isFile() || !$controllerFile->isReadable()) { - continue; - } + if (!is_dir($controllerPath)) { + continue; + } + $dirIterator = new \RecursiveDirectoryIterator($controllerPath); + $iteratorIterator = new \RecursiveIteratorIterator($dirIterator); + $phpFiles = new \RegexIterator($iteratorIterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH); + + foreach ($phpFiles as $controllerFile) { + /** @var \SplFileInfo $controllerFile */ $fileClassName = str_replace('.php', '', $controllerFile->getFilename()); $expectedClasses = [ $baseNamespace . '\\Controllers\\' . $fileClassName,