5
5
use Illuminate \Foundation \Application ;
6
6
use Illuminate \Routing \Router ;
7
7
use OpenApi \Annotations \Operation ;
8
+ use OpenApi \Annotations \Parameter ;
8
9
use Radebatz \OpenApi \Routing \RoutingAdapterInterface ;
9
10
10
11
/**
@@ -21,7 +22,10 @@ class LaravelRoutingAdapter implements RoutingAdapterInterface
21
22
public function __construct (Application $ app , array $ options = [])
22
23
{
23
24
$ this ->app = $ app ;
24
- $ this ->options = $ options + [self ::OPTIONS_NAMESPACE => 'App \\Http \\Controllers \\' ];
25
+ $ this ->options = $ options + [
26
+ self ::OPTIONS_AUTO_REGEX => true ,
27
+ self ::OPTIONS_NAMESPACE => 'App \\Http \\Controllers \\' ,
28
+ ];
25
29
}
26
30
27
31
/**
@@ -30,15 +34,36 @@ public function __construct(Application $app, array $options = [])
30
34
public function register (Operation $ operation , string $ controller , array $ parameters , array $ custom ): void
31
35
{
32
36
$ path = $ operation ->path ;
37
+
38
+ $ where = [];
39
+ /** @var Parameter $parameter */
40
+ foreach ($ parameters as $ name => $ parameter ) {
41
+ if (!$ parameter ['required ' ]) {
42
+ if (false !== strpos ($ path , $ needle = "/{ {$ name }} " )) {
43
+ $ path = str_replace ("/{ {$ name }} " , "/{ {$ name }?} " , $ path );
44
+ }
45
+ }
46
+
47
+ switch ($ parameter ['type ' ]) {
48
+ case 'regex ' :
49
+ if ($ pattern = $ parameter ['pattern ' ]) {
50
+ $ where [$ name ] = $ pattern ;
51
+ }
52
+ break ;
53
+
54
+ case 'integer ' :
55
+ if ($ this ->options [self ::OPTIONS_AUTO_REGEX ]) {
56
+ $ where [$ name ] = '[0-9]+ ' ;
57
+ }
58
+ break ;
59
+ }
60
+ }
61
+
33
62
$ controller = str_replace ('::__invoke ' , '' , $ controller );
34
63
if ($ namespace = $ this ->options [self ::OPTIONS_NAMESPACE ]) {
35
64
$ controller = str_replace ($ namespace , '' , $ controller );
36
65
}
37
66
38
- foreach ($ parameters as $ parameter ) {
39
- // TODO
40
- }
41
-
42
67
/** @var Router $router */
43
68
$ router = $ this ->app ->get ('router ' );
44
69
@@ -49,8 +74,10 @@ public function register(Operation $operation, string $controller, array $parame
49
74
$ action ['as ' ] = $ custom [static ::X_NAME ];
50
75
}
51
76
52
- $ route = $ router ->addRoute (strtoupper ($ operation ->method ), $ path , $ action );
53
- $ route ->middleware ($ custom [static ::X_MIDDLEWARE ]);
77
+ $ router
78
+ ->addRoute (strtoupper ($ operation ->method ), $ path , $ action )
79
+ ->middleware ($ custom [static ::X_MIDDLEWARE ])
80
+ ->where ($ where );
54
81
}
55
82
56
83
/**
0 commit comments