Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
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;

/**
* Controller class.
*
* @author William DURAND <william.durand1@gmail.com>
*/
class Controller
class Controller implements CacheWarmerInterface
{
protected CacheControlConfig $cacheControlConfig;

Expand Down Expand Up @@ -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()));
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions Resources/config/controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
23 changes: 23 additions & 0 deletions Tests/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading