annotate core/lib/Drupal/Core/Logger/LogMessageParser.php @ 19:fa3358dc1485 tip

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