forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheWarmer.php
More file actions
72 lines (58 loc) · 2.48 KB
/
CacheWarmer.php
File metadata and controls
72 lines (58 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Cache;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use function Symfony\Component\String\u;
/**
* @author Javier Eguiluz <[email protected]>
*/
final class CacheWarmer implements CacheWarmerInterface
{
public const DASHBOARD_ROUTES_CACHE = 'easyadmin/routes-dashboard.php';
private RouterInterface $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function isOptional(): bool
{
return false;
}
public function warmUp(string $cacheDir): array
{
$allRoutes = $this->router->getRouteCollection();
$dashboardRoutes = [];
/** @var Route $route */
foreach ($allRoutes as $routeName => $route) {
$controller = $route->getDefault('_controller') ?? '';
// controller is defined as $router->add('admin', '/')->controller(DashboardController::class)
if (\is_string($controller) && !empty($controller) && class_exists($controller)) {
$controller .= '::__invoke';
}
// controller is defined as $router->add('admin', '/')->controller([DashboardController::class, 'index'])
if (\is_array($controller)) {
$controller = $controller[0].'::'.($controller[1] ?? '__invoke');
}
$controller = u($controller);
if (!$controller->endsWith('::index') && !$controller->endsWith('::__invoke')) {
continue;
}
$controllerFqcn = $controller->beforeLast('::')->toString();
if (!is_subclass_of($controllerFqcn, DashboardControllerInterface::class)) {
continue;
}
// when using i18n routes, the same controller can be associated to
// multiple routes (e.g. 'admin.en', 'admin.es', 'admin.fr', etc.)
$dashboardRoutes[$routeName] = $controller->toString();
}
(new Filesystem())->dumpFile(
$cacheDir.'/'.self::DASHBOARD_ROUTES_CACHE,
'<?php return '.var_export($dashboardRoutes, true).';'
);
// we don't use this, but it's required by the interface to return the list of classes to preload
return [];
}
}