annotate core/modules/book/src/Cache/BookNavigationCacheContext.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\book\Cache;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheableMetadata;
Chris@0 6 use Drupal\Core\Cache\Context\CacheContextInterface;
Chris@0 7 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
Chris@0 8 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
Chris@0 9 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Defines the book navigation cache context service.
Chris@0 13 *
Chris@0 14 * Cache context ID: 'route.book_navigation'.
Chris@0 15 *
Chris@0 16 * This allows for book navigation location-aware caching. It depends on:
Chris@0 17 * - whether the current route represents a book node at all
Chris@0 18 * - and if so, where in the book hierarchy we are
Chris@0 19 *
Chris@0 20 * This class is container-aware to avoid initializing the 'book.manager'
Chris@0 21 * service when it is not necessary.
Chris@0 22 */
Chris@0 23 class BookNavigationCacheContext implements CacheContextInterface, ContainerAwareInterface {
Chris@0 24
Chris@0 25 use ContainerAwareTrait;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The request stack.
Chris@0 29 *
Chris@0 30 * @var \Symfony\Component\HttpFoundation\RequestStack
Chris@0 31 */
Chris@0 32 protected $requestStack;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Constructs a new BookNavigationCacheContext service.
Chris@0 36 *
Chris@0 37 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
Chris@0 38 * The request stack.
Chris@0 39 */
Chris@0 40 public function __construct(RequestStack $request_stack) {
Chris@0 41 $this->requestStack = $request_stack;
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * {@inheritdoc}
Chris@0 46 */
Chris@0 47 public static function getLabel() {
Chris@0 48 return t("Book navigation");
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * {@inheritdoc}
Chris@0 53 */
Chris@0 54 public function getContext() {
Chris@0 55 // Find the current book's ID.
Chris@0 56 $current_bid = 0;
Chris@0 57 if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
Chris@0 58 $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
Chris@0 59 }
Chris@0 60
Chris@0 61 // If we're not looking at a book node, then we're not navigating a book.
Chris@0 62 if ($current_bid === 0) {
Chris@0 63 return 'book.none';
Chris@0 64 }
Chris@0 65
Chris@0 66 // If we're looking at a book node, get the trail for that node.
Chris@0 67 $active_trail = $this->container->get('book.manager')
Chris@0 68 ->getActiveTrailIds($node->book['bid'], $node->book);
Chris@0 69 return implode('|', $active_trail);
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public function getCacheableMetadata() {
Chris@0 76 // The book active trail depends on the node and data attached to it.
Chris@0 77 // That information is however not stored as part of the node.
Chris@0 78 $cacheable_metadata = new CacheableMetadata();
Chris@0 79 if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
Chris@0 80 // If the node is part of a book then we can use the cache tag for that
Chris@0 81 // book. If not, then it can't be optimized away.
Chris@0 82 if (!empty($node->book['bid'])) {
Chris@0 83 $cacheable_metadata->addCacheTags(['bid:' . $node->book['bid']]);
Chris@0 84 }
Chris@0 85 else {
Chris@0 86 $cacheable_metadata->setCacheMaxAge(0);
Chris@0 87 }
Chris@0 88 }
Chris@0 89 return $cacheable_metadata;
Chris@0 90 }
Chris@0 91
Chris@0 92 }