Skip to content

Commit

Permalink
CMS 5 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
DevKCode committed Mar 2, 2023
1 parent 1dce243 commit 9fc9794
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 50 deletions.
20 changes: 13 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -37,5 +37,11 @@
]
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true
}
}
}
17 changes: 10 additions & 7 deletions src/Admin/LogViewerAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
];

Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -73,7 +76,7 @@ public function getGridFieldName()
*
* {@inheritDoc}
*/
public function getList()
public function getList(): DataList
{
$list = parent::getList();
return $list->sort(['Created' => 'DESC']);
Expand Down
14 changes: 4 additions & 10 deletions src/Forms/GridField/GridFieldClearAllButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

/**
Expand All @@ -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')
Expand Down Expand Up @@ -74,7 +68,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
/**
* {@inheritDoc}
*/
public function getActions($gridField)
public function getActions($gridField): array
{
return ['clear'];
}
Expand Down
17 changes: 9 additions & 8 deletions src/Handler/DataObjectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']);
}
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -97,7 +98,7 @@ public function setObjectClass($class)
*
* @return string
*/
public function getObjectClass()
public function getObjectClass(): string
{
return $this->objectClass;
}
Expand All @@ -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');
}
Expand Down
24 changes: 12 additions & 12 deletions src/Model/LogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -36,36 +36,36 @@ 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)
*
* @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
*
* @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)
*
* @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' => [
Expand Down Expand Up @@ -109,7 +109,7 @@ public function getCMSFields()
*
* {@inheritDoc}
*/
public function canCreate($member = null, $context = [])
public function canCreate($member = null, $context = []): bool
{
return false;
}
Expand All @@ -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']);
}
Expand Down
13 changes: 7 additions & 6 deletions src/Task/RemoveOldLogEntriesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +19,7 @@ class RemoveOldLogEntriesTask extends BuildTask implements CronTask
/**
* {@inheritDoc}
*/
private static $segment = 'RemoveOldLogEntriesTask';
private static string $segment = 'RemoveOldLogEntriesTask';

/**
* {@inheritDoc}
Expand Down Expand Up @@ -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;
Expand All @@ -60,7 +61,7 @@ public function process()
/**
* {@inheritDoc}
*/
public function getSchedule()
public function getSchedule(): mixed
{
return Config::inst()->get(LogEntry::class, 'cron_schedule');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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();
Expand Down

0 comments on commit 9fc9794

Please sign in to comment.