‪TYPO3CMS  11.5
LogDataTrait.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Log;
19 
25 {
33  protected function ‪unserializeLogData($logData): ?array
34  {
35  // The @ symbol avoids an E_NOTICE when unserialize() fails
36  $cleanedUpData = @unserialize((string)$logData, ['allowed_classes' => false]);
37  if ($cleanedUpData === false) {
38  $cleanedUpData = json_decode((string)$logData, true);
39  }
40  return is_array($cleanedUpData) ? $cleanedUpData : null;
41  }
42 
50  protected function ‪formatLogDetails(string $detailString, $substitutes): string
51  {
52  if (!is_array($substitutes)) {
53  $substitutes = $this->unserializeLogData($substitutes) ?? [];
54  }
55  return self::formatLogDetailsStatic($detailString, $substitutes);
56  }
57 
63  protected static function ‪formatLogDetailsStatic(string $detailString, array $substitutes): string
64  {
65  // Handle legacy "%s" placeholders
66  if (str_contains($detailString, '%')) {
67  $detailString = vsprintf($detailString, $substitutes);
68  } elseif ($substitutes !== []) {
69  // Handles placeholders with "{myPlaceholder}"
70  $detailString = preg_replace_callback('/{([A-z]+)}/', static function ($matches) use ($substitutes) {
71  // $matches[0] contains the unsubstituted placeholder
72  return $substitutes[$matches[1] ?? null] ?? $matches[0];
73  }, $detailString);
74  }
75  // Remove possible pending other %s
76  return str_replace('%s', '', (string)$detailString);
77  }
78 }
‪TYPO3\CMS\Core\Log
Definition: Channel.php:18
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetailsStatic
‪static formatLogDetailsStatic(string $detailString, array $substitutes)
Definition: LogDataTrait.php:63
‪TYPO3\CMS\Core\Log\LogDataTrait\unserializeLogData
‪array null unserializeLogData($logData)
Definition: LogDataTrait.php:33
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetails
‪string formatLogDetails(string $detailString, $substitutes)
Definition: LogDataTrait.php:50
‪TYPO3\CMS\Core\Log\LogDataTrait
Definition: LogDataTrait.php:25