- md5
- 547dc2912b08146befb3091b9320ecd3
- sha1
- 6173c04d4596587fd87498f6960980eccbb84a69
- sha256
- 310692fc35f5ee409f102504f1f22aaeb41e8dfa6f50467f48db3bf6985df983
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\DataCollector;
use Psr\Log\AbstractLogger;
use DebugBar\DataFormatter\DataFormatterInterface;
use DebugBar\DataFormatter\DebugBarVarDumper;
/**
* Provides a way to log messages
*/
class MessagesCollector extends AbstractLogger implements DataCollectorInterface, MessagesAggregateInterface, Renderable, AssetProvider
{
protected $name;
protected $messages = array();
protected $aggregates = array();
protected $dataFormater;
protected $varDumper;
// The HTML var dumper requires debug bar users to support the new inline assets, which not all
// may support yet - so return false by default for now.
protected $useHtmlVarDumper = false;
/**
* @param string $name
*/
public function __construct($name = 'messages')
{
$this->name = $name;
}
/**
* Sets the data formater instance used by this collector
*
* @param DataFormatterInterface $formater
* @return $this
*/
public function setDataFormatter(DataFormatterInterface $formater)
{
$this->dataFormater = $formater;
return $this;
}
/**
* @return DataFormatterInterface
*/
public function getDataFormatter()
{
if ($this->dataFormater === null) {
$this->dataFormater = DataCollector::getDefaultDataFormatter();
}
return $this->dataFormater;
}
/**
* Sets the variable dumper instance used by this collector
*
* @param DebugBarVarDumper $varDumper
* @return $this
*/
public function setVarDumper(DebugBarVarDumper $varDumper)
{
$this->varDumper = $varDumper;
return $this;
}
/**
* Gets the variable dumper instance used by this collector
*
* @return DebugBarVarDumper
*/
public function getVarDumper()
{
if ($this->varDumper === null) {
$this->varDumper = DataCollector::getDefaultVarDumper();
}
return $this->varDumper;
}
/**
* Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
* rich variable rendering. Be sure to set this flag before logging any messages for the
* first time.
*
* @param bool $value
* @return $this
*/
public function useHtmlVarDumper($value = true)
{
$this->useHtmlVarDumper = $value;
return $this;
}
/**
* Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
* rendering.
*
* @return mixed
*/
public function isHtmlVarDumperUsed()
{
return $this->useHtmlVarDumper;
}
/**
* Adds a message
*
* A message can be anything from an object to a string
*
* @param mixed $message
* @param string $label
*/
public function addMessage($message, $label = 'info', $isString = true)
{
$messageText = $message;
$messageHtml = null;
if (!is_string($message)) {
// Send both text and HTML representations; the text version is used for searches
$messageText = $this->getDataFormatter()->formatVar($message);
if ($this->isHtmlVarDumperUsed()) {
$messageHtml = $this->getVarDumper()->renderVar($message);
}
$isString = false;
}
$this->messages[] = array(
'message' => $messageText,
'message_html' => $messageHtml,
'is_string' => $isString,
'label' => $label,
'time' => microtime(true)
);
}
/**
* Aggregates messages from other collectors
*
* @param MessagesAggregateInterface $messages
*/
public function aggregate(MessagesAggregateInterface $messages)
{
$this->aggregates[] = $messages;
}
/**
* @return array
*/
public function getMessages()
{
$messages = $this->messages;
foreach ($this->aggregates as $collector) {
$msgs = array_map(function ($m) use ($collector) {
$m['collector'] = $collector->getName();
return $m;
}, $collector->getMessages());
$messages = array_merge($messages, $msgs);
}
// sort messages by their timestamp
usort($messages, function ($a, $b) {
if ($a['time'] === $b['time']) {
return 0;
}
return $a['time'] < $b['time'] ? -1 : 1;
});
return $messages;
}
/**
* @param $level
* @param $message
* @param array $context
*/
public function log($level, $message, array $context = array()): void
{
// For string messages, interpolate the context following PSR-3
if (is_string($message)) {
$message = $this->interpolate($message, $context);
}
$this->addMessage($message, $level);
}
/**
* Interpolates context values into the message placeholders.
*
* @param $message
* @param array $context
* @return string
*/
function interpolate($message, array $context = array())
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
// check that the value can be cast to string
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
/**
* Deletes all messages
*/
public function clear()
{
$this->messages = array();
}
/**
* @return array
*/
public function collect()
{
$messages = $this->getMessages();
return array(
'count' => count($messages),
'messages' => $messages
);
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return array
*/
public function getAssets() {
return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : array();
}
/**
* @return array
*/
public function getWidgets()
{
$name = $this->getName();
return array(
"$name" => array(
'icon' => 'list-alt',
"widget" => "PhpDebugBar.Widgets.MessagesWidget",
"map" => "$name.messages",
"default" => "[]"
),
"$name:badge" => array(
"map" => "$name.count",
"default" => "null"
)
);
}
}
The same content is in 131 releases, Joomla 4.0.5-rc1 to 5.1.0-alpha4. See them all, or every version of this file.