Skip to content

Commit

Permalink
Merge pull request #10 from robertdrakedennis/master
Browse files Browse the repository at this point in the history
Feature: Domain attribute
  • Loading branch information
freekmurze committed Nov 30, 2020
2 parents 2b31115 + aabb2e3 commit 99a3701
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
Expand Up @@ -186,6 +186,7 @@ You can use the `Prefix` annotation on a class to prefix the routes of all metho

```php
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Prefix;

#[Prefix('my-prefix')]
Expand All @@ -210,6 +211,37 @@ Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']);
Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']);
```

### Specifying a domain

You can use the `Domain` annotation on a class to prefix the routes of all methods of that class.

```php
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Domain;

#[Domain('my-subdomain.localhost')]
class MyController
{
#[Get('my-get-route')]
public function myGetMethod()
{
}

#[Post('my-post-route')]
public function myPostMethod()
{
}
}
```

These annotations will automatically register these routes:

```php
Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost');
Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost');
```

## Testing

``` bash
Expand Down
13 changes: 13 additions & 0 deletions src/Attributes/Domain.php
@@ -0,0 +1,13 @@
<?php

namespace Spatie\RouteAttributes\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class Domain implements RouteAttribute
{
public function __construct(
public string $domain
) {}
}
14 changes: 14 additions & 0 deletions src/ClassRouteAttributes.php
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\RouteAttributes;

use ReflectionClass;
use Spatie\RouteAttributes\Attributes\Domain;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Prefix;
use Spatie\RouteAttributes\Attributes\RouteAttribute;
Expand All @@ -29,6 +30,19 @@ public function prefix(): ?string
return $attribute->prefix;
}

/**
* @psalm-suppress NoInterfaceProperties
*/
public function domain(): ?string
{
/** @var \Spatie\RouteAttributes\Attributes\Domain $attribute */
if (! $attribute = $this->getAttribute(Domain::class)) {
return null;
}

return $attribute->domain;
}

/**
* @psalm-suppress NoInterfaceProperties
*/
Expand Down
4 changes: 4 additions & 0 deletions src/RouteRegistrar.php
Expand Up @@ -116,6 +116,10 @@ protected function processAttributes(string $className): void
$route
->name($attributeClass->name);

if ($domain = $classRouteAttributes->domain()) {
$route->domain($domain);
}

if ($prefix = $classRouteAttributes->prefix()) {
$route->prefix($prefix);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/AttributeTests/DomainAttributeTest.php
@@ -0,0 +1,31 @@
<?php

namespace Spatie\RouteAttributes\Tests\AttributeTests;

use Spatie\RouteAttributes\Tests\TestCase;
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\DomainTestController;

class DomainAttributeTest extends TestCase
{
/** @test */
public function it_can_apply_a_domain_on_the_url_of_every_method()
{
$this->routeRegistrar->registerClass(DomainTestController::class);

$this
->assertRegisteredRoutesCount(2)
->assertRouteRegistered(
DomainTestController::class,
controllerMethod: 'myGetMethod',
uri: 'my-get-method',
domain: 'my-subdomain.localhost'
)
->assertRouteRegistered(
DomainTestController::class,
controllerMethod: 'myPostMethod',
httpMethod: 'post',
uri: 'my-post-method',
domain: 'my-subdomain.localhost'
);
}
}
7 changes: 6 additions & 1 deletion tests/TestCase.php
Expand Up @@ -52,13 +52,14 @@ public function assertRouteRegistered(
string $uri = 'my-method',
string|array $middleware = [],
?string $name = null,
?string $domain = null,
): self {
if (! is_array($middleware)) {
$middleware = Arr::wrap($middleware);
}

$routeRegistered = collect($this->getRouteCollection()->getRoutes())
->contains(function (Route $route) use ($name, $middleware, $controllerMethod, $controller, $uri, $httpMethod) {
->contains(function (Route $route) use ($name, $middleware, $controllerMethod, $controller, $uri, $httpMethod, $domain) {
if (! in_array(strtoupper($httpMethod), $route->methods)) {
return false;
}
Expand All @@ -83,6 +84,10 @@ public function assertRouteRegistered(
return false;
}

if ($route->getDomain() !== $domain) {
return false;
}

return true;
});

Expand Down
21 changes: 21 additions & 0 deletions tests/TestClasses/Controllers/DomainTestController.php
@@ -0,0 +1,21 @@
<?php

namespace Spatie\RouteAttributes\Tests\TestClasses\Controllers;

use Spatie\RouteAttributes\Attributes\Domain;
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;

#[Domain('my-subdomain.localhost')]
class DomainTestController
{
#[Get('my-get-method')]
public function myGetMethod()
{
}

#[Post('my-post-method')]
public function myPostMethod()
{
}
}

0 comments on commit 99a3701

Please sign in to comment.