Skip to content

Commit

Permalink
Merge pull request #125 from ejunker/filename-patterns
Browse files Browse the repository at this point in the history
Add patterns and not_patterns options to directories
  • Loading branch information
freekmurze committed Aug 9, 2023
2 parents c8cc243 + cb8d093 commit 3c9f932
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/route-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [],
],
*/
],
Expand Down
4 changes: 2 additions & 2 deletions src/RouteAttributesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
25 changes: 25 additions & 0 deletions tests/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3c9f932

Please sign in to comment.