diff --git a/README.md b/README.md index d6cd52b..d1c4c63 100644 --- a/README.md +++ b/README.md @@ -48,13 +48,17 @@ use Dbout\WpRestApi\RouteLoader; // One folder $loader = new RouteLoader(__DIR__ . '/src/Api/Routes'); -$loader->register(); // Multiple folders $loader = new RouteLoader([ __DIR__ . '/themes/my-theme/api' __DIR__ . '/src/Api/Routes', ]); + +// You can also use pattern +$loader = new RouteLoader(__DIR__ . '/src/Modules/*/Api/Routes'); + +$loader->register(); ``` > 💡 The module will automatically search for all classes that are in the folder and sub folder. diff --git a/src/Loaders/AnnotationDirectoryLoader.php b/src/Loaders/AnnotationDirectoryLoader.php index 1daef27..18518b7 100644 --- a/src/Loaders/AnnotationDirectoryLoader.php +++ b/src/Loaders/AnnotationDirectoryLoader.php @@ -52,13 +52,13 @@ function (\SplFileInfo $current): bool { $routes = []; foreach ($files as $file) { - if (!$file->isFile() || !str_ends_with($file->getFilename(), '.php')) { + if (!$file->isFile() || $file->getExtension() !== 'php') { continue; } /** @var class-string|null $class */ $class = $this->findClass($file); - if ($class === null) { + if ($class === null || !class_exists($class)) { continue; } diff --git a/src/RouteLoader.php b/src/RouteLoader.php index 79ca3e0..f07a1d1 100644 --- a/src/RouteLoader.php +++ b/src/RouteLoader.php @@ -60,9 +60,19 @@ protected function getRoutes(): array */ protected function findRoutes(): array { - $directories = is_array($this->routeDirectory) ? $this->routeDirectory : [$this->routeDirectory]; + $tmpDirectories = is_array($this->routeDirectory) ? $this->routeDirectory : [$this->routeDirectory]; $routes = []; + $directories = []; + foreach ($tmpDirectories as $dir) { + $globalDirs = glob($dir); + if (!is_array($globalDirs)) { + continue; + } + + $directories = array_merge($directories, $globalDirs); + } + foreach ($directories as $directory) { $directory = new \SplFileInfo($directory); if (!$directory->isDir()) {