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