From a9b3013907d43cbe3323005bf026a469eaa2d851 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Fri, 9 Dec 2022 14:14:22 +1300 Subject: [PATCH 1/3] DEP PHP Support in CMS5 --- .github/workflows/ci.yml | 4 ++++ code/DebugBar.php | 4 ++-- code/Messages/LogFormatter.php | 3 ++- composer.json | 14 +++++++------- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab46643..d5e6ee6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,3 +14,7 @@ jobs: # Only run cron on the silverstripe account if: (github.event_name == 'schedule' && github.repository_owner == 'lekoala') || (github.event_name != 'schedule') uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 + with: + extra_jobs: | + - composer_require_extra: silverstripe/subsites:^3 + name_suffix: subsites diff --git a/code/DebugBar.php b/code/DebugBar.php index 5ff2c24..7aa32a6 100644 --- a/code/DebugBar.php +++ b/code/DebugBar.php @@ -18,7 +18,7 @@ use 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; @@ -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)); diff --git a/code/Messages/LogFormatter.php b/code/Messages/LogFormatter.php index 286a499..6ec56aa 100644 --- a/code/Messages/LogFormatter.php +++ b/code/Messages/LogFormatter.php @@ -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']; } diff --git a/composer.json b/composer.json index 8923894..59b6e85 100644 --- a/composer.json +++ b/composer.json @@ -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": { From 9ec1439181c04cead9256650238d1d16d47c021b Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Wed, 18 Jan 2023 13:03:08 +1300 Subject: [PATCH 2/3] Add MonologCollector class to avoid failed tests --- .github/workflows/ci.yml | 4 -- code/Bridge/MonologCollector.php | 108 +++++++++++++++++++++++++++++++ code/DebugBar.php | 2 +- 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 code/Bridge/MonologCollector.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d5e6ee6..ab46643 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,3 @@ jobs: # Only run cron on the silverstripe account if: (github.event_name == 'schedule' && github.repository_owner == 'lekoala') || (github.event_name != 'schedule') uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 - with: - extra_jobs: | - - composer_require_extra: silverstripe/subsites:^3 - name_suffix: subsites diff --git a/code/Bridge/MonologCollector.php b/code/Bridge/MonologCollector.php new file mode 100644 index 0000000..e4841a4 --- /dev/null +++ b/code/Bridge/MonologCollector.php @@ -0,0 +1,108 @@ + + * $debugbar->addCollector(new MonologCollector($logger)); + * + */ +class MonologCollector extends AbstractProcessingHandler implements DataCollectorInterface, Renderable, MessagesAggregateInterface +{ + 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" + ) + ); + } +} diff --git a/code/DebugBar.php b/code/DebugBar.php index 7aa32a6..5944f93 100644 --- a/code/DebugBar.php +++ b/code/DebugBar.php @@ -15,7 +15,7 @@ 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 Symfony\Component\Mailer\MailerInterface; From abd1a29cbde5c7edc5b2331aff873fc656106d70 Mon Sep 17 00:00:00 2001 From: Sabina Talipova <87288324+sabina-talipova@users.noreply.github.com> Date: Wed, 18 Jan 2023 16:04:42 +1300 Subject: [PATCH 3/3] Update phpdocs code/Bridge/MonologCollector.php Co-authored-by: Steve Boyd --- code/Bridge/MonologCollector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/Bridge/MonologCollector.php b/code/Bridge/MonologCollector.php index e4841a4..e7b842a 100644 --- a/code/Bridge/MonologCollector.php +++ b/code/Bridge/MonologCollector.php @@ -13,7 +13,7 @@ * 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. + * this class should be deprecated and the DebugBar\Bridge\MonologCollector class should be used instead of this * * $debugbar->addCollector(new MonologCollector($logger)); *