Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always cache the compiled menus #2886

Merged
merged 6 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ services:
tags:
- { name: knp_menu.menu_builder, method: createAdminMenu, alias: admin_menu } # The alias is what is used to retrieve the menu

Bolt\Menu\BackendMenuBuilderInterface: '@Bolt\Menu\BackendMenuBuilder'
Bolt\Menu\BackendMenuBuilderInterface: '@Bolt\Menu\BackendMenu'

Bolt\Menu\FrontendMenuBuilder: ~

Bolt\Menu\FrontendMenuBuilderInterface: '@Bolt\Menu\FrontendMenuBuilder'
Bolt\Menu\FrontendMenuBuilderInterface: '@Bolt\Menu\FrontendMenu'

# Needed for SetContent from bolt/core
Bolt\Storage\ContentQueryParser:
Expand Down
12 changes: 0 additions & 12 deletions config/services_dev.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions config/services_prod.yaml

This file was deleted.

52 changes: 52 additions & 0 deletions src/Menu/BackendMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Bolt\Menu;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

final class BackendMenu implements BackendMenuBuilderInterface
{
/** @var CacheInterface */
private $cache;

/** @var BackendMenuBuilder */
private $menuBuilder;

/** @var RequestStack */
private $requestStack;

/** @var Stopwatch */
private $stopwatch;

public function __construct(BackendMenuBuilder $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack, Stopwatch $stopwatch)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
$this->requestStack = $requestStack;
$this->stopwatch = $stopwatch;
}

public function buildAdminMenu(): array
{
$this->stopwatch->start('bolt.backendMenu');

$locale = $this->requestStack->getCurrentRequest()->getLocale();
$cacheKey = 'bolt.backendmenu_' . $locale;

$menu = $this->cache->get($cacheKey, function (ItemInterface $item) {
$item->tag('backendmenu');

return $this->menuBuilder->buildAdminMenu();
});

$this->stopwatch->stop('bolt.backendMenu');

return $menu;
}
}
22 changes: 0 additions & 22 deletions src/Menu/BackendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,6 @@ private function createAdminMenu(): ItemInterface
]);
}

/*
* @todo Make fixtures work from the backend
*/
// $menu->getChild('Maintenance')->addChild('Fixtures', [
// 'uri' => '',
// 'extras' => [
// 'name' => $t->trans('caption.fixtures_dummy_content'),
// 'icon' => 'fa-hat-wizard',
// ],
// ]);

if ($this->authorizationChecker->isGranted('clearcache')) {
$menu->getChild('Maintenance')->addChild('Clear the cache', [
'uri' => $this->urlGenerator->generate('bolt_clear_cache'),
Expand All @@ -261,17 +250,6 @@ private function createAdminMenu(): ItemInterface
]);
}

/*
* @todo Make Installation checks work from the backend
*/
// $menu->getChild('Maintenance')->addChild('Installation checks', [
// 'uri' => '',
// 'extras' => [
// 'name' => $t->trans('caption.installation_checks'),
// 'icon' => 'fa-clipboard-check',
// ],
// ]);

if ($this->authorizationChecker->isGranted('translation')) {
$menu->getChild('Maintenance')->addChild('Translations', [
'uri' => $this->urlGenerator->generate('translation_index'),
Expand Down
29 changes: 6 additions & 23 deletions src/Menu/CachedBackendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,21 @@

namespace Bolt\Menu;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

/**
* @deprecated since Bolt 5.1. This class is just an empty wrapper around BackendMenu now. Use that class instead
*/
final class CachedBackendMenuBuilder implements BackendMenuBuilderInterface
{
/** @var CacheInterface */
private $cache;

/** @var BackendMenuBuilderInterface */
/** @var BackendMenu */
private $menuBuilder;

/** @var RequestStack */
private $requestStack;

public function __construct(BackendMenuBuilderInterface $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack)
public function __construct(BackendMenu $menuBuilder)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
$this->requestStack = $requestStack;
}

public function buildAdminMenu(): array
{
$locale = $this->requestStack->getCurrentRequest()->getLocale();
$cacheKey = 'backendmenu_' . $locale;

return $this->cache->get($cacheKey, function (ItemInterface $item) {
$item->tag('backendmenu');

return $this->menuBuilder->buildAdminMenu();
});
return $this->menuBuilder->buildAdminMenu();
}
}
25 changes: 5 additions & 20 deletions src/Menu/CachedFrontendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,23 @@

namespace Bolt\Menu;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Twig\Environment;

/**
* @deprecated since Bolt 5.1. This class is just an empty wrapper around FrontendMenu now. Use that class instead
*/
final class CachedFrontendMenuBuilder implements FrontendMenuBuilderInterface
{
/** @var TagAwareCacheInterface */
private $cache;

/** @var FrontendMenuBuilderInterface */
private $menuBuilder;

/** @var Request */
private $request;

public function __construct(FrontendMenuBuilderInterface $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack)
public function __construct(FrontendMenu $menuBuilder)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
$this->request = $requestStack->getCurrentRequest();
}

public function buildMenu(Environment $twig, ?string $name = null): array
{
$key = 'frontendmenu_' . ($name ?: 'main') . '_' . $this->request->getLocale();

return $this->cache->get($key, function (ItemInterface $item) use ($name, $twig) {
$item->tag('frontendmenu');

return $this->menuBuilder->buildMenu($twig, $name);
});
return $this->menuBuilder->buildMenu($twig, $name);
}
}
53 changes: 53 additions & 0 deletions src/Menu/FrontendMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Bolt\Menu;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Twig\Environment;


final class FrontendMenu implements FrontendMenuBuilderInterface
{
/** @var TagAwareCacheInterface */
private $cache;

/** @var FrontendMenuBuilder */
private $menuBuilder;

/** @var Request */
private $request;

/** @var Stopwatch */
private $stopwatch;

public function __construct(FrontendMenuBuilder $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack, Stopwatch $stopwatch)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
$this->request = $requestStack->getCurrentRequest();
$this->stopwatch = $stopwatch;
}

public function buildMenu(Environment $twig, ?string $name = null): array
{
$this->stopwatch->start('bolt.frontendMenu');

$key = 'frontendmenu_' . ($name ?: 'main') . '_' . $this->request->getLocale();

$menu = $this->cache->get($key, function (ItemInterface $item) use ($name, $twig) {
$item->tag('frontendmenu');

return $this->menuBuilder->buildMenu($twig, $name);
});

$this->stopwatch->stop('bolt.frontendMenu');

return $menu;
}
}
20 changes: 7 additions & 13 deletions src/Menu/StopwatchBackendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@

