Skip to content

Commit

Permalink
added support to withoutMiddleware in route-attribute (#11)
Browse files Browse the repository at this point in the history
* added withouMiddleware route-attribute support (#10)

* added documentation about on withoutMiddleware (#10)
  • Loading branch information
addeeandra authored Oct 4, 2023
1 parent c9a88e8 commit 3dc7a20
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 7 deletions.
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ Dentro\Yalr\Attributes\Middleware(string | array $middleware);

#### Available Method Target
```php
Dentro\Yalr\Attributes\Get(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Post(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Put(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Patch(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Delete(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Options(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Delete(string $uri, ?string $name = null, array | string $middleware = []);
Dentro\Yalr\Attributes\Get(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
Dentro\Yalr\Attributes\Post(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
Dentro\Yalr\Attributes\Put(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
Dentro\Yalr\Attributes\Patch(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
Dentro\Yalr\Attributes\Delete(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
Dentro\Yalr\Attributes\Options(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
Dentro\Yalr\Attributes\Delete(string $uri, ?string $name = null, array | string $middleware = [], array | string $withoutMiddleware = []);
```

#### Added To Configuration Route
Expand Down
2 changes: 2 additions & 0 deletions src/Attributes/Any.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: Router::$verbs,
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware,
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: 'DELETE',
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: ['GET', 'HEAD'],
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: 'OPTIONS',
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: 'PATCH',
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: 'POST',
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
method: 'PUT',
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
3 changes: 3 additions & 0 deletions src/Attributes/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
class Route implements RouteAttribute
{
public array $middleware;
public array $withoutMiddleware;

public function __construct(
public array | string $method,
public string $uri,
public ?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
$this->middleware = Arr::wrap($middleware);
$this->withoutMiddleware = Arr::wrap($withoutMiddleware);
}
}
17 changes: 17 additions & 0 deletions src/Attributes/WithoutMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dentro\Yalr\Attributes;

use Attribute;
use Illuminate\Support\Arr;

#[Attribute(Attribute::TARGET_CLASS)]
class WithoutMiddleware implements RouteAttribute
{
public array $withoutMiddleware = [];

public function __construct(string | array $middleware = [])
{
$this->withoutMiddleware = Arr::wrap($middleware);
}
}
11 changes: 11 additions & 0 deletions src/BaseRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ abstract class BaseRoute implements Bindable, Registerable
*/
protected array|string $middleware;

/**
* Middleware excluded in route.
*
* @var array|string
*/
protected array|string $withoutMiddleware;

/**
* Route for specific domain.
*
Expand Down Expand Up @@ -172,6 +179,10 @@ public function getRouteGroupOptions(): array
$options['middleware'] = $this->middleware;
}

if (isset($this->withoutMiddleware) && !empty($this->withoutMiddleware)) {
$options['withoutMiddleware'] = $this->withoutMiddleware;
}

if (isset($this->domain) && !empty($this->domain)) {
$options['domain'] = $this->domain;
}
Expand Down
10 changes: 10 additions & 0 deletions src/RouteAttributeRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dentro\Yalr;

use Dentro\Yalr\Attributes\WithoutMiddleware;
use Illuminate\Routing\RouteRegistrar;
use Dentro\Yalr\Attributes\Domain;
use Dentro\Yalr\Attributes\Middleware;
Expand Down Expand Up @@ -46,6 +47,10 @@ public function registerClass(string $className): void
break;
case $attribute instanceof Middleware:
$carry['middleware'] = $attribute->middleware;
break;
case $attribute instanceof WithoutMiddleware:
$carry['withoutMiddleware'] = $attribute->withoutMiddleware;
break;
}

return $carry;
Expand Down Expand Up @@ -76,6 +81,7 @@ protected function registerMethod(ReflectionClass $class): void
$uri = $attributeInstance->uri;
$name = $attributeInstance->name;
$middleware = $attributeInstance->middleware;
$withoutMiddleware = $attributeInstance->withoutMiddleware;

if (!empty($name)) {
$this->name($name);
Expand All @@ -85,6 +91,10 @@ protected function registerMethod(ReflectionClass $class): void
$this->middleware($middleware);
}

if (!empty($withoutMiddleware)) {
$this->withoutMiddleware($withoutMiddleware);
}

$this->router->addRoute($httpMethods, $uri, $this->compileAction($action));

// reset attributes after registered
Expand Down

0 comments on commit 3dc7a20

Please sign in to comment.