LogMessageParser.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Drupal\Core\Logger;
  3. /**
  4. * Parses log messages and their placeholders.
  5. */
  6. class LogMessageParser implements LogMessageParserInterface {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public function parseMessagePlaceholders(&$message, array &$context) {
  11. $variables = [];
  12. $has_psr3 = FALSE;
  13. if (($start = strpos($message, '{')) !== FALSE && strpos($message, '}') > $start) {
  14. $has_psr3 = TRUE;
  15. // Transform PSR3 style messages containing placeholders to
  16. // \Drupal\Component\Render\FormattableMarkup style.
  17. $message = preg_replace('/\{(.*)\}/U', '@$1', $message);
  18. }
  19. foreach ($context as $key => $variable) {
  20. // PSR3 style placeholders.
  21. if ($has_psr3) {
  22. // Keys are not prefixed with anything according to PSR3 specs.
  23. // If the message is "User {username} created" the variable key will be
  24. // just "username".
  25. if (strpos($message, '@' . $key) !== FALSE) {
  26. $key = '@' . $key;
  27. }
  28. }
  29. if (!empty($key) && ($key[0] === '@' || $key[0] === '%' || $key[0] === '!')) {
  30. // The key is now in \Drupal\Component\Render\FormattableMarkup style.
  31. $variables[$key] = $variable;
  32. }
  33. }
  34. return $variables;
  35. }
  36. }