namespace Bolt\Menu;

use Symfony\Component\Stopwatch\Stopwatch;

/**
* @deprecated since Bolt 5.1. This class is just an empty wrapper around BackendMenu now. Use that class instead
*/
final class StopwatchBackendMenuBuilder implements BackendMenuBuilderInterface
{
/** @var BackendMenuBuilderInterface */
/** @var BackendMenu */
private $menuBuilder;

/** @var Stopwatch */
private $stopwatch;

public function __construct(BackendMenuBuilderInterface $menuBuilder, Stopwatch $stopwatch)
public function __construct(BackendMenu $menuBuilder)
{
$this->menuBuilder = $menuBuilder;
$this->stopwatch = $stopwatch;
}

public function buildAdminMenu(): array
{
$this->stopwatch->start('bolt.backendMenu');
$menu = $this->menuBuilder->buildAdminMenu();
$this->stopwatch->stop('bolt.backendMenu');

return $menu;
return $this->menuBuilder->buildAdminMenu();
;
}
}
18 changes: 6 additions & 12 deletions src/Menu/StopwatchFrontendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@

namespace Bolt\Menu;

use Symfony\Component\Stopwatch\Stopwatch;
use Twig\Environment;

/**
* @deprecated since Bolt 5.1. This class is just an empty wrapper around FrontendMenu now. Use that class instead
*/
final class StopwatchFrontendMenuBuilder implements FrontendMenuBuilderInterface
{
/** @var FrontendMenuBuilderInterface */
/** @var FrontendMenu */
private $menuBuilder;

/** @var Stopwatch */
private $stopwatch;

public function __construct(FrontendMenuBuilderInterface $menuBuilder, Stopwatch $stopwatch)
public function __construct(FrontendMenu $menuBuilder)
{
$this->menuBuilder = $menuBuilder;
$this->stopwatch = $stopwatch;
}

public function buildMenu(Environment $twig, ?string $name = null): array
{
$this->stopwatch->start('bolt.frontendMenu');
$menu = $this->menuBuilder->buildMenu($twig, $name);
$this->stopwatch->stop('bolt.frontendMenu');

return $menu;
return $this->menuBuilder->buildMenu($twig, $name);
}
}
10 changes: 10 additions & 0 deletions yaml-migrations/m_2021-10-26-services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Remove keys (and underlying values) from the file: `services.yaml`
# See: https://github.com/bolt/core/pull/2886

file: services.yaml
since: 5.0.6

add:
services:
Bolt\Menu\BackendMenuBuilderInterface: '@Bolt\Menu\BackendMenu'
Bolt\Menu\FrontendMenuBuilderInterface: '@Bolt\Menu\FrontendMenu'
12 changes: 12 additions & 0 deletions yaml-migrations/m_2021-10-26-services_dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Remove keys (and underlying values) from the file: `services_dev.yaml`
# See: https://github.com/bolt/core/pull/2886

file: services_dev.yaml
since: 5.0.6

remove:
services:
Bolt\Menu\StopwatchBackendMenuBuilder: ~
Bolt\Menu\BackendMenuBuilderInterface: ~
Bolt\Menu\StopwatchFrontendMenuBuilder: ~
Bolt\Menu\FrontendMenuBuilderInterface: ~
12 changes: 12 additions & 0 deletions yaml-migrations/m_2021-10-26-services_prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Remove keys (and underlying values) from the file: `services_prod.yaml`
# See: https://github.com/bolt/core/pull/2886

file: services_prod.yaml
since: 5.0.6

remove:
services:
Bolt\Menu\CachedBackendMenuBuilder: ~
Bolt\Menu\BackendMenuBuilderInterface: ~
Bolt\Menu\CachedFrontendMenuBuilder: ~
Bolt\Menu\FrontendMenuBuilderInterface: ~