From 9fc979407b8744ac3af8639512122434d948a8bf Mon Sep 17 00:00:00 2001 From: "y2ksoft@gmail.com" Date: Thu, 2 Mar 2023 12:42:32 +1300 Subject: [PATCH] CMS 5 upgrade --- composer.json | 20 ++++++++++------ src/Admin/LogViewerAdmin.php | 17 +++++++------ .../GridField/GridFieldClearAllButton.php | 14 ++++------- src/Handler/DataObjectHandler.php | 17 ++++++------- src/Model/LogEntry.php | 24 +++++++++---------- src/Task/RemoveOldLogEntriesTask.php | 13 +++++----- 6 files changed, 55 insertions(+), 50 deletions(-) diff --git a/composer.json b/composer.json index 24d2b2c..6538659 100644 --- a/composer.json +++ b/composer.json @@ -13,14 +13,14 @@ } ], "require": { - "silverstripe/admin": "^1", - "silverstripe/framework": "^4", - "monolog/monolog": "~1.11", - "silverstripe/crontask": "^2" + "silverstripe/admin": "^2.0.0-beta1", + "silverstripe/framework": "^5", + "monolog/monolog": "^3.3.1", + "silverstripe/crontask": "^3.0.0-beta1" }, "require-dev": { - "phpunit/phpunit": "^5.7", - "squizlabs/php_codesniffer": "^3" + "phpunit/phpunit": "^9.5", + "squizlabs/php_codesniffer": "^3.7.2" }, "autoload": { "psr-4": { @@ -37,5 +37,11 @@ ] }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "config": { + "allow-plugins": { + "composer/installers": true, + "silverstripe/vendor-plugin": true + } + } } diff --git a/src/Admin/LogViewerAdmin.php b/src/Admin/LogViewerAdmin.php index 30e85d8..c5fcf4a 100644 --- a/src/Admin/LogViewerAdmin.php +++ b/src/Admin/LogViewerAdmin.php @@ -5,7 +5,10 @@ use SilverLeague\LogViewer\Model\LogEntry; use SilverLeague\LogViewer\Forms\GridField\GridFieldClearAllButton; use SilverStripe\Admin\ModelAdmin; +use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridFieldAddNewButton; +use SilverStripe\ORM\DataList; +use SilverStripe\ORM\DataObject; use SilverStripe\View\Requirements; /** @@ -16,13 +19,13 @@ */ class LogViewerAdmin extends ModelAdmin { - private static $url_segment = 'logs'; + private static string $url_segment = 'logs'; - private static $menu_title = 'Logs'; + private static string $menu_title = 'Logs'; - private static $menu_icon_class = 'font-icon-list'; + private static string $menu_icon_class = 'font-icon-list'; - private static $managed_models = [ + private static array $managed_models = [ LogEntry::class ]; @@ -44,7 +47,7 @@ protected function init() * * {@inheritDoc} */ - public function getEditForm($id = null, $fields = null) + public function getEditForm($id = null, $fields = null): Form { $form = parent::getEditForm($id, $fields); @@ -63,7 +66,7 @@ public function getEditForm($id = null, $fields = null) * * @return string */ - public function getGridFieldName() + public function getGridFieldName(): string { return $this->sanitiseClassName($this->modelClass); } @@ -73,7 +76,7 @@ public function getGridFieldName() * * {@inheritDoc} */ - public function getList() + public function getList(): DataList { $list = parent::getList(); return $list->sort(['Created' => 'DESC']); diff --git a/src/Forms/GridField/GridFieldClearAllButton.php b/src/Forms/GridField/GridFieldClearAllButton.php index 54f1557..4150298 100644 --- a/src/Forms/GridField/GridFieldClearAllButton.php +++ b/src/Forms/GridField/GridFieldClearAllButton.php @@ -16,19 +16,13 @@ */ class GridFieldClearAllButton implements GridField_HTMLProvider, GridField_ActionProvider { - /** - * Fragment to write the button to - * - * @var string - */ - protected $targetFragment; /** * @param string $targetFragment The HTML fragment to write the button into */ - public function __construct($targetFragment = 'after') + public function __construct(protected string $targetFragment = 'after') { - $this->targetFragment = $targetFragment; + } /** @@ -37,7 +31,7 @@ public function __construct($targetFragment = 'after') * @param GridField $gridField * @return array */ - public function getHTMLFragments($gridField) + public function getHTMLFragments($gridField): array { $button = GridField_FormAction::create($gridField, 'clear', 'Clear all', 'clear', null) ->setAttribute('data-icon', 'clear-all-logs') @@ -74,7 +68,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat /** * {@inheritDoc} */ - public function getActions($gridField) + public function getActions($gridField): array { return ['clear']; } diff --git a/src/Handler/DataObjectHandler.php b/src/Handler/DataObjectHandler.php index c0ccb91..c2d1d99 100644 --- a/src/Handler/DataObjectHandler.php +++ b/src/Handler/DataObjectHandler.php @@ -5,6 +5,7 @@ use Monolog\Logger; use Monolog\Formatter\JsonFormatter; use Monolog\Handler\AbstractProcessingHandler; +use Monolog\LogRecord; use SilverLeague\LogViewer\Model\LogEntry; use SilverStripe\Core\Config\Config; @@ -37,26 +38,26 @@ class DataObjectHandler extends AbstractProcessingHandler * * @var string */ - protected $objectClass; + protected string $objectClass; /** * @param string $objectClass The DataObject class to use for handling the write * @param int $level The minimum logging level at which this handler will be triggered (configurable) * @param boolean $bubble Whether the messages that are handled can bubble up the stack or not */ - public function __construct($objectClass = self::DEFAULT_CLASS, $level = Logger::DEBUG, $bubble = true) + public function __construct(string $objectClass = self::DEFAULT_CLASS, int $level = Logger::DEBUG, bool $bubble = true) { $this->setObjectClass($objectClass); $level = $this->getMinimumLogLevel(); parent::__construct($level, $bubble); } - protected function getDefaultFormatter() + protected function getDefaultFormatter(): JsonFormatter { return new JsonFormatter; } - protected function write(array $record) + protected function write(LogRecord $record): void { $this->addDataObject((string) $record['formatted'], $record['level_name']); } @@ -68,7 +69,7 @@ protected function write(array $record) * @param string $level The log level text, e.g. "DEBUG" * @return int The written DataObject ID */ - public function addDataObject($message, $level) + public function addDataObject(string $message, string $level): int { $class = $this->getObjectClass(); @@ -86,7 +87,7 @@ public function addDataObject($message, $level) * @param string $class * @return $this */ - public function setObjectClass($class) + public function setObjectClass(string $class): self { $this->objectClass = $class; return $this; @@ -97,7 +98,7 @@ public function setObjectClass($class) * * @return string */ - public function getObjectClass() + public function getObjectClass(): string { return $this->objectClass; } @@ -108,7 +109,7 @@ public function getObjectClass() * @see \Monolog\Logger * @return int */ - public function getMinimumLogLevel() + public function getMinimumLogLevel(): int { return (int) Config::inst()->get(LogEntry::class, 'minimum_log_level'); } diff --git a/src/Model/LogEntry.php b/src/Model/LogEntry.php index e348de7..c598589 100644 --- a/src/Model/LogEntry.php +++ b/src/Model/LogEntry.php @@ -16,14 +16,14 @@ */ class LogEntry extends DataObject implements PermissionProvider { - private static $table_name = 'LogEntry'; + private static string $table_name = 'LogEntry'; - private static $db = [ + private static array $db = [ 'Entry' => 'Text', 'Level' => 'Varchar' ]; - private static $summary_fields = [ + private static array $summary_fields = [ 'Entry', 'Created', 'Level' @@ -36,7 +36,7 @@ class LogEntry extends DataObject implements PermissionProvider * @config * @var bool */ - private static $cron_enabled = true; + private static bool $cron_enabled = true; /** * How often the cron should run (default: 4am daily) @@ -44,7 +44,7 @@ class LogEntry extends DataObject implements PermissionProvider * @config * @var string */ - private static $cron_schedule = '0 4 * * *'; + private static string $cron_schedule = '0 4 * * *'; /** * The maximum age in days for a LogEntry before it will be removed @@ -52,7 +52,7 @@ class LogEntry extends DataObject implements PermissionProvider * @config * @var int */ - private static $max_log_age = 30; + private static int $max_log_age = 30; /** * Which Monolog\Logger levels (numeric) to start handling from (see class for examples) @@ -60,12 +60,12 @@ class LogEntry extends DataObject implements PermissionProvider * @config * @var integer */ - private static $minimum_log_level = 300; + private static int $minimum_log_level = 300; /** * Permissions */ - public function providePermissions() + public function providePermissions(): array { return [ 'DELETE_ENTRY' => [ @@ -109,7 +109,7 @@ public function getCMSFields() * * {@inheritDoc} */ - public function canCreate($member = null, $context = []) + public function canCreate($member = null, $context = []): bool { return false; } @@ -119,17 +119,17 @@ public function canCreate($member = null, $context = []) * * {@inheritDoc} */ - public function canEdit($member = null) + public function canEdit($member = null): bool { return false; } - public function canDelete($member = null) + public function canDelete($member = null): bool | int { return Permission::checkMember($member, ['DELETE_ENTRY', 'CMS_ACCESS_LogViewerAdmin']); } - public function canView($member = null) + public function canView($member = null): bool | int { return Permission::checkMember($member, ['VIEW_ENTRY', 'CMS_ACCESS_LogViewerAdmin']); } diff --git a/src/Task/RemoveOldLogEntriesTask.php b/src/Task/RemoveOldLogEntriesTask.php index b883251..31e92ed 100644 --- a/src/Task/RemoveOldLogEntriesTask.php +++ b/src/Task/RemoveOldLogEntriesTask.php @@ -3,6 +3,7 @@ namespace SilverLeague\LogViewer\Task; use SilverLeague\LogViewer\Model\LogEntry; +use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\BuildTask; use SilverStripe\Core\Config\Config; use SilverStripe\CronTask\Interfaces\CronTask; @@ -18,7 +19,7 @@ class RemoveOldLogEntriesTask extends BuildTask implements CronTask /** * {@inheritDoc} */ - private static $segment = 'RemoveOldLogEntriesTask'; + private static string $segment = 'RemoveOldLogEntriesTask'; /** * {@inheritDoc} @@ -49,7 +50,7 @@ public function run($request) * * @return bool Whether anything was removed */ - public function process() + public function process(): bool { if (!$this->getCronEnabled()) { return false; @@ -60,7 +61,7 @@ public function process() /** * {@inheritDoc} */ - public function getSchedule() + public function getSchedule(): mixed { return Config::inst()->get(LogEntry::class, 'cron_schedule'); } @@ -70,7 +71,7 @@ public function getSchedule() * * @return int */ - public function getMaxAge() + public function getMaxAge(): int { return (int) Config::inst()->get(LogEntry::class, 'max_log_age'); } @@ -80,7 +81,7 @@ public function getMaxAge() * * @return bool */ - public function getCronEnabled() + public function getCronEnabled(): bool { return (bool) Config::inst()->get(LogEntry::class, 'cron_enabled'); } @@ -90,7 +91,7 @@ public function getCronEnabled() * * @return bool Whether anything was deleted or not */ - protected function removeOldLogs() + protected function removeOldLogs(): bool { $tableName = LogEntry::singleton()->baseTable(); $maxAge = $this->getMaxAge();