@@ -21,6 +21,7 @@ final class FastRoute
2121 * @param class-string<DataGenerator> $dataGenerator
2222 * @param class-string<Dispatcher> $dispatcher
2323 * @param class-string<ConfigureRoutes> $routesConfiguration
24+ * @param class-string<GenerateUri> $uriGenerator
2425 * @param Cache|class-string<Cache>|null $cacheDriver
2526 * @param non-empty-string|null $cacheKey
2627 */
@@ -30,6 +31,7 @@ private function __construct(
3031 private readonly string $ dataGenerator ,
3132 private readonly string $ dispatcher ,
3233 private readonly string $ routesConfiguration ,
34+ private readonly string $ uriGenerator ,
3335 private readonly Cache |string |null $ cacheDriver ,
3436 private readonly ?string $ cacheKey ,
3537 ) {
@@ -47,6 +49,7 @@ public static function recommendedSettings(Closure $routeDefinitionCallback, str
4749 DataGenerator \MarkBased::class,
4850 Dispatcher \MarkBased::class,
4951 RouteCollector::class,
52+ GenerateUri \FromProcessedConfiguration::class,
5053 FileCache::class,
5154 $ cacheKey ,
5255 );
@@ -60,6 +63,7 @@ public function disableCache(): self
6063 $ this ->dataGenerator ,
6164 $ this ->dispatcher ,
6265 $ this ->routesConfiguration ,
66+ $ this ->uriGenerator ,
6367 null ,
6468 null ,
6569 );
@@ -77,6 +81,7 @@ public function withCache(Cache|string $driver, string $cacheKey): self
7781 $ this ->dataGenerator ,
7882 $ this ->dispatcher ,
7983 $ this ->routesConfiguration ,
84+ $ this ->uriGenerator ,
8085 $ driver ,
8186 $ cacheKey ,
8287 );
@@ -114,6 +119,22 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher):
114119 $ dataGenerator ,
115120 $ dispatcher ,
116121 $ this ->routesConfiguration ,
122+ $ this ->uriGenerator ,
123+ $ this ->cacheDriver ,
124+ $ this ->cacheKey ,
125+ );
126+ }
127+
128+ /** @param class-string<GenerateUri> $uriGenerator */
129+ public function withUriGenerator (string $ uriGenerator ): self
130+ {
131+ return new self (
132+ $ this ->routeDefinitionCallback ,
133+ $ this ->routeParser ,
134+ $ this ->dataGenerator ,
135+ $ this ->dispatcher ,
136+ $ this ->routesConfiguration ,
137+ $ uriGenerator ,
117138 $ this ->cacheDriver ,
118139 $ this ->cacheKey ,
119140 );
@@ -122,6 +143,10 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher):
122143 /** @return ProcessedData */
123144 private function buildConfiguration (): array
124145 {
146+ if ($ this ->processedConfiguration !== null ) {
147+ return $ this ->processedConfiguration ;
148+ }
149+
125150 $ loader = function (): array {
126151 $ configuredRoutes = new $ this ->routesConfiguration (
127152 new $ this ->routeParser (),
@@ -134,7 +159,7 @@ private function buildConfiguration(): array
134159 };
135160
136161 if ($ this ->cacheDriver === null ) {
137- return $ loader ();
162+ return $ this -> processedConfiguration = $ loader ();
138163 }
139164
140165 assert (is_string ($ this ->cacheKey ));
@@ -143,11 +168,16 @@ private function buildConfiguration(): array
143168 ? new $ this ->cacheDriver ()
144169 : $ this ->cacheDriver ;
145170
146- return $ cache ->get ($ this ->cacheKey , $ loader );
171+ return $ this -> processedConfiguration = $ cache ->get ($ this ->cacheKey , $ loader );
147172 }
148173
149174 public function dispatcher (): Dispatcher
150175 {
151176 return new $ this ->dispatcher ($ this ->buildConfiguration ());
152177 }
178+
179+ public function uriGenerator (): GenerateUri
180+ {
181+ return new $ this ->uriGenerator ($ this ->buildConfiguration ()[2 ]);
182+ }
153183}
0 commit comments