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

chore: craft 5 support #82

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions .github/workflows/code-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Code Analysis

on:
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
code_analysis:
strategy:
fail-fast: false
matrix:
actions:
- name: 'PHPStan'
run: composer phpstan
- name: 'Coding Standards'
run: composer check-cs
name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
- name: Setup PHP
id: setup-php
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: 'ctype,curl,dom,iconv,imagick,intl,json,mbstring,openssl,pcre,pdo,reflection,spl,zip'
ini-values: post_max_size=256M, max_execution_time=180, memory_limit=512M
tools: composer:v2
- name: Install Composer dependencies
run: composer install --no-interaction --no-ansi --no-progress
- run: ${{ matrix.actions.run }}
9 changes: 0 additions & 9 deletions .scrutinizer.yml

This file was deleted.

20 changes: 19 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
}
],
"require": {
"php": "^8.0.2",
"php": "^8.2.0",
"craftcms/cms": "^4.0.0",
"donatj/phpuseragentparser": "^1.7.0",
"geoip2/geoip2": "^2.5"
},
"require-dev": {
"craftcms/ecs": "dev-main",
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main"
},
"repositories": [
{
"type": "composer",
Expand All @@ -38,6 +43,19 @@
"superbig\\audit\\": "src/"
}
},
"scripts": {
"phpstan": "phpstan --ansi --memory-limit=1G",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi"
},
"config": {
"allow-plugins": {
"craftcms/plugin-installer": true,
"yiisoft/yii2-composer": true
},
"optimize-autoloader": true,
"sort-packages": true
},
"extra": {
"name": "Audit",
"handle": "audit",
Expand Down
14 changes: 14 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use craft\ecs\SetList;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function(ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__FILE__,
]);

$ecsConfig->parallel();
$ecsConfig->sets([SetList::CRAFT_CMS_4]);
};
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
includes:
- %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon
parameters:
level: 5
paths:
- src
27 changes: 13 additions & 14 deletions src/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@

namespace superbig\audit;

use craft\events\ElementEvent;
use Craft;

use craft\base\Plugin;
use craft\console\Application as ConsoleApplication;
use craft\events\ElementEvent;
use craft\events\PluginEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\events\RouteEvent;
use craft\helpers\StringHelper;
use craft\queue\jobs\ResaveElements;
use craft\queue\Queue;
use craft\services\Elements;
use craft\services\Plugins;

use craft\services\Routes;
use craft\services\UserPermissions;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use superbig\audit\models\AuditModel;
use superbig\audit\models\Settings;
use superbig\audit\services\Audit_GeoService;
use superbig\audit\services\AuditService;
use superbig\audit\models\Settings;

use Craft;
use craft\console\Application as ConsoleApplication;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\services\Elements;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;

