44namespace FastRoute ;
55
66use Closure ;
7- use FastRoute \Cache \FileCache ;
87
98use function assert ;
109use function is_string ;
@@ -16,123 +15,16 @@ final class FastRoute
1615 private ?array $ processedConfiguration = null ;
1716
1817 /**
19- * @param Closure(ConfigureRoutes):void $routeDefinitionCallback
20- * @param class-string<DataGenerator> $dataGenerator
21- * @param class-string<Dispatcher> $dispatcher
22- * @param class-string<ConfigureRoutes> $routesConfiguration
23- * @param class-string<GenerateUri> $uriGenerator
24- * @param Cache|class-string<Cache>|null $cacheDriver
25- * @param non-empty-string|null $cacheKey
18+ * @param Closure(ConfigureRoutes):void $routeDefinitionCallback
19+ * @param non-empty-string|null $cacheKey
2620 */
2721 public function __construct (
2822 private Closure $ routeDefinitionCallback ,
29- private DataGenerator $ dataGenerator ,
30- private Dispatcher $ dispatcher ,
31- private ConfigureRoutes $ routesConfiguration ,
32- private GenerateUri $ uriGenerator ,
33- private ?Cache $ cacheDriver ,
3423 private ?string $ cacheKey ,
24+ private Settings $ settings = new FastSettings (),
3525 ) {
3626 }
3727
38- /**
39- * @param Closure(ConfigureRoutes):void $routeDefinitionCallback
40- * @param non-empty-string $cacheKey
41- */
42- public static function recommendedSettings (Closure $ routeDefinitionCallback , string $ cacheKey ): self
43- {
44- return new self (
45- $ routeDefinitionCallback ,
46- new DataGenerator \MarkBased (),
47- new Dispatcher \MarkBased (),
48- new RouteCollector (new RouteParser \Std ()),
49- new GenerateUri \FromProcessedConfiguration ,
50- new FileCache (),
51- $ cacheKey ,
52- );
53- }
54-
55- public function disableCache (): self
56- {
57- return new self (
58- $ this ->routeDefinitionCallback ,
59- $ this ->dataGenerator ,
60- $ this ->dispatcher ,
61- $ this ->routesConfiguration ,
62- $ this ->uriGenerator ,
63- null ,
64- null ,
65- );
66- }
67-
68- /**
69- * @param Cache|class-string<Cache> $driver
70- * @param non-empty-string $cacheKey
71- */
72- public function withCache (Cache $ driver , string $ cacheKey ): self
73- {
74- return new self (
75- $ this ->routeDefinitionCallback ,
76- $ this ->dataGenerator ,
77- $ this ->dispatcher ,
78- $ this ->routesConfiguration ,
79- $ this ->uriGenerator ,
80- $ driver ,
81- $ cacheKey ,
82- );
83- }
84-
85- public function useCharCountDispatcher (): self
86- {
87- return $ this ->useCustomDispatcher (new DataGenerator \CharCountBased (), new Dispatcher \CharCountBased ());
88- }
89-
90- public function useGroupCountDispatcher (): self
91- {
92- return $ this ->useCustomDispatcher (new DataGenerator \GroupCountBased (), new Dispatcher \GroupCountBased ());
93- }
94-
95- public function useGroupPosDispatcher (): self
96- {
97- return $ this ->useCustomDispatcher (new DataGenerator \GroupPosBased (), new Dispatcher \GroupPosBased ());
98- }
99-
100- public function useMarkDispatcher (): self
101- {
102- return $ this ->useCustomDispatcher (new DataGenerator \MarkBased (), new Dispatcher \MarkBased ());
103- }
104-
105- /**
106- * @param class-string<DataGenerator> $dataGenerator
107- * @param class-string<Dispatcher> $dispatcher
108- */
109- public function useCustomDispatcher (DataGenerator $ dataGenerator , Dispatcher $ dispatcher ): self
110- {
111- return new self (
112- $ this ->routeDefinitionCallback ,
113- $ dataGenerator ,
114- $ dispatcher ,
115- $ this ->routesConfiguration ,
116- $ this ->uriGenerator ,
117- $ this ->cacheDriver ,
118- $ this ->cacheKey ,
119- );
120- }
121-
122- /** @param class-string<GenerateUri> $uriGenerator */
123- public function withUriGenerator (GenerateUri $ uriGenerator ): self
124- {
125- return new self (
126- $ this ->routeDefinitionCallback ,
127- $ this ->dataGenerator ,
128- $ this ->dispatcher ,
129- $ this ->routesConfiguration ,
130- $ uriGenerator ,
131- $ this ->cacheDriver ,
132- $ this ->cacheKey ,
133- );
134- }
135-
13628 /** @return ProcessedData */
13729 private function buildConfiguration (): array
13830 {
@@ -141,26 +33,30 @@ private function buildConfiguration(): array
14133 }
14234
14335 $ loader = function (): array {
144- ($ this ->routeDefinitionCallback )($ this ->routesConfiguration );
145- return $ this ->routesConfiguration ->processedRoutes ($ this ->dataGenerator );
36+ $ routesConfiguration = $ this ->settings ->getRoutesConfiguration ();
37+ ($ this ->routeDefinitionCallback )($ routesConfiguration );
38+
39+ return $ routesConfiguration ->processedRoutes ($ this ->settings ->getDataGenerator ());
14640 };
14741
148- if ($ this ->cacheDriver === null ) {
42+ $ cacheDriver = $ this ->settings ->getCacheDriver ();
43+
44+ if ($ cacheDriver === null ) {
14945 return $ this ->processedConfiguration = $ loader ();
15046 }
15147
15248 assert (is_string ($ this ->cacheKey ));
15349
154- return $ this ->processedConfiguration = $ this -> cacheDriver ->get ($ this ->cacheKey , $ loader );
50+ return $ this ->processedConfiguration = $ cacheDriver ->get ($ this ->cacheKey , $ loader );
15551 }
15652
15753 public function dispatcher (): Dispatcher
15854 {
159- return $ this ->dispatcher ->with ($ this ->buildConfiguration ());
55+ return $ this ->settings -> getDispatcher () ->with ($ this ->buildConfiguration ());
16056 }
16157
16258 public function uriGenerator (): GenerateUri
16359 {
164- return $ this ->uriGenerator ->with ($ this ->buildConfiguration ()[2 ]);
60+ return $ this ->settings -> getUriGenerator () ->with ($ this ->buildConfiguration ()[2 ]);
16561 }
16662}
0 commit comments