Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Renderer.php
Go to the documentation of this file.
1 <?php
8 
12 
18 class Renderer implements RendererInterface
19 {
23  protected $assetTypeOrder = ['css', 'ico', 'js'];
24 
28  protected $pageConfig;
29 
33  protected $assetMergeService;
34 
38  protected $escaper;
39 
43  protected $string;
44 
48  protected $logger;
49 
53  protected $urlBuilder;
54 
63  public function __construct(
65  \Magento\Framework\View\Asset\MergeService $assetMergeService,
66  \Magento\Framework\UrlInterface $urlBuilder,
67  \Magento\Framework\Escaper $escaper,
68  \Magento\Framework\Stdlib\StringUtils $string,
69  \Psr\Log\LoggerInterface $logger
70  ) {
71  $this->pageConfig = $pageConfig;
72  $this->assetMergeService = $assetMergeService;
73  $this->urlBuilder = $urlBuilder;
74  $this->escaper = $escaper;
75  $this->string = $string;
76  $this->logger = $logger;
77  }
78 
85  public function renderElementAttributes($elementType)
86  {
87  $resultAttributes = [];
88  foreach ($this->pageConfig->getElementAttributes($elementType) as $name => $value) {
89  $resultAttributes[] = sprintf('%s="%s"', $name, $value);
90  }
91  return implode(' ', $resultAttributes);
92  }
93 
99  public function renderHeadContent()
100  {
101  $result = '';
102  $result .= $this->renderMetadata();
103  $result .= $this->renderTitle();
104  $this->prepareFavicon();
105  $result .= $this->renderAssets($this->getAvailableResultGroups());
106  $result .= $this->pageConfig->getIncludes();
107  return $result;
108  }
109 
115  public function renderTitle()
116  {
117  return '<title>' . $this->escaper->escapeHtml($this->pageConfig->getTitle()->get()) . '</title>' . "\n";
118  }
119 
125  public function renderMetadata()
126  {
127  $result = '';
128  foreach ($this->pageConfig->getMetadata() as $name => $content) {
129  $metadataTemplate = $this->getMetadataTemplate($name);
130  if (!$metadataTemplate) {
131  continue;
132  }
133  $content = $this->processMetadataContent($name, $content);
134  if ($content) {
135  $result .= str_replace(['%name', '%content'], [$name, $content], $metadataTemplate);
136  }
137  }
138  return $result;
139  }
140 
148  protected function processMetadataContent($name, $content)
149  {
150  $method = 'get' . $this->string->upperCaseWords($name, '_', '');
151  if ($name === 'title') {
152  if (!$content) {
153  $content = $this->escaper->escapeHtml($this->pageConfig->$method()->get());
154  }
155  return $content;
156  }
157  if (method_exists($this->pageConfig, $method)) {
158  $content = $this->pageConfig->$method();
159  }
160  return $content;
161  }
162 
169  protected function getMetadataTemplate($name)
170  {
171  if (strpos($name, 'og:') === 0) {
172  return '<meta property="' . $name . '" content="%content"/>' . "\n";
173  }
174 
175  switch ($name) {
176  case Config::META_CHARSET:
177  $metadataTemplate = '<meta charset="%content"/>' . "\n";
178  break;
179 
180  case Config::META_CONTENT_TYPE:
181  $metadataTemplate = '<meta http-equiv="Content-Type" content="%content"/>' . "\n";
182  break;
183 
184  case Config::META_X_UI_COMPATIBLE:
185  $metadataTemplate = '<meta http-equiv="X-UA-Compatible" content="%content"/>' . "\n";
186  break;
187 
188  case Config::META_MEDIA_TYPE:
189  $metadataTemplate = false;
190  break;
191 
192  default:
193  $metadataTemplate = '<meta name="%name" content="%content"/>' . "\n";
194  break;
195  }
196  return $metadataTemplate;
197  }
198 
204  public function prepareFavicon()
205  {
206  if ($this->pageConfig->getFaviconFile()) {
207  $this->pageConfig->addRemotePageAsset(
208  $this->pageConfig->getFaviconFile(),
210  ['attributes' => ['rel' => 'icon', 'type' => 'image/x-icon']],
211  'icon'
212  );
213  $this->pageConfig->addRemotePageAsset(
214  $this->pageConfig->getFaviconFile(),
216  ['attributes' => ['rel' => 'shortcut icon', 'type' => 'image/x-icon']],
217  'shortcut-icon'
218  );
219  } else {
220  $this->pageConfig->addPageAsset(
221  $this->pageConfig->getDefaultFavicon(),
222  ['attributes' => ['rel' => 'icon', 'type' => 'image/x-icon']],
223  'icon'
224  );
225  $this->pageConfig->addPageAsset(
226  $this->pageConfig->getDefaultFavicon(),
227  ['attributes' => ['rel' => 'shortcut icon', 'type' => 'image/x-icon']],
228  'shortcut-icon'
229  );
230  }
231  }
232 
240  public function renderAssets($resultGroups = [])
241  {
243  foreach ($this->pageConfig->getAssetCollection()->getGroups() as $group) {
244  $type = $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE);
245  if (!isset($resultGroups[$type])) {
246  $resultGroups[$type] = '';
247  }
248  $resultGroups[$type] .= $this->renderAssetGroup($group);
249  }
250  return implode('', $resultGroups);
251  }
252 
259  protected function renderAssetGroup(\Magento\Framework\View\Asset\PropertyGroup $group)
260  {
261  $groupHtml = $this->renderAssetHtml($group);
262  $groupHtml = $this->processIeCondition($groupHtml, $group);
263  return $groupHtml;
264  }
265 
273  protected function processMerge($groupAssets, $group)
274  {
275  if ($group->getProperty(GroupedCollection::PROPERTY_CAN_MERGE) && count($groupAssets) > 1) {
276  $groupAssets = $this->assetMergeService->getMergedAssets(
277  $groupAssets,
278  $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE)
279  );
280  }
281  return $groupAssets;
282  }
283 
290  protected function getGroupAttributes($group)
291  {
292  $attributes = $group->getProperty('attributes');
293  if (!empty($attributes)) {
294  if (is_array($attributes)) {
295  $attributesString = '';
296  foreach ($attributes as $name => $value) {
297  $attributesString .= ' ' . $name . '="' . $this->escaper->escapeHtml($value) . '"';
298  }
299  $attributes = $attributesString;
300  } else {
301  $attributes = ' ' . $attributes;
302  }
303  }
304  return $attributes;
305  }
306 
314  protected function addDefaultAttributes($contentType, $attributes)
315  {
316  switch ($contentType) {
317  case 'js':
318  $attributes = ' type="text/javascript" ' . $attributes;
319  break;
320 
321  case 'css':
322  $attributes = ' rel="stylesheet" type="text/css" ' . ($attributes ?: ' media="all"');
323  break;
324  }
325  return $attributes;
326  }
327 
335  protected function getAssetTemplate($contentType, $attributes)
336  {
337  switch ($contentType) {
338  case 'js':
339  $groupTemplate = '<script ' . $attributes . ' src="%s"></script>' . "\n";
340  break;
341 
342  case 'css':
343  default:
344  $groupTemplate = '<link ' . $attributes . ' href="%s" />' . "\n";
345  break;
346  }
347  return $groupTemplate;
348  }
349 
357  protected function processIeCondition($groupHtml, $group)
358  {
359  $ieCondition = $group->getProperty('ie_condition');
360  if (!empty($ieCondition)) {
361  $groupHtml = '<!--[if ' . $ieCondition . ']>' . "\n" . $groupHtml . '<![endif]-->' . "\n";
362  }
363  return $groupHtml;
364  }
365 
372  protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $group)
373  {
374  $assets = $this->processMerge($group->getAll(), $group);
375  $attributes = $this->getGroupAttributes($group);
376 
377  $result = '';
378  try {
380  foreach ($assets as $asset) {
381  $template = $this->getAssetTemplate(
382  $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE),
384  );
385  $result .= sprintf($template, $asset->getUrl());
386  }
387  } catch (LocalizedException $e) {
388  $this->logger->critical($e);
389  $result .= sprintf($template, $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']));
390  }
391  return $result;
392  }
393 
400  protected function getAssetContentType(\Magento\Framework\View\Asset\AssetInterface $asset)
401  {
402  return $asset->getContentType();
403  }
404 
410  public function getAvailableResultGroups()
411  {
412  return array_fill_keys($this->assetTypeOrder, '');
413  }
414 }
__construct(Config $pageConfig, \Magento\Framework\View\Asset\MergeService $assetMergeService, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\Escaper $escaper, \Magento\Framework\Stdlib\StringUtils $string, \Psr\Log\LoggerInterface $logger)
Definition: Renderer.php:63
$group
Definition: sections.phtml:16
renderAssetGroup(\Magento\Framework\View\Asset\PropertyGroup $group)
Definition: Renderer.php:259
$type
Definition: item.phtml:13
getAssetTemplate($contentType, $attributes)
Definition: Renderer.php:335
$value
Definition: gender.phtml:16
getAssetContentType(\Magento\Framework\View\Asset\AssetInterface $asset)
Definition: Renderer.php:400
$attributes
Definition: matrix.phtml:13
$method
Definition: info.phtml:13
addDefaultAttributes($contentType, $attributes)
Definition: Renderer.php:314
$template
Definition: export.php:12
if(!isset($_GET['name'])) $name
Definition: log.php:14