249aa2024a2fdc9ee7b4c85bb51a7f874b470997
[yaffs-website] / web / core / modules / dynamic_page_cache / tests / dynamic_page_cache_test / src / DynamicPageCacheTestController.php
1 <?php
2
3 namespace Drupal\dynamic_page_cache_test;
4
5 use Drupal\Core\Cache\CacheableResponse;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drupal\user\Entity\User;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\Response;
10
11 /**
12  * Controller routines for dynamic_page_cache_test routes.
13  */
14 class DynamicPageCacheTestController {
15
16   use StringTranslationTrait;
17
18   /**
19    * A route returning a Response object.
20    *
21    * @return \Symfony\Component\HttpFoundation\Response
22    *   A Response object.
23    */
24   public function response() {
25     return new Response('foobar');
26   }
27
28   /**
29    * A route returning a CacheableResponse object.
30    *
31    * @return \Drupal\Core\Cache\CacheableResponseInterface
32    *   A CacheableResponseInterface object.
33    */
34   public function cacheableResponse() {
35     $user = User::load(1);
36     $response = new CacheableResponse($user->label());
37     $response->addCacheableDependency($user);
38     return $response;
39   }
40
41   /**
42    * A route returning a render array (without cache contexts, so cacheable).
43    *
44    * @return array
45    *   A render array.
46    */
47   public function html() {
48     return [
49       'content' => [
50         '#markup' => 'Hello world.',
51       ],
52     ];
53   }
54
55   /**
56    * A route returning a render array (with cache contexts, so cacheable).
57    *
58    * @param \Symfony\Component\HttpFoundation\Request $request
59    *   The current request.
60    *
61    * @return array
62    *   A render array.
63    *
64    * @see html()
65    */
66   public function htmlWithCacheContexts(Request $request) {
67     $build = $this->html();
68     $build['dynamic_part'] = [
69       '#markup' => $this->t('Hello there, %animal.', ['%animal' => $request->query->get('animal')]),
70       '#cache' => [
71         'contexts' => [
72           'url.query_args:animal',
73         ],
74       ],
75     ];
76     return $build;
77   }
78
79   /**
80    * A route returning a render array (with max-age=0, so uncacheable)
81    *
82    * @return array
83    *   A render array.
84    *
85    * @see html()
86    */
87   public function htmlUncacheableMaxAge() {
88     $build = $this->html();
89     $build['very_dynamic_part'] = [
90       '#markup' => 'Drupal cannot handle the awesomeness of llamas.',
91       '#cache' => [
92         'max-age' => 0,
93       ],
94     ];
95     return $build;
96   }
97
98   /**
99    * A route returning a render array (with 'user' context, so uncacheable)
100    *
101    * @return array
102    *   A render array.
103    *
104    * @see html()
105    */
106   public function htmlUncacheableContexts() {
107     $build = $this->html();
108     $build['very_dynamic_part'] = [
109       '#markup' => $this->t('@username cannot handle the awesomeness of llamas.', ['@username' => \Drupal::currentUser()->getDisplayName()]),
110       '#cache' => [
111         'contexts' => [
112           'user',
113         ],
114       ],
115     ];
116     return $build;
117   }
118
119   /**
120    * A route returning a render array (with a cache tag preventing caching).
121    *
122    * @return array
123    *   A render array.
124    *
125    * @see html()
126    */
127   public function htmlUncacheableTags() {
128     $build = $this->html();
129     $build['very_dynamic_part'] = [
130       '#markup' => 'Drupal cannot handle the awesomeness of llamas.',
131       '#cache' => [
132         'tags' => [
133           'current-temperature',
134         ],
135       ],
136     ];
137     return $build;
138   }
139
140 }