File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed
src/FreeElephants/JsonApiToolkit/Routing/FastRoute Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
### Added
10
10
- Data Transfer Object classes generation from swagger spec
11
11
- JsonApiResponseFactory::createResponse() ` meta ` and ` links ` arguments
12
+ - CacheableDispatcherFactoryProxy
12
13
13
14
## [ 0.0.13] - 2021-01-27
14
15
### Added
Original file line number Diff line number Diff line change 22
22
"neomerx/json-api" : " ^4.0" ,
23
23
"nette/php-generator" : " ^3.4" ,
24
24
"nikic/fast-route" : " ^1.3" ,
25
- "psr/http-message " : " ^1.0" ,
25
+ "psr/cache " : " ^1.0" ,
26
26
"psr/http-factory" : " ^1.0" ,
27
+ "psr/http-message" : " ^1.0" ,
27
28
"psr/http-server-handler" : " ^1.0" ,
28
29
"psr/http-server-middleware" : " ^1.0" ,
29
30
"rakit/validation" : " ^1.2" ,
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace FreeElephants \JsonApiToolkit \Routing \FastRoute ;
4
+
5
+ use FastRoute \Dispatcher ;
6
+ use Psr \Cache \CacheItemPoolInterface ;
7
+
8
+ class CacheableDispatcherFactoryProxy implements DispatcherFactoryInterface
9
+ {
10
+ private DispatcherFactoryInterface $ dispatcherFactory ;
11
+ private CacheItemPoolInterface $ cacheItemPool ;
12
+
13
+ public function __construct (DispatcherFactoryInterface $ dispatcherFactory , CacheItemPoolInterface $ cacheItemPool )
14
+ {
15
+ $ this ->dispatcherFactory = $ dispatcherFactory ;
16
+ $ this ->cacheItemPool = $ cacheItemPool ;
17
+ }
18
+
19
+ public function buildDispatcher (string $ openApiDocumentSource ): Dispatcher
20
+ {
21
+ $ key = md5_file ($ openApiDocumentSource );
22
+ $ cacheItem = $ this ->cacheItemPool ->getItem ($ key );
23
+ if ($ cacheItem ->isHit ()) {
24
+ $ dispatcher = $ cacheItem ->get ();
25
+ } else {
26
+ $ dispatcher = $ this ->dispatcherFactory ->buildDispatcher ($ openApiDocumentSource );
27
+ $ cacheItem ->set ($ dispatcher );
28
+ $ this ->cacheItemPool ->save ($ cacheItem );
29
+ }
30
+
31
+ return $ dispatcher ;
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments