Skip to content

Commit 768a42c

Browse files
authored
Merge pull request #110 from bnomei/call-job-on-model
call job on model
2 parents 2771fae + 013e5be commit 768a42c

File tree

15 files changed

+120
-7
lines changed

15 files changed

+120
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ node_modules
3434
/vendor/**/php4/*
3535
/vendor/getkirby/composer-installer
3636

37+
/tests/site/backups/

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ The `$model` will match the model of whatever page, file, user or site object yo
132132
This plugin comes with a [few commands](https://github.com/bnomei/kirby3-janitor/tree/master/commands) you might like to use yourself and some [example commands](https://github.com/bnomei/kirby3-janitor/tree/master/tests/site/commands) used to showcase the various options the button has (like how to change the icon or open a URL in a new tab). Some commands can be used in both panel and terminal. Others are limited in their use to either one of them. In the terminal you can use `--help` argument to view the help for each command.
133133

134134
- `janitor:backupzip`, creates a backup zip
135+
- `janitor:call`, calls a method on the current model with optional data parameter
135136
- `janitor:cleancontent`, removes fields from content file that are not defined in your blueprints
136137
- `janitor:clipboard`, copies a defined value to your clipboard
137138
- `janitor:download`, triggers a download of an URL
@@ -234,11 +235,16 @@ janitor_thumbssite:
234235
type: janitor
235236
command: 'janitor:thumbs --site'
236237
label: Generate thumbs from existing thumb jobs (full site)
238+
239+
janitor_callWithData:
240+
label: Call method on model with Data
241+
type: janitor
242+
command: 'janitor:call --method repeatAfterMe --data {{ user.id }}'
237243
```
238244

239245
If you want you can also call any of [the core shipping with the CLI](https://github.com/getkirby/cli#available-core-commands) like `clear:cache`.
240246

241-
Keep in mind that the Janitor panel button will append the `--quiet` option on all commands automatically to silence outputs to the non-existing CLI.
247+
Keep in mind that the Janitor panel button and webhooks will append the `--quiet` option on all commands automatically to silence outputs to the non-existing CLI. But if you use `janitor()->command()` you will have to append `--quiet` to your command yourself.
242248

243249
### Smartly delaying resolution of a command argument
244250

commands/call.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
if (!class_exists('Bnomei\Janitor')) {
6+
require_once __DIR__ . '/../classes/Janitor.php';
7+
}
8+
9+
use Bnomei\Janitor;
10+
use Kirby\CLI\CLI;
11+
12+
return [
13+
'description' => 'Call a job-like method on a model',
14+
'args' => [
15+
'method' => [
16+
'longPrefix' => 'method',
17+
'description' => 'Name of the method on the model',
18+
'castTo' => 'string',
19+
'required' => true,
20+
],
21+
] + Janitor::ARGS, // page, file, user, site, data, model
22+
'command' => static function (CLI $cli): void {
23+
$method = $cli->arg('method');
24+
$result = [
25+
'status' => 500,
26+
];
27+
28+
$model = null;
29+
if ($cli->arg('site')) {
30+
$model = $cli->kirby()->site();
31+
} elseif (!empty($cli->arg('page'))) {
32+
$model = $cli->kirby()->page($cli->arg('page'));
33+
} elseif (!empty($cli->arg('file'))) {
34+
$model = $cli->kirby()->file($cli->arg('file'));
35+
} elseif (!empty($cli->arg('user'))) {
36+
$model = $cli->kirby()->user($cli->arg('user'));
37+
}
38+
39+
if ($model) {
40+
if (method_exists($model, $method)) {
41+
if (empty($cli->arg('data'))) {
42+
$result = $model->$method();
43+
} else {
44+
$result = $model->$method($cli->arg('data'));
45+
}
46+
} else {
47+
$result['message'] = t('janitor.method-not-found', 'Method "' . $method . '" could not be called on model of class <' . $model::class . '>.');
48+
}
49+
} else {
50+
$result['message'] = t('janitor.model-not-found', 'No model provided');
51+
$cli->error('No model provided. Use `--page`, `--file`, `--user` or `--site`.');
52+
}
53+
54+
(A::get($result, 'status') === 200 ? $cli->success($model::class . '->' . $method) : $cli->error($model::class . '->' . $method));
55+
$cli->out(print_r($result, true));
56+
57+
janitor()->data($cli->arg('command'), $result);
58+
}
59+
];

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby3-janitor",
33
"type": "kirby-plugin",
4-
"version": "3.6.0",
4+
"version": "3.7.0",
55
"license": "MIT",
66
"homepage": "https://github.com/bnomei/kirby3-janitor",
77
"description": "Kirby 3 Plugin for running commands like cleaning the cache from within the Panel, PHP code or a cronjob",

index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
],
2424
'commands' => [ // https://github.com/getkirby/cli
2525
'janitor:backupzip' => require __DIR__ . '/commands/backupzip.php',
26+
'janitor:call' => require __DIR__ . '/commands/call.php',
2627
'janitor:cleancontent' => require __DIR__ . '/commands/cleancontent.php',
2728
'janitor:clipboard' => require __DIR__ . '/commands/clipboard.php',
2829
'janitor:download' => require __DIR__ . '/commands/download.php',

tests/JanitorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ public function testJob()
5454
$janitor->command('janitor:job --key some.key.to.task --site --data "some data"')['message']
5555
);
5656
}
57+
58+
public function testMethod()
59+
{
60+
$janitor = new Janitor();
61+
$this->assertEquals(
62+
200,
63+
$janitor->command('janitor:call --method whoAmI --page page://vf0xqIlpU0ZlSorI')['status']
64+
);
65+
66+
$this->assertEquals(
67+
'Repeat after me: hello',
68+
$janitor->command('janitor:call --method repeatAfterMe --data hello --page page://vf0xqIlpU0ZlSorI')['message']
69+
);
70+
}
5771
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)