Skip to content

Commit f9a223a

Browse files
committedNov 27, 2024
feature #6570 Use buildDir for cache (alexndlm)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Use buildDir for cache Feature available since Symfony 5.2 - symfony/symfony#36515 Commits ------- 9daaade Use buildDir for cache
2 parents 2982a8c + 9daaade commit f9a223a

File tree

5 files changed

+45
-41
lines changed

5 files changed

+45
-41
lines changed
 

‎config/services.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
->tag('kernel.event_listener', ['event' => ViewEvent::class])
181181

182182
->set(AdminContextFactory::class)
183-
->arg(0, '%kernel.cache_dir%')
183+
->arg(0, '%kernel.build_dir%')
184184
->arg(1, new Reference('security.token_storage', ContainerInterface::NULL_ON_INVALID_REFERENCE))
185185
->arg(2, new Reference(MenuFactory::class))
186186
->arg(3, new Reference(CrudControllerRegistry::class))

‎src/Cache/CacheWarmer.php

+37-33
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,51 @@ public function isOptional(): bool
3030

3131
public function warmUp(string $cacheDir, ?string $buildDir = null): array
3232
{
33-
$allRoutes = $this->router->getRouteCollection();
34-
$dashboardRoutes = [];
35-
36-
/** @var Route $route */
37-
foreach ($allRoutes as $routeName => $route) {
38-
$controller = $route->getDefault('_controller') ?? '';
39-
// controller is defined as $router->add('admin', '/')->controller(DashboardController::class)
40-
if (\is_string($controller) && '' !== $controller && class_exists($controller)) {
41-
$controller .= '::__invoke';
42-
}
33+
$cacheFilename = ($buildDir ?? $cacheDir).'/'.self::DASHBOARD_ROUTES_CACHE;
4334

44-
// controller is defined as $router->add('admin', '/')->controller([DashboardController::class, 'index'])
45-
if (\is_array($controller)) {
46-
$controller = $controller[0].'::'.($controller[1] ?? '__invoke');
47-
}
35+
if (false === file_exists($cacheFilename)) {
36+
$allRoutes = $this->router->getRouteCollection();
37+
$dashboardRoutes = [];
4838

49-
$controller = u($controller);
50-
if ($controller->isEmpty()) {
51-
// this happens e.g. when using 'lexik/jwt-authentication-bundle', which defines an empty controller
52-
continue;
53-
}
39+
/** @var Route $route */
40+
foreach ($allRoutes as $routeName => $route) {
41+
$controller = $route->getDefault('_controller') ?? '';
42+
// controller is defined as $router->add('admin', '/')->controller(DashboardController::class)
43+
if (\is_string($controller) && '' !== $controller && class_exists($controller)) {
44+
$controller .= '::__invoke';
45+
}
5446

55-
if (!$controller->endsWith('::index') && !$controller->endsWith('::__invoke')) {
56-
continue;
57-
}
47+
// controller is defined as $router->add('admin', '/')->controller([DashboardController::class, 'index'])
48+
if (\is_array($controller)) {
49+
$controller = $controller[0].'::'.($controller[1] ?? '__invoke');
50+
}
5851

59-
$controllerFqcn = $controller->beforeLast('::')->toString();
60-
if (!is_subclass_of($controllerFqcn, DashboardControllerInterface::class)) {
61-
continue;
52+
$controller = u($controller);
53+
if ($controller->isEmpty()) {
54+
// this happens e.g. when using 'lexik/jwt-authentication-bundle', which defines an empty controller
55+
continue;
56+
}
57+
58+
if (!$controller->endsWith('::index') && !$controller->endsWith('::__invoke')) {
59+
continue;
60+
}
61+
62+
$controllerFqcn = $controller->beforeLast('::')->toString();
63+
if (!is_subclass_of($controllerFqcn, DashboardControllerInterface::class)) {
64+
continue;
65+
}
66+
67+
// when using i18n routes, the same controller can be associated to
68+
// multiple routes (e.g. 'admin.en', 'admin.es', 'admin.fr', etc.)
69+
$dashboardRoutes[$routeName] = $controller->toString();
6270
}
6371

64-
// when using i18n routes, the same controller can be associated to
65-
// multiple routes (e.g. 'admin.en', 'admin.es', 'admin.fr', etc.)
66-
$dashboardRoutes[$routeName] = $controller->toString();
72+
(new Filesystem())->dumpFile(
73+
$cacheFilename,
74+
'<?php return '.var_export($dashboardRoutes, true).';'
75+
);
6776
}
6877

69-
(new Filesystem())->dumpFile(
70-
$cacheDir.'/'.self::DASHBOARD_ROUTES_CACHE,
71-
'<?php return '.var_export($dashboardRoutes, true).';'
72-
);
73-
7478
// we don't use this, but it's required by the interface to return the list of classes to preload
7579
return [];
7680
}

‎src/DependencyInjection/CreateControllerRegistriesPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private function createDashboardControllerRegistryService(ContainerBuilder $cont
3535
->register(DashboardControllerRegistry::class, DashboardControllerRegistry::class)
3636
->setPublic(false)
3737
->setArguments([
38-
$container->getParameter('kernel.cache_dir'),
38+
$container->getParameter('kernel.build_dir'),
3939
$controllerFqcnToContextIdMap,
4040
array_flip($controllerFqcnToContextIdMap),
4141
]);

‎src/Factory/AdminContextFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
*/
3434
final class AdminContextFactory
3535
{
36-
private string $cacheDir;
36+
private string $buildDir;
3737
private ?TokenStorageInterface $tokenStorage;
3838
private MenuFactoryInterface $menuFactory;
3939
private CrudControllerRegistry $crudControllers;
4040
private EntityFactory $entityFactory;
4141
private AdminRouteGenerator $adminRouteGenerator;
4242

43-
public function __construct(string $cacheDir, ?TokenStorageInterface $tokenStorage, MenuFactoryInterface $menuFactory, CrudControllerRegistry $crudControllers, EntityFactory $entityFactory, AdminRouteGenerator $adminRouteGenerator)
43+
public function __construct(string $buildDir, ?TokenStorageInterface $tokenStorage, MenuFactoryInterface $menuFactory, CrudControllerRegistry $crudControllers, EntityFactory $entityFactory, AdminRouteGenerator $adminRouteGenerator)
4444
{
45-
$this->cacheDir = $cacheDir;
45+
$this->buildDir = $buildDir;
4646
$this->tokenStorage = $tokenStorage;
4747
$this->menuFactory = $menuFactory;
4848
$this->crudControllers = $crudControllers;
@@ -75,7 +75,7 @@ public function create(Request $request, DashboardControllerInterface $dashboard
7575

7676
private function getDashboardDto(Request $request, DashboardControllerInterface $dashboardControllerInstance): DashboardDto
7777
{
78-
$dashboardRoutesCachePath = $this->cacheDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
78+
$dashboardRoutesCachePath = $this->buildDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
7979
$dashboardControllerRoutes = !file_exists($dashboardRoutesCachePath) ? [] : require $dashboardRoutesCachePath;
8080
$dashboardController = $dashboardControllerInstance::class.'::index';
8181
$dashboardRouteName = null;

‎src/Registry/DashboardControllerRegistry.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ final class DashboardControllerRegistry implements DashboardControllerRegistryIn
1515
* @param string[] $contextIdToControllerFqcnMap
1616
*/
1717
public function __construct(
18-
string $cacheDir,
18+
string $buildDir,
1919
private readonly array $controllerFqcnToContextIdMap,
2020
private readonly array $contextIdToControllerFqcnMap,
2121
) {
22-
$dashboardRoutesCachePath = $cacheDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
22+
$dashboardRoutesCachePath = $buildDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
2323
$dashboardControllerRoutes = !file_exists($dashboardRoutesCachePath) ? [] : require $dashboardRoutesCachePath;
2424
foreach ($dashboardControllerRoutes as $routeName => $controller) {
2525
$this->controllerFqcnToRouteMap[u($controller)->before('::')->toString()] = $routeName;

0 commit comments

Comments
 (0)
Please sign in to comment.