Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEP PHP Support in CMS5 #144

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions code/Bridge/MonologCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace LeKoala\DebugBar\Bridge;

use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\MessagesAggregateInterface;
use DebugBar\DataCollector\Renderable;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\LogRecord;

/**
* A monolog handler as well as a data collector. Based on DebugBar\Bridge\MonologCollector
* Note: This class is a temporary solution to keep existing dependencies working.
* As soon as maximebf/php-debugbar is updated and compatible with monolog/monolog:^3
* this class will be deprecated and removed immediately.
sabina-talipova marked this conversation as resolved.
Show resolved Hide resolved
* <code>
* $debugbar->addCollector(new MonologCollector($logger));
* </code>
*/
class MonologCollector extends AbstractProcessingHandler implements DataCollectorInterface, Renderable, MessagesAggregateInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we copy pasting this in from https://github.com/maximebf/php-debugbar/blob/master/src/DebugBar/Bridge/MonologCollector.php?

"maximebf/debugbar": "^1.18", which should be PHP 8.1 compatible, is already included in composer.json?

Copy link
Collaborator Author

@sabina-talipova sabina-talipova Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this method declaration https://github.com/maximebf/php-debugbar/blob/v1.18.1/src/DebugBar/Bridge/MonologCollector.php#L62 is not compatible with version of monolog/monolog (see here) that we use Monolog\Handler\AbstractProcessingHandler::write(Monolog\LogRecord $record): void. It still fail our tests https://github.com/lekoala/silverstripe-debugbar/actions/runs/3934516960/jobs/6729336460#step:12:62.

This issue was already created php-debugbar/php-debugbar#514

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, probably need to do this then to workout this issue

Could you please put a class docblock explaining the reason why this class exists, and mention that it should be deprecated and stop being used once the maximebf/php-debugbar works with monolog/monolog:^3

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE

{
protected $name;

protected $records = array();

/**
* @param Logger $logger
* @param int $level
* @param boolean $bubble
* @param string $name
*/
public function __construct(Logger $logger = null, $level = Logger::DEBUG, $bubble = true, $name = 'monolog')
{
parent::__construct($level, $bubble);
$this->name = $name;
if ($logger !== null) {
$this->addLogger($logger);
}
}

/**
* Adds logger which messages you want to log
*
* @param Logger $logger
*/
public function addLogger(Logger $logger)
{
$logger->pushHandler($this);
}

protected function write(LogRecord $record): void
{
$this->records[] = array(
'message' => $record['formatted'],
'is_string' => true,
'label' => strtolower($record['level_name']),
'time' => $record['datetime']->format('U')
);
}

/**
* @return array
*/
public function getMessages()
{
return $this->records;
}

/**
* @return array
*/
public function collect()
{
return array(
'count' => count($this->records),
'records' => $this->records
);
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array
*/
public function getWidgets()
{
$name = $this->getName();
return array(
$name => array(
"icon" => "suitcase",
"widget" => "PhpDebugBar.Widgets.MessagesWidget",
"map" => "$name.records",
"default" => "[]"
),
"$name:badge" => array(
"map" => "$name.count",
"default" => "null"
)
);
}
}
6 changes: 3 additions & 3 deletions code/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\View\Requirements;
use SilverStripe\Control\Controller;
use DebugBar\Bridge\MonologCollector;
use LeKoala\DebugBar\Bridge\MonologCollector;
use SilverStripe\Control\HTTPRequest;
use DebugBar\DebugBar as BaseDebugBar;
use SilverStripe\Control\Email\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Config\ConfigLoader;
use SilverStripe\Core\Config\Configurable;
Expand Down Expand Up @@ -208,7 +208,7 @@ public static function initDebugBar()

// Email logging
if (self::config()->email_collector) {
$mailer = Injector::inst()->get(Mailer::class);
$mailer = Injector::inst()->get(MailerInterface::class);
if ($mailer instanceof SwiftMailer) {
$swiftInst = $mailer->getSwiftMailer();
$debugbar['messages']->aggregate(new SwiftLogCollector($swiftInst));
Expand Down
3 changes: 2 additions & 1 deletion code/Messages/LogFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace LeKoala\DebugBar\Messages;

use Monolog\Formatter\FormatterInterface;
use Monolog\LogRecord;

/**
* Formats incoming log messages for display in the debug bar
*/
class LogFormatter implements FormatterInterface
{
public function format(array $record)
public function format(array|LogRecord $record)
{
return $record['message'];
}
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"silverstripe/framework": "^4.0",
"maximebf/debugbar": "^1.13",
"php": "^8.1",
"silverstripe/framework": "^5",
"maximebf/debugbar": "^1.18",
"jdorn/sql-formatter": "1.3.x-dev",
"tractorcow/silverstripe-proxy-db": "^1.0"
"silverstripe/silverstripe-proxy-db": "^2"
},
"require-dev": {
"silverstripe/siteconfig": "^4.0",
"silverstripe/admin": "^1.0",
"silverstripe/siteconfig": "^5",
"silverstripe/admin": "^2",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3"
},
"extra": {
"branch-alias": {
Expand Down