use superbig\audit\variables\AuditVariable;
use yii\base\Event;
Expand All @@ -54,8 +53,8 @@
*/
class Audit extends Plugin
{
const PERMISSION_VIEW_LOGS = 'audit-view-logs';
const PERMISSION_CLEAR_LOGS = 'audit-clear-logs';
public const PERMISSION_VIEW_LOGS = 'audit-view-logs';
public const PERMISSION_CLEAR_LOGS = 'audit-clear-logs';

public static Audit $plugin;
public static $craft31 = false;
Expand Down Expand Up @@ -251,7 +250,7 @@
'event' => Elements::EVENT_AFTER_SAVE_ELEMENT,
'handler' => function(ElementEvent $event) {
$isNew = $event->sender->firstSave ?? $event->isNew;
$this->auditService->onSaveElement($event->element, $isNew);

Check failure on line 253 in src/Audit.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $element of method superbig\audit\services\AuditService::onSaveElement() expects craft\base\Element, craft\base\ElementInterface given.
},
],
[
Expand Down
1 change: 0 additions & 1 deletion src/assetbundles/audit/AuditAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace superbig\audit\assetbundles\audit;

use Craft;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

Expand Down
1 change: 0 additions & 1 deletion src/assetbundles/indexcpsection/IndexCPSectionAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace superbig\audit\assetbundles\indexcpsection;

use Craft;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

Expand Down
22 changes: 11 additions & 11 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@

return [
// How many days to keep log entries around
'pruneDays' => 30,
'pruneDays' => 30,

// Enable logging
'enabled' => true,
'enabled' => true,

// Toggle specific event types
'logElementEvents' => true,
'logChildElementEvents' => false,
'logDraftEvents' => false,
'logPluginEvents' => true,
'logUserEvents' => true,
'logRouteEvents' => true,
'logElementEvents' => true,
'logChildElementEvents' => false,
'logDraftEvents' => false,
'logPluginEvents' => true,
'logUserEvents' => true,
'logRouteEvents' => true,

// Prune old records when a admin is logged in
'pruneRecordsOnAdminRequests' => false,

// Enable geolocation status
'enabledGeolocation' => true,
'maxmindLicenseKey' => '',
'enabledGeolocation' => true,
'maxmindLicenseKey' => '',

// Where to save Maxmind DB files
'dbPath' => '',
'dbPath' => '',
];
1 change: 0 additions & 1 deletion src/console/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
class DefaultController extends Controller
{

// Protected Properties
// =========================================================================

Expand Down Expand Up @@ -59,7 +58,7 @@
ConsoleHelper::startProgress(4, 6);

if (isset($response['error'])) {
Console::error($response['error']);

Check failure on line 61 in src/console/controllers/DefaultController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method error() on an unknown class superbig\audit\console\controllers\Console.
ConsoleHelper::endProgress();

return ExitCode::DATAERR;
Expand Down
19 changes: 9 additions & 10 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

namespace superbig\audit\controllers;

use Craft;
use craft\helpers\Template;
use craft\helpers\UrlHelper;
use superbig\audit\Audit;

use Craft;
use craft\web\Controller;
use superbig\audit\Audit;
use superbig\audit\models\AuditModel;
use superbig\audit\records\AuditRecord;

Expand All @@ -26,7 +26,6 @@
*/
class DefaultController extends Controller
{

// Protected Properties
// =========================================================================

Expand All @@ -35,7 +34,7 @@
* The actions must be in 'kebab-case'
* @access protected
*/
protected array|int|bool $allowAnonymous = [];

Check failure on line 37 in src/controllers/DefaultController.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc type array|bool|int of property superbig\audit\controllers\DefaultController::$allowAnonymous is not covariant with PHPDoc type array<int|string>|bool|int of overridden property craft\web\Controller::$allowAnonymous.

// Public Methods
// =========================================================================
Expand All @@ -48,12 +47,12 @@
$this->requirePermission(Audit::PERMISSION_VIEW_LOGS);

$itemsPerPage = 100;
$query = AuditRecord::find()
$query = AuditRecord::find()
->orderBy('dateCreated desc')
->with('user')
->limit($itemsPerPage)
->where(['parentId' => null]);
$models = [];
$models = [];
$paginate = Template::paginateCriteria($query);
list($pageInfo, $records) = $paginate;

Expand All @@ -64,7 +63,7 @@
}

return $this->renderTemplate('audit/index', [
'logs' => $models,
'logs' => $models,
'pageInfo' => $pageInfo,
]);
}
Expand All @@ -80,13 +79,13 @@
{
$this->requirePermission(Audit::PERMISSION_VIEW_LOGS);

$service = Audit::$plugin->auditService;
$log = $service->getEventById($id);
$service = Audit::$plugin->auditService;
$log = $service->getEventById($id);
$logsInSession = $service->getEventsBySessionId($log->sessionId);

Check failure on line 84 in src/controllers/DefaultController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $id of method superbig\audit\services\AuditService::getEventsBySessionId() expects null, string given.

return $this->renderTemplate('audit/_view', [
'settings' => Audit::$plugin->getSettings(),
'log' => $log,
'settings' => Audit::$plugin->getSettings(),
'log' => $log,
'logsInSession' => $logsInSession,
]);
}
Expand Down
15 changes: 3 additions & 12 deletions src/controllers/GeoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@

namespace superbig\audit\controllers;

use craft\helpers\Template;
use craft\web\UrlManager;
use superbig\audit\Audit;

use Craft;

use craft\web\Controller;
use superbig\audit\models\AuditModel;
use superbig\audit\records\AuditRecord;
use yii\data\Pagination;
use superbig\audit\Audit;
use yii\web\HttpException;
use yii\widgets\LinkPager;

use JasonGrimes\Paginator;

/**
* @author Superbig
Expand All @@ -31,7 +23,6 @@
*/
class GeoController extends Controller
{

protected array|int|bool $allowAnonymous = ['update-database'];

// Protected Properties
Expand Down Expand Up @@ -69,25 +60,25 @@
public function actionUpdateDatabase()
{
$validKey = Audit::$plugin->getSettings()->updateAuthKey;
$key = Craft::$app->getRequest()->getParam('key');
$key = Craft::$app->getRequest()->getParam('key');

if (!Craft::$app->getUser()->getIsAdmin() && $key !== $validKey) {
throw new HttpException('Not authorized to run this action');

Check failure on line 66 in src/controllers/GeoController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $status of class yii\web\HttpException constructor expects int, string given.
}

$response = Audit::$plugin->geo->downloadDatabase();

if (isset($response['error'])) {
return $this->renderJSON($response['error']);

Check failure on line 72 in src/controllers/GeoController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method superbig\audit\controllers\GeoController::actionUpdateDatabase() with return type void returns mixed but should not return anything.
}

$response = Audit::$plugin->geo->unpackDatabase();

if (isset($response['error'])) {
return $this->renderJSON($response['error']);

Check failure on line 78 in src/controllers/GeoController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method superbig\audit\controllers\GeoController::actionUpdateDatabase() with return type void returns mixed but should not return anything.
}

return $this->renderJSON($response);

Check failure on line 81 in src/controllers/GeoController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method superbig\audit\controllers\GeoController::actionUpdateDatabase() with return type void returns mixed but should not return anything.
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/events/SnapshotEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ class SnapshotEvent extends Event
* @var array Snapshot
*/
public $snapshot;

}
23 changes: 3 additions & 20 deletions src/helpers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,9 @@

namespace superbig\audit\helpers;

use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\Plugin;
use craft\base\PluginInterface;
use craft\fields\Assets;
use craft\fields\Entries;
use craft\fields\Lightswitch;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\models\EntryDraft;
use superbig\audit\Audit;

use Craft;
use craft\base\Component;
use superbig\audit\models\AuditModel;
use superbig\audit\records\AuditRecord;
use yii\base\Exception;

/**
* @author Superbig
* @package Audit
Expand All @@ -48,16 +32,15 @@ public static function getUriDisplayHtml($uriParts = [])
foreach ($uriParts as $part) {
if (is_string($part)) {
$uriDisplayHtml .= Html::encode($part);
}
else {
} else {
$uriDisplayHtml .= Html::encodeParams('<span class="token" data-name="{name}" data-value="{value}"><span>{name}</span></span>',
[
'name' => $part[0],
'name' => $part[0],
'value' => $part[1],
]);
}
}

return $uriDisplayHtml;
}
}
}
Loading