‪TYPO3CMS  ‪main
AbstractWriter.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
24 abstract class ‪AbstractWriter implements ‪WriterInterface
25 {
27 
34  public function ‪__construct(array $options = [])
35  {
36  foreach ($options as $optionKey => $optionValue) {
37  $methodName = 'set' . ucfirst($optionKey);
38  if (method_exists($this, $methodName)) {
39  $this->{$methodName}($optionValue);
40  } else {
41  throw new ‪InvalidLogWriterConfigurationException('Invalid LogWriter configuration option "' . $optionKey . '" for log writer of type "' . static::class . '"', 1321696152);
42  }
43  }
44  }
45 
49  protected function ‪interpolate(string $message, array $context = []): string
50  {
51  // Build a replacement array with braces around the context keys.
52  $replace = [];
53  foreach ($context as $key => $val) {
54  if (!is_array($val) && !is_null($val) && (!is_object($val) || method_exists($val, '__toString'))) {
55  $replace['{' . $key . '}'] = $this->‪formatContextValue($val);
56  }
57  }
58 
59  // Interpolate replacement values into the message and return.
60  return strtr($message, $replace);
61  }
62 
71  protected function ‪formatContextValue(string $value): string
72  {
73  return $value;
74  }
75 
82  protected function ‪formatException(\Throwable $ex): string
83  {
84  $classname = get_class($ex);
85  if ($pos = strrpos($classname, '\\')) {
86  $classname = substr($classname, $pos + 1);
87  }
88 
89  return sprintf(
90  '- %s: %s, in file %s:%s',
91  $classname,
92  $ex->getMessage(),
93  $ex->getFile(),
94  $ex->getLine(),
95  );
96  }
97 }
‪TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException
Definition: InvalidLogWriterConfigurationException.php:23
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter\formatContextValue
‪formatContextValue(string $value)
Definition: AbstractWriter.php:71
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter\__construct
‪__construct(array $options=[])
Definition: AbstractWriter.php:34
‪TYPO3\CMS\Core\Log\Writer\WriterInterface
Definition: WriterInterface.php:24
‪TYPO3\CMS\Core\Log\Writer
Definition: AbstractWriter.php:16
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter\interpolate
‪interpolate(string $message, array $context=[])
Definition: AbstractWriter.php:49
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter
Definition: AbstractWriter.php:25
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter\formatException
‪formatException(\Throwable $ex)
Definition: AbstractWriter.php:82