TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractStandaloneMessage.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
3 
6 
7 /*
8  * This file is part of the TYPO3 CMS project.
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
25 {
31  protected $htmlTemplate;
32 
38  protected $defaultMarkers = [];
39 
45  protected $markers = [];
46 
56  {
58  if (!empty($message)) {
59  $this->setMessage($message);
60  }
61  $this->setTitle(!empty($title) ? $title : 'Error!');
62  $this->setSeverity($severity);
63  }
64 
72  public function setMarkers(array $markers)
73  {
74  $this->markers = array_merge($this->markers, $markers);
75  }
76 
82  protected function getDefaultMarkers()
83  {
84  $classes = [
85  self::NOTICE => 'notice',
86  self::INFO => 'information',
87  self::OK => 'ok',
88  self::WARNING => 'warning',
89  self::ERROR => 'error'
90  ];
91  $defaultMarkers = [
92  '###CSS_CLASS###' => $classes[$this->severity],
93  '###TITLE###' => $this->title,
94  '###MESSAGE###' => $this->message,
95  // Avoid calling TYPO3_SITE_URL here to get the base URL as it might be that we output an exception message with
96  // invalid trusted host, which would lead to a nested exception! See: #30377
97  // Instead we calculate the relative path to the document root without involving HTTP request parameters.
98  '###BASEURL###' => substr(PATH_site, strlen(GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'))),
99  '###TYPO3_mainDir###' => TYPO3_mainDir,
100  '###TYPO3_copyright_year###' => TYPO3_copyright_year
101  ];
102  return $defaultMarkers;
103  }
104 
110  public function getHtmlTemplate()
111  {
112  if (!$this->htmlTemplate) {
113  throw new \RuntimeException('No HTML template file has been defined, yet', 1314390127);
114  }
115  return $this->htmlTemplate;
116  }
117 
125  {
126  $this->htmlTemplate = PATH_site . $htmlTemplate;
127  if (!file_exists($this->htmlTemplate)) {
128  throw new \RuntimeException('Template file "' . $this->htmlTemplate . '" not found', 1312830504);
129  }
130  }
131 
137  public function render()
138  {
139  $markers = array_merge($this->getDefaultMarkers(), $this->markers);
140  $content = file_get_contents($this->htmlTemplate);
141  $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
142  $content = $templateService->substituteMarkerArray($content, $markers, '', false, true);
143  return $content;
144  }
145 
151  public function output()
152  {
153  $content = $this->render();
154  echo $content;
155  }
156 }
static makeInstance($className,...$constructorArguments)
__construct($message= '', $title= '', $severity=AbstractMessage::ERROR)