function LogMessageParser::parseMessagePlaceholders

Same name in other branches
  1. 9 core/lib/Drupal/Core/Logger/LogMessageParser.php \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders()
  2. 10 core/lib/Drupal/Core/Logger/LogMessageParser.php \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders()
  3. 11.x core/lib/Drupal/Core/Logger/LogMessageParser.php \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders()

Overrides LogMessageParserInterface::parseMessagePlaceholders

File

core/lib/Drupal/Core/Logger/LogMessageParser.php, line 13

Class

LogMessageParser
Parses log messages and their placeholders.

Namespace

Drupal\Core\Logger

Code

public function parseMessagePlaceholders(&$message, array &$context) {
    $variables = [];
    $has_psr3 = FALSE;
    if (($start = strpos($message, '{')) !== FALSE && strpos($message, '}') > $start) {
        $has_psr3 = TRUE;
        // Transform PSR3 style messages containing placeholders to
        // \Drupal\Component\Render\FormattableMarkup 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\Render\FormattableMarkup style.
            $variables[$key] = $variable;
        }
    }
    return $variables;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.