-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathLayout.php
188 lines (167 loc) · 5.04 KB
/
Layout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Result;
use Magento\Framework;
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\AbstractResult;
use Magento\Framework\View;
/**
* A generic layout response can be used for rendering any kind of layout
* So it comprises a response body from the layout elements it has and sets it to the HTTP response
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
* @api
* @since 100.0.2
*/
class Layout extends AbstractResult
{
/**
* @var \Magento\Framework\View\LayoutFactory
*/
protected $layoutFactory;
/**
* @var \Magento\Framework\View\Layout\BuilderFactory
*/
protected $layoutBuilderFactory;
/**
* @var \Magento\Framework\View\Layout\ReaderPool
*/
protected $layoutReaderPool;
/**
* @var \Magento\Framework\View\LayoutInterface
*/
protected $layout;
/**
* @var \Magento\Framework\Translate\InlineInterface
*/
protected $translateInline;
/**
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $eventManager;
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $request;
/**
* Constructor
*
* @param View\Element\Template\Context $context
* @param View\LayoutFactory $layoutFactory
* @param View\Layout\ReaderPool $layoutReaderPool
* @param Framework\Translate\InlineInterface $translateInline
* @param View\Layout\BuilderFactory $layoutBuilderFactory
* @param View\Layout\GeneratorPool $generatorPool
* @param bool $isIsolated
*/
public function __construct(
View\Element\Template\Context $context,
View\LayoutFactory $layoutFactory,
View\Layout\ReaderPool $layoutReaderPool,
Framework\Translate\InlineInterface $translateInline,
View\Layout\BuilderFactory $layoutBuilderFactory,
View\Layout\GeneratorPool $generatorPool,
$isIsolated = false
) {
$this->layoutFactory = $layoutFactory;
$this->layoutBuilderFactory = $layoutBuilderFactory;
$this->layoutReaderPool = $layoutReaderPool;
$this->eventManager = $context->getEventManager();
$this->request = $context->getRequest();
$this->translateInline = $translateInline;
// TODO Shared layout object will be deleted in MAGETWO-28359
$this->layout = $isIsolated
? $this->layoutFactory->create(['reader' => $this->layoutReaderPool, 'generatorPool' => $generatorPool])
: $context->getLayout();
$this->layout->setGeneratorPool($generatorPool);
$this->initLayoutBuilder();
}
/**
* Create layout builder
*
* @return void
*/
protected function initLayoutBuilder()
{
$this->layoutBuilderFactory->create(View\Layout\BuilderFactory::TYPE_LAYOUT, ['layout' => $this->layout]);
}
/**
* Get layout instance for current page
*
* @return \Magento\Framework\View\LayoutInterface
*/
public function getLayout()
{
return $this->layout;
}
/**
* @return $this
*/
public function addDefaultHandle()
{
$this->addHandle($this->getDefaultLayoutHandle());
return $this;
}
/**
* Retrieve the default layout handle name for the current action
*
* @return string
*/
public function getDefaultLayoutHandle()
{
return strtolower($this->request->getFullActionName());
}
/**
* @param string|string[] $handleName
* @return $this
*/
public function addHandle($handleName)
{
$this->getLayout()->getUpdate()->addHandle($handleName);
return $this;
}
/**
* Add update to merge object
*
* @param string $update
* @return $this
*/
public function addUpdate($update)
{
$this->getLayout()->getUpdate()->addUpdate($update);
return $this;
}
/**
* Render current layout
*
* @param HttpResponseInterface|ResponseInterface $httpResponse
* @return $this
*/
public function renderResult(ResponseInterface $httpResponse)
{
\Magento\Framework\Profiler::start('LAYOUT');
\Magento\Framework\Profiler::start('layout_render');
$this->eventManager->dispatch('layout_render_before');
$this->eventManager->dispatch('layout_render_before_' . $this->request->getFullActionName());
$this->applyHttpHeaders($httpResponse);
$this->render($httpResponse);
\Magento\Framework\Profiler::stop('layout_render');
\Magento\Framework\Profiler::stop('LAYOUT');
return $this;
}
/**
* {@inheritdoc}
*/
protected function render(HttpResponseInterface $response)
{
$output = $this->layout->getOutput();
$this->translateInline->processResponseBody($output);
$response->appendBody($output);
return $this;
}
}