diff --git a/README.md b/README.md index 3bb6f85..ada5bc2 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,23 @@ For controllers outside the applications root namespace directories can also be ], ``` +If you are using a directory structure where you co-locate multiple types of files in the same directory and want to +be more specific about which files are checked for route attributes, you can use the `patterns` and `not_patterns` +options. For example, if you are co-locating your tests with your controllers you could use the `patterns` option to only +look in controller files, or you could use `not_patterns` to configure it to not look in test files for route +attributes. + +```php +'directories' => [ + base_path('app-modules/Blog') => [ + // only register routes in files that match the patterns + 'patterns' => ['*Controller.php'], + // do not register routes in files that match the patterns + 'not_patterns => ['*Test.php'], + ], +], +``` + ## Usage The package provides several annotations that should be put on controller classes and methods. These annotations will be used to automatically register routes diff --git a/config/route-attributes.php b/config/route-attributes.php index c1bca38..b640312 100644 --- a/config/route-attributes.php +++ b/config/route-attributes.php @@ -18,6 +18,10 @@ app_path('Http/Controllers/Api') => [ 'prefix' => 'api', 'middleware' => 'api', + // only register routes in files that match the patterns + 'patterns' => ['*Controller.php'], + // do not register routes in files that match the patterns + 'not_patterns => [], ], */ ], diff --git a/src/RouteAttributesServiceProvider.php b/src/RouteAttributesServiceProvider.php index 9f00d26..6ce7b7b 100644 --- a/src/RouteAttributesServiceProvider.php +++ b/src/RouteAttributesServiceProvider.php @@ -34,12 +34,12 @@ protected function registerRoutes(): void collect($this->getRouteDirectories())->each(function (string|array $directory, string|int $namespace) use ($routeRegistrar) { if (is_array($directory)) { - $options = Arr::except($directory, ['namespace', 'base_path']); + $options = Arr::except($directory, ['namespace', 'base_path', 'patterns', 'not_patterns']); $routeRegistrar ->useRootNamespace($directory['namespace'] ?? app()->getNamespace()) ->useBasePath($directory['base_path'] ?? (isset($directory['namespace']) ? $namespace : app()->path())) - ->group($options, fn () => $routeRegistrar->registerDirectory($namespace)); + ->group($options, fn () => $routeRegistrar->registerDirectory($namespace, $directory['patterns'] ?? [], $directory['not_patterns'] ?? [])); } else { is_string($namespace) ? $routeRegistrar diff --git a/src/RouteRegistrar.php b/src/RouteRegistrar.php index bed71e9..0d3ed13 100644 --- a/src/RouteRegistrar.php +++ b/src/RouteRegistrar.php @@ -67,11 +67,12 @@ public function middleware(): array return $this->middleware ?? []; } - public function registerDirectory(string | array $directories): void + public function registerDirectory(string | array $directories, array $patterns = [], array $notPatterns = []): void { $directories = Arr::wrap($directories); + $patterns = $patterns ?: ['*.php']; - $files = (new Finder())->files()->name('*.php')->in($directories)->sortByName(); + $files = (new Finder())->files()->in($directories)->name($patterns)->notName($notPatterns)->sortByName(); collect($files)->each(fn (SplFileInfo $file) => $this->registerFile($file)); } diff --git a/tests/RouteRegistrarTest.php b/tests/RouteRegistrarTest.php index a757cdd..0a8538a 100644 --- a/tests/RouteRegistrarTest.php +++ b/tests/RouteRegistrarTest.php @@ -82,4 +82,29 @@ public function the_registrar_can_register_a_directory_with_defined_namespace() controllerMethod: 'thirdPartyGetMethod', ); } + + /** @test */ + public function the_registrar_can_register_a_directory_with_filename_pattern() + { + $this + ->routeRegistrar + ->registerDirectory($this->getTestPath('TestClasses/Controllers/RouteRegistrar'), ['*FirstController.php']); + + $this->assertRegisteredRoutesCount(1); + + $this->assertRouteRegistered( + RegistrarTestFirstController::class, + uri: 'first-method', + ); + } + + /** @test */ + public function the_registrar_can_register_a_directory_with_filename_not_pattern() + { + $this + ->routeRegistrar + ->registerDirectory($this->getTestPath('TestClasses/Controllers/RouteRegistrar'), [], ['*FirstController.php']); + + $this->assertRegisteredRoutesCount(2); + } }