Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Page.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
12 
30 class Page extends Layout
31 {
35  protected $pageLayout;
36 
40  protected $pageConfig;
41 
46 
51 
55  protected $pageLayoutReader;
56 
60  protected $viewFileSystem;
61 
65  protected $viewVars;
66 
70  protected $template;
71 
75  protected $request;
76 
82  protected $assetRepo;
83 
87  protected $logger;
88 
92  protected $urlBuilder;
93 
97  private $entitySpecificHandlesList;
98 
116  public function __construct(
117  View\Element\Template\Context $context,
118  View\LayoutFactory $layoutFactory,
119  View\Layout\ReaderPool $layoutReaderPool,
120  Framework\Translate\InlineInterface $translateInline,
121  View\Layout\BuilderFactory $layoutBuilderFactory,
122  View\Layout\GeneratorPool $generatorPool,
123  View\Page\Config\RendererFactory $pageConfigRendererFactory,
124  View\Page\Layout\Reader $pageLayoutReader,
125  $template,
126  $isIsolated = false,
127  View\EntitySpecificHandlesList $entitySpecificHandlesList = null
128  ) {
129  $this->request = $context->getRequest();
130  $this->assetRepo = $context->getAssetRepository();
131  $this->logger = $context->getLogger();
132  $this->urlBuilder = $context->getUrlBuilder();
133  $this->pageConfig = $context->getPageConfig();
134  $this->pageLayoutReader = $pageLayoutReader;
135  $this->viewFileSystem = $context->getViewFileSystem();
136  $this->pageConfigRendererFactory = $pageConfigRendererFactory;
137  $this->template = $template;
138  $this->entitySpecificHandlesList = $entitySpecificHandlesList
139  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(View\EntitySpecificHandlesList::class);
140  parent::__construct(
141  $context,
142  $layoutFactory,
143  $layoutReaderPool,
144  $translateInline,
145  $layoutBuilderFactory,
147  $isIsolated
148  );
149  $this->initPageConfigReader();
150  }
151 
157  protected function initPageConfigReader()
158  {
159  $this->pageConfigRenderer = $this->pageConfigRendererFactory->create(['pageConfig' => $this->pageConfig]);
160  }
161 
167  protected function initLayoutBuilder()
168  {
169  $this->layoutBuilderFactory->create(View\Layout\BuilderFactory::TYPE_PAGE, [
170  'layout' => $this->layout,
171  'pageConfig' => $this->pageConfig,
172  'pageLayoutReader' => $this->pageLayoutReader
173  ]);
174  }
175 
181  public function initLayout()
182  {
183  $this->addHandle('default');
184  $this->addHandle($this->getDefaultLayoutHandle());
185  $update = $this->getLayout()->getUpdate();
186  if ($update->isLayoutDefined()) {
187  $update->removeHandle('default');
188  }
189  return $this;
190  }
191 
197  public function addDefaultHandle()
198  {
199  $this->addHandle('default');
200  return parent::addDefaultHandle();
201  }
202 
208  public function getConfig()
209  {
210  return $this->pageConfig;
211  }
212 
221  public function addPageLayoutHandles(array $parameters = [], $defaultHandle = null, $entitySpecific = true)
222  {
223  $handle = $defaultHandle ? $defaultHandle : $this->getDefaultLayoutHandle();
224  $pageHandles = [$handle];
225  foreach ($parameters as $key => $value) {
226  $pageHandle = $handle . '_' . $key . '_' . $value;
227  $pageHandles[] = $pageHandle;
228  if ($entitySpecific) {
229  $this->entitySpecificHandlesList->addHandle($pageHandle);
230  }
231  }
232  // Do not sort array going into add page handles. Ensure default layout handle is added first.
233  $this->addHandle($pageHandles);
234  return true;
235  }
236 
240  protected function render(HttpResponseInterface $response)
241  {
242  $this->pageConfig->publicBuild();
243  if ($this->getPageLayout()) {
244  $config = $this->getConfig();
245  $this->addDefaultBodyClasses();
246  $addBlock = $this->getLayout()->getBlock('head.additional'); // todo
247  $requireJs = $this->getLayout()->getBlock('require.js');
248  $this->assign([
249  'requireJs' => $requireJs ? $requireJs->toHtml() : null,
250  'headContent' => $this->pageConfigRenderer->renderHeadContent(),
251  'headAdditional' => $addBlock ? $addBlock->toHtml() : null,
252  'htmlAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_HTML),
253  'headAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_HEAD),
254  'bodyAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_BODY),
255  'loaderIcon' => $this->getViewFileUrl('images/loader-2.gif'),
256  ]);
257 
258  $output = $this->getLayout()->getOutput();
259  $this->assign('layoutContent', $output);
260  $output = $this->renderPage();
261  $this->translateInline->processResponseBody($output);
262  $response->appendBody($output);
263  } else {
264  parent::render($response);
265  }
266  return $this;
267  }
268 
274  protected function addDefaultBodyClasses()
275  {
276  $this->pageConfig->addBodyClass($this->request->getFullActionName('-'));
277  $pageLayout = $this->getPageLayout();
278  if ($pageLayout) {
279  $this->pageConfig->addBodyClass('page-layout-' . $pageLayout);
280  }
281  return $this;
282  }
283 
287  protected function getPageLayout()
288  {
289  return $this->pageConfig->getPageLayout() ?: $this->getLayout()->getUpdate()->getPageLayout();
290  }
291 
299  protected function assign($key, $value = null)
300  {
301  if (is_array($key)) {
302  foreach ($key as $subKey => $subValue) {
303  $this->assign($subKey, $subValue);
304  }
305  } else {
306  $this->viewVars[$key] = $value;
307  }
308  return $this;
309  }
310 
317  protected function renderPage()
318  {
319  $fileName = $this->viewFileSystem->getTemplateFileName($this->template);
320  if (!$fileName) {
321  throw new \InvalidArgumentException('Template "' . $this->template . '" is not found');
322  }
323 
324  ob_start();
325  try {
326  extract($this->viewVars, EXTR_SKIP);
327  include $fileName;
328  } catch (\Exception $exception) {
329  ob_end_clean();
330  throw $exception;
331  }
332  $output = ob_get_clean();
333  return $output;
334  }
335 
343  protected function getViewFileUrl($fileId, array $params = [])
344  {
345  try {
346  $params = array_merge(['_secure' => $this->request->isSecure()], $params);
347  return $this->assetRepo->getUrlWithParams($fileId, $params);
348  } catch (\Magento\Framework\Exception\LocalizedException $e) {
349  $this->logger->critical($e);
350  return $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']);
351  }
352  }
353 }
$response
Definition: 404.php:11
getViewFileUrl($fileId, array $params=[])
Definition: Page.php:343
render(HttpResponseInterface $response)
Definition: Page.php:240
$config
Definition: fraud_order.php:17
__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, View\Page\Config\RendererFactory $pageConfigRendererFactory, View\Page\Layout\Reader $pageLayoutReader, $template, $isIsolated=false, View\EntitySpecificHandlesList $entitySpecificHandlesList=null)
Definition: Page.php:116
addPageLayoutHandles(array $parameters=[], $defaultHandle=null, $entitySpecific=true)
Definition: Page.php:221
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
assign($key, $value=null)
Definition: Page.php:299
$handle
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18