Skip to content

Commit

Permalink
♻️ codeclimate, added a few tests and fix a few bugs
Browse files Browse the repository at this point in the history
Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Jul 21, 2024
1 parent befc158 commit f648a6a
Show file tree
Hide file tree
Showing 19 changed files with 565 additions and 437 deletions.
4 changes: 0 additions & 4 deletions .coveralls.yml

This file was deleted.

21 changes: 15 additions & 6 deletions .github/workflows/pest-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Pest Tests

on: ['push', 'pull_request']
on: [ 'push', 'pull_request' ]

jobs:
test:
Expand All @@ -17,11 +17,20 @@ jobs:
tools: composer:v2
coverage: xdebug

- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}

- name: Install
run: composer install --no-interaction --prefer-dist --optimize-autoloader

- name: Run Tests
run: |
php tests/patch.php
./vendor/bin/pest --group=SetupPagesInSeparatePHPUnitRun
./vendor/bin/pest
- name: Test & publish code coverage
uses: paambaati/[email protected]
with:
coverageCommand: composer coverage
coverageLocations: ${{github.workspace}}/tests/clover.xml:clover
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}

4 changes: 3 additions & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ jobs:
composer install --no-interaction --prefer-dist --optimize-autoloader
- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
run: |
php tests/patch.php
./vendor/bin/phpstan --error-format=github
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Kirby 3 Janitor
# Kirby Janitor

