0b0cdf430b6cbd185490b76147d82adfd9a890dc
[yaffs-website] / web / core / modules / system / tests / modules / early_rendering_controller_test / src / EarlyRenderingTestController.php
1 <?php
2
3 namespace Drupal\early_rendering_controller_test;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\InsertCommand;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Render\RendererInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\Response;
11
12 /**
13  * Controller routines for early_rendering_test routes.
14  *
15  * The methods on this controller each correspond to a route for this module,
16  * each of which exist solely for test cases in EarlyRenderingControllerTest;
17  * see that test for documentation.
18  *
19  * @see core/modules/early_rendering_controller_test/early_rendering_controller_test.routing.yml
20  * @see \Drupal\system\Tests\Common\EarlyRenderingControllerTest::testEarlyRendering()
21  */
22 class EarlyRenderingTestController extends ControllerBase {
23
24   /**
25    * The renderer.
26    *
27    * @var \Drupal\Core\Render\RendererInterface
28    */
29   protected $renderer;
30
31   /**
32    * Constructs a EarlyRenderingTestController.
33    *
34    * @param \Drupal\Core\Render\RendererInterface $renderer
35    */
36   public function __construct(RendererInterface $renderer) {
37     $this->renderer = $renderer;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function create(ContainerInterface $container) {
44     return new static(
45       $container->get('renderer')
46     );
47   }
48
49   protected function earlyRenderContent() {
50     return [
51       '#markup' => 'Hello world!',
52       '#cache' => [
53         'tags' => [
54           'foo',
55         ],
56       ],
57     ];
58   }
59
60   public function renderArray() {
61     return [
62       '#pre_render' => [function () {
63         $elements = $this->earlyRenderContent();
64         return $elements;
65       }],
66     ];
67   }
68
69   public function renderArrayEarly() {
70     $render_array = $this->earlyRenderContent();
71     return [
72       '#markup' => $this->renderer->render($render_array),
73     ];
74   }
75
76   public function ajaxResponse() {
77     $response = new AjaxResponse();
78     $response->addCommand(new InsertCommand(NULL, $this->renderArray()));
79     return $response;
80   }
81
82   public function ajaxResponseEarly() {
83     $response = new AjaxResponse();
84     $response->addCommand(new InsertCommand(NULL, $this->renderArrayEarly()));
85     return $response;
86   }
87
88   public function response() {
89     return new Response('Hello world!');
90   }
91
92   public function responseEarly() {
93     $render_array = $this->earlyRenderContent();
94     return new Response($this->renderer->render($render_array));
95   }
96
97   public function responseWithAttachments() {
98     return new AttachmentsTestResponse('Hello world!');
99   }
100
101   public function responseWithAttachmentsEarly() {
102     $render_array = $this->earlyRenderContent();
103     return new AttachmentsTestResponse($this->renderer->render($render_array));
104   }
105
106   public function cacheableResponse() {
107     return new CacheableTestResponse('Hello world!');
108   }
109
110   public function cacheableResponseEarly() {
111     $render_array = $this->earlyRenderContent();
112     return new CacheableTestResponse($this->renderer->render($render_array));
113   }
114
115   public function domainObject() {
116     return new TestDomainObject();
117   }
118
119   public function domainObjectEarly() {
120     $render_array = $this->earlyRenderContent();
121     $this->renderer->render($render_array);
122     return new TestDomainObject();
123   }
124
125   public function domainObjectWithAttachments() {
126     return new AttachmentsTestDomainObject();
127   }
128
129   public function domainObjectWithAttachmentsEarly() {
130     $render_array = $this->earlyRenderContent();
131     $this->renderer->render($render_array);
132     return new AttachmentsTestDomainObject();
133   }
134
135   public function cacheableDomainObject() {
136     return new CacheableTestDomainObject();
137   }
138
139   public function cacheableDomainObjectEarly() {
140     $render_array = $this->earlyRenderContent();
141     $this->renderer->render($render_array);
142     return new CacheableTestDomainObject();
143   }
144
145 }