You are here

public function LogMessageParser::parseMessagePlaceholders in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/Core/Logger/LogMessageParser.php \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders()

Parses and transforms message and its placeholders to a common format.

For a value to be considered as a placeholder should be in the following formats:

  • PSR3 format:

    • Drupal specific string placeholder format:

Values in PSR3 format will be transformed to SafeMarkup::format() format.

Parameters

string $message: The message that contains the placeholders. If the message is in PSR3 style, it will be transformed to \Drupal\Component\Utility\SafeMarkup::format() style.

array $context: An array that may or may not contain placeholder variables.

Return value

array An array of the extracted message placeholders.

Overrides LogMessageParserInterface::parseMessagePlaceholders

See also

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logg...

\Drupal\Component\Utility\SafeMarkup::format()

File

lib/Drupal/Core/Logger/LogMessageParser.php, line 18
Contains \Drupal\Core\Logger\LogMessageParser.

Class

LogMessageParser
Parses log messages and their placeholders.

Namespace

Drupal\Core\Logger

Code

public function parseMessagePlaceholders(&$message, array &$context) {
  $variables = array();
  $has_psr3 = FALSE;
  if (($start = strpos($message, '{')) !== FALSE && strpos($message, '}') > $start) {
    $has_psr3 = TRUE;

    // Transform PSR3 style messages containing placeholders to
    // \Drupal\Component\Utility\SafeMarkup::format() style.
    $message = preg_replace('/\\{(.*)\\}/U', '@$1', $message);
  }
  foreach ($context as $key => $variable) {

    // PSR3 style placeholders.
    if ($has_psr3) {

      // Keys are not prefixed with anything according to PSR3 specs.
      // If the message is "User {username} created" the variable key will be
      // just "username".
      if (strpos($message, '@' . $key) !== FALSE) {
        $key = '@' . $key;
      }
    }
    if (!empty($key) && ($key[0] === '@' || $key[0] === '%' || $key[0] === '!')) {

      // The key is now in \Drupal\Component\Utility\SafeMarkup::format() style.
      $variables[$key] = $variable;
    }
  }
  return $variables;
}