![Release](https://flat.badgen.net/packagist/v/bnomei/kirby3-janitor?color=ae81ff)
![Downloads](https://flat.badgen.net/packagist/dt/bnomei/kirby3-janitor?color=272822)
[![Build Status](https://flat.badgen.net/travis/bnomei/kirby3-janitor)](https://travis-ci.com/bnomei/kirby3-janitor)
[![Coverage Status](https://flat.badgen.net/coveralls/c/github/bnomei/kirby3-janitor)](https://coveralls.io/github/bnomei/kirby3-janitor)
[![Coverage Status](https://flat.badgen.net/codeclimate/coverage/github/bnomei/kirby3-janitor)](https://coveralls.io/github/bnomei/kirby3-janitor)
[![Maintainability](https://flat.badgen.net/codeclimate/maintainability/bnomei/kirby3-janitor)](https://codeclimate.com/github/bnomei/kirby3-janitor)
[![Twitter](https://flat.badgen.net/badge/twitter/bnomei?color=66d9ef)](https://twitter.com/bnomei)
[![Discord](https://flat.badgen.net/badge/discord/bnomei?color=7289da)](https://discordapp.com/users/bnomei)

Kirby 3 Plugin for running commands.
Kirby Plugin for running commands.

- It is a Panel Button!
- It has commands built-in for cleaning the cache, sessions, create zip-backup, pre-generate thumbs, open URLs, refresh the current Panel page and more.
Expand All @@ -16,13 +15,14 @@ Kirby 3 Plugin for running commands.

## Install

Using composer:
You have to use composer to install both the plugin and the CLI locally into your project:

```bash
composer require getkirby/cli bnomei/kirby3-janitor
```

You need to install the CLI with composer. Since Janitor depends on the CLI to be available installing only the janitor plugin via submodules or via ZIP do not make much sense. Please stick to composer for now.
> [!WARNING]
> You need to install the CLI with composer into your project and not use the global version. Since Janitor depends on the CLI to be available, installing only the janitor plugin via submodules or via ZIP is NOT supported.
## Commercial Usage

Expand Down Expand Up @@ -125,6 +125,7 @@ fields:

The `$model` will match the model of whatever page, file, user or site object you pressed the button at.

> [!NOTE]
> Why just a single model variable instead of one each for page, file, user and site? For one reason to make it work directly with any existing version 2 callbacks you might have already created and secondly because this why it is very easy to get the model that triggered the callback.
### Built in commands and examples
Expand Down
31 changes: 20 additions & 11 deletions classes/Janitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function singleton(array $options = []): Janitor
return self::$singleton;
}

public static function isTrue($val, bool $return_null = false): bool
public static function isTrue(mixed $val, bool $return_null = false): bool
{
$boolval = (is_string($val) ? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : (bool) $val);

Expand Down Expand Up @@ -150,18 +150,23 @@ public static function parseCommand(string $command): array
return [$name, $args];
}

public static function query(?string $template = null, mixed $model = null): string
public static function query(mixed $template = null, mixed $model = null): string
{
// array|Closure|string|null could be passed from I18n::translate
if (! is_string($template)) {
return '';
}

$page = null;
$file = null;
$site = kirby()->site();
$user = kirby()->user();
if ($model instanceof Page) {
$page = $model;
} elseif ($model instanceof File) {
$site = $model;
} elseif ($model instanceof Site) {
$file = $model;
} elseif ($model instanceof Site || $model === $site) {
$site = $model;
} elseif ($model instanceof User) {
$user = $model;
}
Expand All @@ -176,9 +181,9 @@ public static function query(?string $template = null, mixed $model = null): str
]);
}

public static function requestBlockedByMaintenance(): bool
public static function requestBlockedByMaintenance(?string $request = null): bool
{
$request = kirby()->request()->url()->toString();
$request ??= kirby()->request()->url()->toString();
foreach ([
kirby()->urls()->panel(),
kirby()->urls()->api(),
Expand All @@ -191,7 +196,7 @@ public static function requestBlockedByMaintenance(): bool

$isBlocked = option('bnomei.janitor.maintenance.check', true);
if ($isBlocked && ! is_string($isBlocked) && is_callable($isBlocked)) {
$isBlocked = $isBlocked();
$isBlocked = $isBlocked(); // @codeCoverageIgnore
}

return (bool) $isBlocked;
Expand Down Expand Up @@ -220,12 +225,16 @@ public static function resolveModel(string $uuid): mixed

public static function resolveQueriesInCommand(array $args): array
{
$model = null;
$modelKey = array_search('--model', $args);
$model = $modelKey !== false && $modelKey + 1 < count($args) ? $args[$modelKey + 1] : null;
if (! $model) {
return $args;

if ($modelKey !== false && is_int($modelKey)) {
$model = $modelKey + 1 < count($args) ? $args[$modelKey + 1] : null;
if (! $model) {
return $args; // @codeCoverageIgnore
}
$model = Janitor::resolveModel($model);
}
$model = Janitor::resolveModel($model);

$args = array_map(function ($value) use ($model) {
// allows for html even without {< since it is not a blueprint query
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-janitor",
"type": "kirby-plugin",
"version": "4.1.0",
"version": "4.2.0",
"license": "MIT",
"homepage": "https://github.com/bnomei/kirby3-janitor",
"description": "Kirby Plugin for running commands like cleaning the cache from within the Panel, PHP code or a cronjob",
Expand Down Expand Up @@ -44,14 +44,14 @@
}
},
"require": {
"php": ">=8.1",
"php": ">=8.2",
"getkirby/composer-installer": "^1.2",
"symfony/deprecation-contracts": "^3.0.1",
"symfony/finder": "^7.0"
},
"require-dev": {
"getkirby/cli": "dev-develop",
"getkirby/cms": "^4.0.0-beta.3",
"getkirby/cms": "^4.3.0",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.13",
"pestphp/pest": "^2.24",
Expand All @@ -61,10 +61,10 @@
"scripts": {
"analyze": "./vendor/bin/phpstan",
"fix": "./vendor/bin/pint",
"test": [
"mkdir -p tests/logs",
"@putenv XDEBUG_MODE=coverage",
"phpunit --configuration ./phpunit.xml"
"test": "./vendor/bin/pest",
"coverage": [
"php tests/patch.php",
"./vendor/bin/pest --coverage"
],
"dist": [
"npm run format",
Expand Down
Loading

0 comments on commit f648a6a

Please sign in to comment.