Skip to content

Commit

Permalink
Make Prefix attribute repeatable
Browse files Browse the repository at this point in the history
Mark the `Prefix` attribute as repeatable and update the `ClassRouteAttributes` class to support multiple prefixes.

* **Prefix Attribute**:
  - Mark the `Prefix` attribute in `src/Attributes/Prefix.php` as repeatable by adding `Attribute::IS_REPEATABLE`.

* **ClassRouteAttributes**:
  - Update the `prefix` method in `src/ClassRouteAttributes.php` to return an array of prefixes.
  - Update the `groups` method in `src/ClassRouteAttributes.php` to handle multiple prefixes.

* **Tests**:
  - Add a test for multiple prefixes in `tests/AttributeTests/PrefixAttributeTest.php`.
  - Add multiple `Prefix` attributes to the `PrefixTestController` class in `tests/TestClasses/Controllers/PrefixTestController.php`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/spatie/laravel-route-attributes?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
eabay committed Jan 8, 2025
1 parent c3b21a0 commit 280caee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Attributes/Prefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Prefix implements RouteAttribute
{
public function __construct(
Expand Down
25 changes: 16 additions & 9 deletions src/ClassRouteAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ public function __construct(
/**
* @psalm-suppress NoInterfaceProperties
*/
public function prefix(): ?string
public function prefix(): array
{
/** @var Prefix $attribute */
if (! $attribute = $this->getAttribute(Prefix::class)) {
return null;
$prefixes = [];

/** @var ReflectionClass[] $attributes */
$attributes = $this->class->getAttributes(Prefix::class, \ReflectionAttribute::IS_INSTANCEOF);
foreach ($attributes as $attribute) {
$attributeClass = $attribute->newInstance();
$prefixes[] = $attributeClass->prefix;
}

return $attribute->prefix;
return $prefixes;
}

/**
Expand Down Expand Up @@ -81,10 +85,13 @@ public function groups(): array
]);
}
} else {
$groups[] = array_filter([
'domain' => $this->domainFromConfig() ?? $this->domain(),
'prefix' => $this->prefix(),
]);
$prefixes = $this->prefix();
foreach ($prefixes as $prefix) {
$groups[] = array_filter([
'domain' => $this->domainFromConfig() ?? $this->domain(),
'prefix' => $prefix,
]);
}
}

return $groups;
Expand Down
41 changes: 41 additions & 0 deletions tests/AttributeTests/PrefixAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,45 @@ public function it_can_apply_a_prefix_on_the_url_of_every_method()
uri: 'my-prefix/my-post-method',
);
}

/** @test */
public function it_can_apply_multiple_prefixes_on_the_url_of_every_method()
{
$this->routeRegistrar->registerClass(PrefixTestController::class);

$this
->assertRegisteredRoutesCount(6)
->assertRouteRegistered(
PrefixTestController::class,
controllerMethod: 'myRootGetMethod',
uri: 'my-prefix',
)
->assertRouteRegistered(
PrefixTestController::class,
controllerMethod: 'myGetMethod',
uri: 'my-prefix/my-get-method',
)
->assertRouteRegistered(
PrefixTestController::class,
controllerMethod: 'myPostMethod',
httpMethods: 'post',
uri: 'my-prefix/my-post-method',
)
->assertRouteRegistered(
PrefixTestController::class,
controllerMethod: 'myRootGetMethod',
uri: 'my-second-prefix',
)
->assertRouteRegistered(
PrefixTestController::class,
controllerMethod: 'myGetMethod',
uri: 'my-second-prefix/my-get-method',
)
->assertRouteRegistered(
PrefixTestController::class,
controllerMethod: 'myPostMethod',
httpMethods: 'post',
uri: 'my-second-prefix/my-post-method',
);
}
}
1 change: 1 addition & 0 deletions tests/TestClasses/Controllers/PrefixTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\RouteAttributes\Attributes\Prefix;

#[Prefix('my-prefix')]
#[Prefix('my-second-prefix')]
class PrefixTestController
{
#[Get('/')]
Expand Down

0 comments on commit 280caee

Please sign in to comment.