Skip to content

Commit 6c2f14b

Browse files
committed
fixed class loaded in some cases
Signed-off-by: Bruno Meilick <[email protected]>
1 parent 3c57951 commit 6c2f14b

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

classes/janitor.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Bnomei;
44

5-
use Kirby\Toolkit\Dir;
6-
use Kirby\Toolkit\F;
7-
use Kirby\Toolkit\Str;
5+
use \Kirby\Toolkit;
86

97
class Janitor
108
{
@@ -13,13 +11,13 @@ public static function cacheRemoveUnusedFiles() {
1311
$exclude = option('bnomei.janitor.exclude');
1412
$c = 0;
1513
foreach (static::cacheFolders() as $folder) {
16-
$cache = kirby()->cache(str_replace(DIRECTORY_SEPARATOR, '.', $folder));
17-
foreach (Dir::files($krc.DIRECTORY_SEPARATOR.$folder) as $file) {
14+
$cache = kirby()->cache(\str_replace(DIRECTORY_SEPARATOR, '.', $folder));
15+
foreach (\Kirby\Toolkit\Dir::files($krc.DIRECTORY_SEPARATOR.$folder) as $file) {
1816
if(!$cache->get($file)) { // expired
1917
$fpath = $krc.DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$file;
2018
$success = true;
21-
if (!option('bnomei.janitor.simulate') && F::exists($fpath)) {
22-
$success = F::remove($fpath);
19+
if (!option('bnomei.janitor.simulate') && \Kirby\Toolkit\F::exists($fpath)) {
20+
$success = \Kirby\Toolkit\F::remove($fpath);
2321
}
2422
if($success) {
2523
$c++;
@@ -37,12 +35,12 @@ public static function cacheClearFolders() {
3735
$c = 0;
3836
foreach (static::cacheFolders() as $folder) {
3937
$f = $krc.DIRECTORY_SEPARATOR.$folder;
40-
if (!is_dir($f)) {
38+
if (!\is_dir($f)) {
4139
continue;
4240
}
4341
$success = true;
4442
if (!option('bnomei.janitor.simulate')) {
45-
$success = Dir::remove($f);
43+
$success = \Kirby\Toolkit\Dir::remove($f);
4644
}
4745
if ($success) {
4846
$c++;
@@ -59,7 +57,7 @@ public static function cacheFlush() {
5957
$exclude = option('bnomei.janitor.exclude');
6058
$c = 0;
6159
foreach (static::cacheFolders() as $folder) {
62-
$cacheName = str_replace(DIRECTORY_SEPARATOR, '.', $folder);
60+
$cacheName = \str_replace(DIRECTORY_SEPARATOR, '.', $folder);
6361
if ($cache = kirby()->cache($cacheName)) {
6462
if (!option('bnomei.janitor.simulate')) {
6563
$cache->flush();
@@ -73,8 +71,8 @@ public static function cacheFlush() {
7371

7472
public static function cacheRepair() {
7573
$krc = kirby()->roots()->cache();
76-
if (!is_dir($krc)) {
77-
if(Dir::make($krc)) {
74+
if (!\is_dir($krc)) {
75+
if(\Kirby\Toolkit\Dir::make($krc)) {
7876
static::log('#janitor recreated the root cache folder', 'warning');
7977
} else {
8078
static::log('#janitor failed to create the root cache folder', 'error');
@@ -88,7 +86,7 @@ public static function cacheRepair() {
8886
public static function lootTheSafe(): int {
8987
// NOTE: used to test the api $_$
9088
$loot = random_int(1, 9);
91-
sleep(1);
89+
\sleep(1);
9290
return $loot;
9391
}
9492

@@ -97,13 +95,13 @@ public static function cacheFolders(bool $all = false): array {
9795
$exclude = option('bnomei.janitor.exclude');
9896

9997
$folders = [];
100-
foreach(Dir::index($krc, true) as $file) {
98+
foreach(\Kirby\Toolkit\Dir::index($krc, true) as $file) {
10199
$skip = false;
102100
$path = $krc . DIRECTORY_SEPARATOR . $file;
103-
if (is_dir($path) && count(Dir::dirs($path)) == 0) {
101+
if (\is_dir($path) && count(\Kirby\Toolkit\Dir::dirs($path)) == 0) {
104102
if (!$all) {
105103
foreach ($exclude as $e) {
106-
if (Str::contains($file, trim($e, DIRECTORY_SEPARATOR))) {
104+
if (\Kirby\Toolkit\Str::contains($file, \trim($e, DIRECTORY_SEPARATOR))) {
107105
$skip = true;
108106
break;
109107
}
@@ -114,33 +112,34 @@ public static function cacheFolders(bool $all = false): array {
114112
}
115113
}
116114
}
117-
return array_keys($folders);
115+
return \array_keys($folders);
118116
}
119117

120118
public static function api(string $job, bool $remote = false, string $secret = null): array {
121119
if(!$remote || ($secret && $secret == option('bnomei.janitor.secret'))) {
122-
$job = Str::slug($job);
120+
$job = \Kirby\Toolkit\Str::slug($job);
123121

124122
$defaults = option('bnomei.janitor.jobs.defaults', []);
125-
$jobs = array_merge($defaults, option('bnomei.janitor.jobs', []));
123+
$jobs = \array_merge($defaults, option('bnomei.janitor.jobs', []));
126124
foreach(option('bnomei.janitor.jobs.extends', []) as $optionID) {
127-
$jobs = array_merge($jobs, option($optionID, []));
125+
$jobs = \array_merge($jobs, option($optionID, []));
128126
}
129127

130-
$before = time();
128+
$before = \time();
131129
$data = [];
132130
$success = false;
133131

134-
if(array_key_exists($job, $jobs)) {
132+
if(\array_key_exists($job, $jobs)) {
135133
$c = $jobs[$job];
136-
if (is_callable($c)) {
134+
if (\is_callable($c)) {
137135
static::log('@' . $job . ' started');
138136
$r = $c();
139-
if(is_array($r)) {
137+
if(\is_array($r)) {
140138
$data = $r;
141-
$success = boolval(\Kirby\Toolkit\A::get($data, 'status', 404) == 200);
139+
$v = \Kirby\Toolkit\A::get($data, 'status', 404);
140+
$success = \intval($v) == 200;
142141
} else {
143-
$success = boolval($r);
142+
$success = \boolval($r);
144143
}
145144
static::log('@' . $job . ' finished');
146145
} else {
@@ -150,8 +149,8 @@ public static function api(string $job, bool $remote = false, string $secret = n
150149
static::log('@' . $job . ' not found', 'warning');
151150
}
152151

153-
$after = time();
154-
return array_merge([
152+
$after = \time();
153+
return \array_merge([
155154
'job' => $job,
156155
'started' => $before,
157156
'finished' => $after,
@@ -165,7 +164,7 @@ public static function api(string $job, bool $remote = false, string $secret = n
165164
public static function log(string $msg = '', string $level = 'info', array $context = []):bool
166165
{
167166
$log = option('bnomei.janitor.log');
168-
if ($log && is_callable($log)) {
167+
if ($log && \is_callable($log)) {
169168
if (!option('debug') && $level == 'debug') {
170169
// skip but...
171170
return true;

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby3-janitor",
33
"type": "plugin",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
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
"autoload": {
@@ -29,7 +29,7 @@
2929
},
3030
"scripts": {
3131
"build": "composer update; composer dumpautoload -o; yarn run build;",
32-
"zip": "rm kirby3-janitor.zip; composer update; composer remove getkirby/cms; composer dumpautoload -o; yarn run build; zip -r kirby3-janitor.zip . -x *.git* -x node_modules/\\*; composer require getkirby/cms:'dev-master as 3.0.0'; composer dumpautoload -o;"
32+
"zip": "rm kirby3-janitor.zip; composer update; composer remove getkirby/cms; composer dumpautoload -o; yarn run build; zip -r kirby3-janitor.zip . -x *.git* -x *.gif* -x node_modules/\\*; composer require getkirby/cms:'dev-master as 3.0.0'; composer dumpautoload -o;"
3333
},
3434
"minimum-stability": "beta"
3535
}

config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
'action' => function(string $job, string $secret) {
6565
\Bnomei\Janitor::log('janitor-api-secret', 'debug');
6666
$api = \Bnomei\Janitor::api($job, true, $secret);
67-
return \Kirby\Http\Response::json($api, $api['status']);
67+
return Kirby\Http\Response::json($api, $api['status']);
6868
}
6969
]
7070
],
@@ -83,6 +83,10 @@
8383

8484
]);
8585

86+
if (!class_exists('Bnomei\Janitor')) {
87+
require_once __DIR__ . '/classes/janitor.php';
88+
}
89+
8690
if(!function_exists('janitor')) {
8791
function janitor(string $job, bool $dump = false) {
8892
\Bnomei\Janitor::log('janitor()', 'debug');

0 commit comments

Comments
 (0)