Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with routes middleware and add posibility to add routes arguments by annotation #2

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/AnnotationRouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ private function createAnnotationRoute(array $annotationRoute): void
$route->setName( $annotationRoute[ 'name' ] );
}

if ( !empty($annotationRoute[ 'arguments' ])) {
$route->setArguments( $annotationRoute[ 'arguments' ] );
}

if ( $annotationRoute[ 'middleware' ] !== [] && $this->container instanceof ContainerInterface ) {
foreach ( $annotationRoute[ 'middleware' ] as $middlewareName ) {
$route->addMiddleware( $this->container->get( $middlewareName ) );
Expand Down
4 changes: 4 additions & 0 deletions src/Annotations/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ class Route

/** @var string */
public $description = '';

/** @var string[] */
public $arguments = [];

}
5 changes: 3 additions & 2 deletions src/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function load(string $className): array
}

foreach ($middleware as $middlewareName) {
foreach ($routes as $route) {
$route['middleware'][] = $middlewareName;
foreach ($routes as $i => $route) {
$routes[$i]['middleware'][] = $middlewareName;
}
}

Expand Down Expand Up @@ -117,6 +117,7 @@ private function getRoute(string $class, string $action, string $routePrefix, ar
return [
'name' => $annotation->name,
'methods' => $annotation->methods,
'arguments' => $annotation->arguments,
'pattern' => $routePrefix . $annotation->value,
'class' => $class,
'action' => $action,
Expand Down