Passed
Push — master ( 8ada89...3fca1b )
by
unknown
14:42
created

AbstractWriter::formatContextValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
16
namespace TYPO3\CMS\Core\Log\Writer;
17
18
use TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException;
19
use TYPO3\CMS\Core\Security\BlockSerializationTrait;
20
21
/**
22
 * Abstract implementation of a log writer
23
 */
24
abstract class AbstractWriter implements WriterInterface
25
{
26
    use BlockSerializationTrait;
27
28
    /**
29
     * Constructs this log writer
30
     *
31
     * @param array $options Configuration options - depends on the actual log writer
32
     * @throws \TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException
33
     */
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
46
    /**
47
     * Interpolates context values into the message placeholders.
48
     */
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
63
    /**
64
     * Escape or quote a value from the context appropriate for the output.
65
     *
66
     * Note: In some output cases, escaping should not be done here but later on output,
67
     * such as if it's being written to a database for later display.
68
     *
69
     * @param string $value
70
     * @return string
71
     */
72
    protected function formatContextValue(string $value): string
73
    {
74
        return $value;
75
    }
76
77
    /**
78
     * Formats an exception into a string.
79
     *
80
     * The format here is nearly the same as just casting an exception to a string,
81
     * but omits the full class namespace and stack trace, as those get very long.
82
     *
83
     * @param \Exception $ex
84
     * @return string
85
     */
86
    protected function formatException(\Exception $ex): string
87
    {
88
        $classname = get_class($ex);
89
        if ($pos = strrpos($classname, '\\')) {
90
            $classname = substr($classname, $pos + 1);
91
        }
92
93
        return sprintf(
94
            '- %s: %s, in file %s:%s',
95
            $classname,
96
            $ex->getMessage(),
97
            $ex->getFile(),
98
            $ex->getLine(),
99
        );
100
    }
101
}
102