@@ -47,9 +47,17 @@ protected function isRoute(string $name): bool
4747 return $ this ->router ?->getRouteCollection()->get ($ name ) !== null ;
4848 }
4949
50+ protected function getAllRoutes (): array
51+ {
52+ $ routesCollection = $ this ->router ?->getRouteCollection()->all ();
53+
54+ return array_keys ($ routesCollection );
55+ }
56+
5057 /**
5158 * @param array{
5259 * route?: string,
60+ * routes?: string[],
5361 * method?: string[],
5462 * status?: int[],
5563 * level?: string,
@@ -59,11 +67,15 @@ protected function isRoute(string $name): bool
5967 public function parse (string $ name , array $ config ): Filter
6068 {
6169 if (
62- !array_key_exists ('route ' , $ config ) ||
70+ ( !array_key_exists ('route ' , $ config) && ! array_key_exists ( ' routes ' , $ config ) ) ||
6371 !array_key_exists ('method ' , $ config ) ||
6472 !array_key_exists ('status ' , $ config )
6573 ) {
66- throw new ParseException (sprintf ('Undefined "route", "method" or "status" parameter from filter "%s" ' , $ name ));
74+ throw new ParseException (sprintf ('Undefined "route(s)", "method" or "status" parameter from filter "%s" ' , $ name ));
75+ }
76+
77+ if (array_key_exists ('route ' , $ config ) && array_key_exists ('routes ' , $ config )) {
78+ throw new ParseException (sprintf ('You can \'t use both "route" and "routes" parameter from filter "%s" ' , $ name ));
6779 }
6880
6981 if (!array_key_exists ('level ' , $ config )) {
@@ -74,7 +86,14 @@ public function parse(string $name, array $config): Filter
7486 ->setMethod ($ config ['method ' ])
7587 ->setStatus ($ config ['status ' ]);
7688
77- $ this ->parseRoute ($ filter , $ config ['route ' ]);
89+ if (array_key_exists ('route ' , $ config )) {
90+ $ this ->parseRoute ($ filter , $ config ['route ' ]);
91+ }
92+
93+ if (array_key_exists ('routes ' , $ config )) {
94+ $ this ->parseRoutes ($ filter , $ config ['routes ' ] ?? []);
95+ }
96+
7897 $ this ->parseLevel ($ filter , $ config ['level ' ]);
7998
8099 return $ filter ->setOptions ($ config ['options ' ] ?? []);
@@ -89,6 +108,42 @@ protected function parseRoute(Filter $filter, ?string $route): void
89108 $ filter ->setRoute ($ route );
90109 }
91110
111+ protected function parseRoutes (Filter $ filter , ?array $ routes ): void
112+ {
113+ if (empty ($ routes )) {
114+ $ filter ->setRoutes (['all ' ]);
115+
116+ return ;
117+ }
118+
119+ // Find and keep excluded routes
120+ $ excludedRoutes = array_filter ($ routes , function (string $ route ) {
121+ return str_starts_with ($ route , '! ' );
122+ });
123+
124+ // Create an array with routes not excluded
125+ $ routes = array_diff_key ($ routes , $ excludedRoutes );
126+
127+ // Check that the route's name exist
128+ foreach ($ routes as $ route ) {
129+ if (!$ this ->isRoute ($ route )) {
130+ throw new ParseException (sprintf ('Undefined route "%s" from router service ' , $ route ));
131+ }
132+ }
133+
134+ // If empty routes, return all routes except the excluded ones
135+ if (empty ($ routes )) {
136+ $ existingRoutes = $ this ->getAllRoutes ();
137+ $ excludedRoutes = array_map (function (string $ route ) {
138+ return ltrim ($ route , '! ' );
139+ }, $ excludedRoutes );
140+
141+ $ routes = array_values (array_diff ($ existingRoutes , $ excludedRoutes ));
142+ }
143+
144+ $ filter ->setRoutes ($ routes );
145+ }
146+
92147 protected function parseLevel (Filter $ filter , ?string $ level ): void
93148 {
94149 if (!in_array ($ level , $ this ->allowedLevels , true )) {
0 commit comments