TYPO3 CMS  TYPO3_8-7
AbstractStandaloneMessage.php
Go to the documentation of this file.
1 <?php
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 
71  public function setMarkers(array $markers)
72  {
73  $this->markers = array_merge($this->markers, $markers);
74  }
75 
81  protected function getDefaultMarkers()
82  {
83  $classes = [
84  self::NOTICE => 'notice',
85  self::INFO => 'information',
86  self::OK => 'ok',
87  self::WARNING => 'warning',
88  self::ERROR => 'error'
89  ];
90  $defaultMarkers = [
91  '###CSS_CLASS###' => $classes[$this->severity],
92  '###TITLE###' => $this->title,
93  '###MESSAGE###' => $this->message,
94  // Avoid calling TYPO3_SITE_URL here to get the base URL as it might be that we output an exception message with
95  // invalid trusted host, which would lead to a nested exception! See: #30377
96  // Instead we calculate the relative path to the document root without involving HTTP request parameters.
97  '###BASEURL###' => substr(PATH_site, strlen(GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'))),
98  '###TYPO3_mainDir###' => TYPO3_mainDir,
99  '###TYPO3_copyright_year###' => TYPO3_copyright_year
100  ];
101  return $defaultMarkers;
102  }
103 
109  public function getHtmlTemplate()
110  {
111  if (!$this->htmlTemplate) {
112  throw new \RuntimeException('No HTML template file has been defined, yet', 1314390127);
113  }
114  return $this->htmlTemplate;
115  }
116 
123  {
124  $this->htmlTemplate = PATH_site . $htmlTemplate;
125  if (!file_exists($this->htmlTemplate)) {
126  throw new \RuntimeException('Template file "' . $this->htmlTemplate . '" not found', 1312830504);
127  }
128  }
129 
135  public function render()
136  {
137  $markers = array_merge($this->getDefaultMarkers(), $this->markers);
138  $content = file_get_contents($this->htmlTemplate);
139  $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
140  $content = $templateService->substituteMarkerArray($content, $markers, '', false, true);
141  return $content;
142  }
143 
147  public function output()
148  {
149  $content = $this->render();
150  echo $content;
151  }
152 }
__construct($message='', $title='', $severity=AbstractMessage::ERROR)
static makeInstance($className,... $constructorArguments)