Skip to content

Commit 97b0512

Browse files
committed
fixes #8
Signed-off-by: Bruno Meilick <[email protected]>
1 parent e29abd3 commit 97b0512

File tree

6 files changed

+82
-10
lines changed

6 files changed

+82
-10
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ $json = janitor('clean', true); // array
4848
label: Enter Bank
4949
progress: Performing Heist...
5050
job: heist
51+
data: Grand # (string) forwarded to job context
5152
```
5253
5354
**Kirby API (post Authentification)**
@@ -87,15 +88,23 @@ Go build your own jobs. Trigger APIs, create ZIPs, rename Files, ...
8788
<?php
8889
return [
8990
'bnomei.janitor.jobs' => [
90-
'heist' => function() {
91+
'heist' => function(Kirby\Cms\Page $page = null, string $data = null) {
9192
\Bnomei\Janitor::log('heist.mask '.time());
93+
9294
$grand = \Bnomei\Janitor::lootTheSafe();
9395
// or trigger a snippet like this:
9496
// snippet('call-police');
97+
98+
// $page is Kirby Page Object if job issued by Panel
99+
$location = $page ? $page->title() : 'Bank';
100+
101+
// $data is optional [data] prop from the Janitor Panel Field
102+
$currency = $data ? $data : 'Coins';
103+
95104
\Bnomei\Janitor::log('heist.exit '.time());
96105
return [
97106
'status' => $grand > 0 ? 200 : 404,
98-
'label' => $grand . ' Grand looted!'
107+
'label' => $grand . ' ' . $currency . ' looted at ' . $location . '!'
99108
];
100109
}
101110
],
@@ -110,6 +119,27 @@ Go build your own jobs. Trigger APIs, create ZIPs, rename Files, ...
110119
- **flush** calls `flush()` on *all* cache-folders. Dangerous!
111120
- **repair** creates the root cache folder if it is missing.
112121

122+
123+
## Panel context page and data
124+
125+
Since 1.3.0 you can access the Page-Object the Panel-Field was called at and the forwarded custom data prop.
126+
127+
128+
```yaml
129+
myjob:
130+
type: janitor
131+
label: Perform Job
132+
progress: Performing Job...
133+
job: myjob
134+
data: my custom data
135+
```
136+
137+
```php
138+
'myjob' => function(Kirby\Cms\Page $page = null, string $data = null) {
139+
// $data == 'my custom data'
140+
}
141+
```
142+
113143
## Settings
114144

115145
All settings must be prefixed with `bnomei.janitor.`.

classes/Janitor.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Bnomei;
44

5+
use Kirby\Exception\Exception;
56
use \Kirby\Toolkit;
67

78
class Janitor
@@ -122,24 +123,34 @@ public static function cacheFolders(bool $all = false): array
122123
return \array_keys($folders);
123124
}
124125

125-
public static function api(string $job, bool $remote = false, string $secret = null): array
126+
public static function api(string $job, bool $remote = false, string $secret = null, string $context = null, string $contextData = null): array
126127
{
127128
if (!$remote || ($secret && $secret == option('bnomei.janitor.secret'))) {
128129
$defaults = option('bnomei.janitor.jobs.defaults', []);
129130
$jobs = \array_merge($defaults, option('bnomei.janitor.jobs', []));
130131
foreach (option('bnomei.janitor.jobs.extends', []) as $optionID) {
131132
$jobs = \array_merge($jobs, option($optionID, []));
132133
}
133-
134+
134135
$before = \time();
135136
$data = [];
136137
$success = false;
137138

138139
if (\array_key_exists($job, $jobs)) {
139140
$c = $jobs[$job];
140141
if (\is_callable($c)) {
142+
$r = null;
141143
static::log('@' . $job . ' started');
142-
$r = $c();
144+
try {
145+
if($context) {
146+
$r = $c(kirby()->page(urldecode($context)), urldecode($contextData));
147+
}
148+
if( ! $r) {
149+
$r = $c();
150+
}
151+
} catch (Exception $ex) {
152+
$success = false;
153+
}
143154
if (\is_array($r)) {
144155
$data = $r;
145156
$v = \Kirby\Toolkit\A::get($data, 'status', 404);

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": "1.2.1",
4+
"version": "1.3.0",
55
"license": "MIT",
66
"description": "Kirby 3 Plugin for running jobs like cleaning the cache from within the Panel, PHP code or a cronjob",
77
"authors": [

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
},
5757
'cooldown' => function (int $cooldownMilliseconds = 2000) {
5858
return intval(option('bnomei.janitor.label.cooldown', $cooldownMilliseconds));
59+
},
60+
'data' => function (string $data = null) {
61+
return \Kirby\Toolkit\I18n::translate($data, $data);
62+
},
63+
'pageURI' => function () {
64+
return $this->model()->uri();
5965
}
6066
]
6167
]
@@ -72,11 +78,27 @@
7278
],
7379
'api' => [
7480
'routes' => [
81+
[
82+
'pattern' => 'plugin-janitor/(:any)/(:any)/(:any)',
83+
'action' => function (string $job, string $context, string $data) {
84+
\Bnomei\Janitor::log('janitor-api-auth', 'debug');
85+
$api = \Bnomei\Janitor::api($job, false, null, $context, $data);
86+
return $api;
87+
}
88+
],
89+
[
90+
'pattern' => 'plugin-janitor/(:any)/(:any)',
91+
'action' => function (string $job, string $context) {
92+
\Bnomei\Janitor::log('janitor-api-auth', 'debug');
93+
$api = \Bnomei\Janitor::api($job, false, null, $context);
94+
return $api;
95+
}
96+
],
7597
[
7698
'pattern' => 'plugin-janitor/(:any)',
7799
'action' => function (string $job) {
78100
\Bnomei\Janitor::log('janitor-api-auth', 'debug');
79-
$api = \Bnomei\Janitor::api($job);
101+
$api = \Bnomei\Janitor::api($job, false, null);
80102
return $api;
81103
}
82104
]

src/components/fields/Janitor.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ export default {
1010
progress: String,
1111
job: String,
1212
cooldown: Number,
13-
status: String
13+
status: String,
14+
data: String,
15+
pageURI: String,
1416
},
1517
methods: {
1618
janitor() {
17-
this.getRequest(this.job)
19+
let url = this.job
20+
if(true) {
21+
url = url + '/' + encodeURIComponent(this.pageURI)
22+
}
23+
if(this.data != undefined) {
24+
url = url + '/' + encodeURIComponent(this.data)
25+
}
26+
this.getRequest(url)
1827
},
1928
getRequest (url) {
2029
let that = this

0 commit comments

Comments
 (0)