[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/DataCollector/ -> MessagesCollector.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\DataCollector;
  12  
  13  use Psr\Log\AbstractLogger;
  14  use DebugBar\DataFormatter\DataFormatterInterface;
  15  use DebugBar\DataFormatter\DebugBarVarDumper;
  16  
  17  /**
  18   * Provides a way to log messages
  19   */
  20  class MessagesCollector extends AbstractLogger implements DataCollectorInterface, MessagesAggregateInterface, Renderable, AssetProvider
  21  {
  22      protected $name;
  23  
  24      protected $messages = array();
  25  
  26      protected $aggregates = array();
  27  
  28      protected $dataFormater;
  29  
  30      protected $varDumper;
  31  
  32      // The HTML var dumper requires debug bar users to support the new inline assets, which not all
  33      // may support yet - so return false by default for now.
  34      protected $useHtmlVarDumper = false;
  35  
  36      /**
  37       * @param string $name
  38       */
  39      public function __construct($name = 'messages')
  40      {
  41          $this->name = $name;
  42      }
  43  
  44      /**
  45       * Sets the data formater instance used by this collector
  46       *
  47       * @param DataFormatterInterface $formater
  48       * @return $this
  49       */
  50      public function setDataFormatter(DataFormatterInterface $formater)
  51      {
  52          $this->dataFormater = $formater;
  53          return $this;
  54      }
  55  
  56      /**
  57       * @return DataFormatterInterface
  58       */
  59      public function getDataFormatter()
  60      {
  61          if ($this->dataFormater === null) {
  62              $this->dataFormater = DataCollector::getDefaultDataFormatter();
  63          }
  64          return $this->dataFormater;
  65      }
  66  
  67      /**
  68       * Sets the variable dumper instance used by this collector
  69       *
  70       * @param DebugBarVarDumper $varDumper
  71       * @return $this
  72       */
  73      public function setVarDumper(DebugBarVarDumper $varDumper)
  74      {
  75          $this->varDumper = $varDumper;
  76          return $this;
  77      }
  78  
  79      /**
  80       * Gets the variable dumper instance used by this collector
  81       *
  82       * @return DebugBarVarDumper
  83       */
  84      public function getVarDumper()
  85      {
  86          if ($this->varDumper === null) {
  87              $this->varDumper = DataCollector::getDefaultVarDumper();
  88          }
  89          return $this->varDumper;
  90      }
  91  
  92      /**
  93       * Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
  94       * rich variable rendering.  Be sure to set this flag before logging any messages for the
  95       * first time.
  96       *
  97       * @param bool $value
  98       * @return $this
  99       */
 100      public function useHtmlVarDumper($value = true)
 101      {
 102          $this->useHtmlVarDumper = $value;
 103          return $this;
 104      }
 105  
 106      /**
 107       * Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
 108       * rendering.
 109       *
 110       * @return mixed
 111       */
 112      public function isHtmlVarDumperUsed()
 113      {
 114          return $this->useHtmlVarDumper;
 115      }
 116  
 117      /**
 118       * Adds a message
 119       *
 120       * A message can be anything from an object to a string
 121       *
 122       * @param mixed $message
 123       * @param string $label
 124       */
 125      public function addMessage($message, $label = 'info', $isString = true)
 126      {
 127          $messageText = $message;
 128          $messageHtml = null;
 129          if (!is_string($message)) {
 130              // Send both text and HTML representations; the text version is used for searches
 131              $messageText = $this->getDataFormatter()->formatVar($message);
 132              if ($this->isHtmlVarDumperUsed()) {
 133                  $messageHtml = $this->getVarDumper()->renderVar($message);
 134              }
 135              $isString = false;
 136          }
 137          $this->messages[] = array(
 138              'message' => $messageText,
 139              'message_html' => $messageHtml,
 140              'is_string' => $isString,
 141              'label' => $label,
 142              'time' => microtime(true)
 143          );
 144      }
 145  
 146      /**
 147       * Aggregates messages from other collectors
 148       *
 149       * @param MessagesAggregateInterface $messages
 150       */
 151      public function aggregate(MessagesAggregateInterface $messages)
 152      {
 153          $this->aggregates[] = $messages;
 154      }
 155  
 156      /**
 157       * @return array
 158       */
 159      public function getMessages()
 160      {
 161          $messages = $this->messages;
 162          foreach ($this->aggregates as $collector) {
 163              $msgs = array_map(function ($m) use ($collector) {
 164                  $m['collector'] = $collector->getName();
 165                  return $m;
 166              }, $collector->getMessages());
 167              $messages = array_merge($messages, $msgs);
 168          }
 169  
 170          // sort messages by their timestamp
 171          usort($messages, function ($a, $b) {
 172              if ($a['time'] === $b['time']) {
 173                  return 0;
 174              }
 175              return $a['time'] < $b['time'] ? -1 : 1;
 176          });
 177  
 178          return $messages;
 179      }
 180  
 181      /**
 182       * @param $level
 183       * @param $message
 184       * @param array $context
 185       */
 186      public function log($level, $message, array $context = array()): void
 187      {
 188          // For string messages, interpolate the context following PSR-3
 189          if (is_string($message)) {
 190              $message = $this->interpolate($message, $context);
 191          }
 192          $this->addMessage($message, $level);
 193      }
 194  
 195      /**
 196       * Interpolates context values into the message placeholders.
 197       *
 198       * @param $message
 199       * @param array $context
 200       * @return string
 201       */
 202      function interpolate($message, array $context = array())
 203      {
 204          // build a replacement array with braces around the context keys
 205          $replace = array();
 206          foreach ($context as $key => $val) {
 207              // check that the value can be cast to string
 208              if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
 209                  $replace['{' . $key . '}'] = $val;
 210              }
 211          }
 212  
 213          // interpolate replacement values into the message and return
 214          return strtr($message, $replace);
 215      }
 216  
 217      /**
 218       * Deletes all messages
 219       */
 220      public function clear()
 221      {
 222          $this->messages = array();
 223      }
 224  
 225      /**
 226       * @return array
 227       */
 228      public function collect()
 229      {
 230          $messages = $this->getMessages();
 231          return array(
 232              'count' => count($messages),
 233              'messages' => $messages
 234          );
 235      }
 236  
 237      /**
 238       * @return string
 239       */
 240      public function getName()
 241      {
 242          return $this->name;
 243      }
 244  
 245      /**
 246       * @return array
 247       */
 248      public function getAssets() {
 249          return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : array();
 250      }
 251  
 252      /**
 253       * @return array
 254       */
 255      public function getWidgets()
 256      {
 257          $name = $this->getName();
 258          return array(
 259              "$name" => array(
 260                  'icon' => 'list-alt',
 261                  "widget" => "PhpDebugBar.Widgets.MessagesWidget",
 262                  "map" => "$name.messages",
 263                  "default" => "[]"
 264              ),
 265              "$name:badge" => array(
 266                  "map" => "$name.count",
 267                  "default" => "null"
 268              )
 269          );
 270      }
 271  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer