‪TYPO3CMS  ‪main
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 {
30  protected function ‪unserializeLogData(mixed $logData): ?array
31  {
32  // The @ symbol avoids an E_NOTICE when unserialize() fails
33  $cleanedUpData = @unserialize((string)$logData, ['allowed_classes' => false]);
34  if ($cleanedUpData === false) {
35  $cleanedUpData = json_decode((string)$logData, true);
36  }
37  return is_array($cleanedUpData) ? $cleanedUpData : null;
38  }
39 
43  protected function ‪formatLogDetails(string $detailString, mixed $substitutes): string
44  {
45  if (!is_array($substitutes)) {
46  $substitutes = $this->unserializeLogData($substitutes) ?? [];
47  }
48  return self::formatLogDetailsStatic($detailString, $substitutes);
49  }
50 
56  protected static function ‪formatLogDetailsStatic(string $detailString, array $substitutes): string
57  {
58  // Handle legacy "%s" placeholders
59  if (str_contains($detailString, '%')) {
60  $detailString = vsprintf($detailString, $substitutes);
61  } elseif ($substitutes !== []) {
62  // Handles placeholders with "{myPlaceholder}"
63  $detailString = preg_replace_callback('/{([A-z]+)}/', static function ($matches) use ($substitutes) {
64  // $matches[0] contains the unsubstituted placeholder
65  return $substitutes[$matches[1] ?? null] ?? $matches[0];
66  }, $detailString);
67  }
68  // Remove possible pending other %s
69  return str_replace('%s', '', (string)$detailString);
70  }
71 }
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetails
‪formatLogDetails(string $detailString, mixed $substitutes)
Definition: LogDataTrait.php:43
‪TYPO3\CMS\Core\Log
Definition: Channel.php:18
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetailsStatic
‪static formatLogDetailsStatic(string $detailString, array $substitutes)
Definition: LogDataTrait.php:56
‪TYPO3\CMS\Core\Log\LogDataTrait\unserializeLogData
‪unserializeLogData(mixed $logData)
Definition: LogDataTrait.php:30
‪TYPO3\CMS\Core\Log\LogDataTrait
Definition: LogDataTrait.php:25