diff --git a/Controller/Controller.php b/Controller/Controller.php index f67a1e1..7d5f4be 100644 --- a/Controller/Controller.php +++ b/Controller/Controller.php @@ -20,6 +20,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag; +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; /** @@ -27,7 +28,7 @@ * * @author William DURAND */ -class Controller +class Controller implements CacheWarmerInterface { protected CacheControlConfig $cacheControlConfig; @@ -60,19 +61,17 @@ public function indexAction(Request $request, $_format): Response $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug); if (!$cache->isFresh() || $this->debug) { - $exposedRoutes = $this->exposedRoutesExtractor->getRoutes(); - $serializedRoutes = $this->serializer->serialize($exposedRoutes, 'json'); - $cache->write($serializedRoutes, $this->exposedRoutesExtractor->getResources()); - } else { - $path = method_exists($cache, 'getPath') ? $cache->getPath() : (string) $cache; - $serializedRoutes = file_get_contents($path); - $exposedRoutes = $this->serializer->deserialize( - $serializedRoutes, - 'Symfony\Component\Routing\RouteCollection', - 'json' - ); + $this->warmUp(dirname($cache->getPath())); } + $path = method_exists($cache, 'getPath') ? $cache->getPath() : (string) $cache; + $serializedRoutes = file_get_contents($path); + $exposedRoutes = $this->serializer->deserialize( + $serializedRoutes, + 'Symfony\Component\Routing\RouteCollection', + 'json' + ); + $this->routesResponse->setBaseUrl($this->exposedRoutesExtractor->getBaseUrl()); $this->routesResponse->setRoutes($exposedRoutes); $this->routesResponse->setPrefix($this->exposedRoutesExtractor->getPrefix($request->getLocale())); @@ -97,4 +96,19 @@ public function indexAction(Request $request, $_format): Response return $response; } + + public function warmUp(string $cacheDir, ?string $buildDir = null): array + { + $cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath(null), $this->debug); + $exposedRoutes = $this->exposedRoutesExtractor->getRoutes(); + $serializedRoutes = $this->serializer->serialize($exposedRoutes, 'json'); + $cache->write($serializedRoutes, $this->exposedRoutesExtractor->getResources()); + + return [$cache->getPath()]; + } + + public function isOptional(): bool + { + return true; + } } diff --git a/Resources/config/controllers.php b/Resources/config/controllers.php index a02ae4e..2d815d3 100644 --- a/Resources/config/controllers.php +++ b/Resources/config/controllers.php @@ -22,6 +22,7 @@ $containerConfigurator->services() ->set('fos_js_routing.controller', '%fos_js_routing.controller.class%') ->public() + ->tag('kernel.cache_warmer') ->args([ service('fos_js_routing.routes_response'), service('fos_js_routing.serializer'), diff --git a/Tests/Controller/ControllerTest.php b/Tests/Controller/ControllerTest.php index 9183b58..6469c36 100644 --- a/Tests/Controller/ControllerTest.php +++ b/Tests/Controller/ControllerTest.php @@ -113,6 +113,29 @@ public function testConfigCache(): void $this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent()); } + public function testWarmUp(): void + { + $routes = new RouteCollection(); + $routes->add('literal', new Route('/homepage')); + + $controller = new Controller( + $this->routesResponse, + $this->getSerializer(), + $this->getExtractor($routes), + ); + + if (file_exists($this->cachePath)) { + unlink($this->cachePath); + } + + $files = $controller->warmUp(dirname($this->cachePath)); + + $this->assertFileExists($this->cachePath); + $this->assertStringContainsString('homepage', file_get_contents($this->cachePath)); + $this->assertCount(1, $files); + $this->assertEquals($this->cachePath, $files[0]); + } + /** * @dataProvider dataProviderForTestGenerateWithCallback */