-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
120 lines (106 loc) · 3.85 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
@include_once __DIR__.'/vendor/autoload.php';
if (! function_exists('nitro')) {
function nitro(): \Bnomei\Nitro
{
return \Bnomei\Nitro::singleton();
}
}
Kirby::plugin('bnomei/nitro', [
'options' => [
'cache' => [
'sfc' => true,
'dir' => true,
],
'global' => true,
'atomic' => true,
'sleep' => 1_000, // MICROSECONDS with usleep, 1ms
'patch-files-class' => true,
'auto-unlock-cache' => true,
'auto-clean-cache' => true,
'max-dirty-cache' => 512, // write every N changes or on destruct
'json-encode-flags' => JSON_THROW_ON_ERROR, // | JSON_INVALID_UTF8_IGNORE,
'model' => [
'read' => true,
'write' => true,
],
],
'cacheTypes' => [
'nitro' => \Bnomei\NitroCache::class,
],
'hooks' => [
'system.loadPlugins:after' => function () {
\Bnomei\Nitro::singleton()->ready();
},
'page.*:after' => function ($event, $page) {
if ($event->action() !== 'render') {
\Bnomei\Nitro::singleton()->dir()->flush();
}
},
'file.*:after' => function ($event, $file) {
if ($event->action() !== 'render') {
\Bnomei\Nitro::singleton()->dir()->flush();
}
},
'system.exception' => function (Throwable $exception) {
// flush and unlock nitro if an exception occurs
\Bnomei\Nitro::singleton()->dir()->flush();
\Bnomei\Nitro::singleton()->cache()->flush();
\Bnomei\Nitro::singleton()->cache()->unlock();
},
],
'commands' => [
'nitro:flush' => [
'description' => 'Flush Nitro Cache',
'args' => [],
'command' => static function ($cli): void {
$cli->out('🚽 Flushing...');
nitro()->flush();
$cli->success('✅ Done.');
if (function_exists('janitor')) {
janitor()->data($cli->arg('command'), [
'status' => 200,
'message' => 'Nitro Cache flushed.',
]);
}
},
],
'nitro:unlock' => [
'description' => 'Forcibly removes the lock of a Nitro Cache',
'args' => [],
'command' => static function ($cli): void {
$cli->out('🛼 Unlocking...');
$success = nitro()->cache(['atomic' => false, 'auto-clean-cache' => false])->unlock();
$success ? $cli->success('🔓 Unlocked.') : $cli->error('❌ Failed.');
// the flush is necessary as the current instance might not have valid data anymore
$cli->out('🚽 Flushing...');
nitro()->flush();
$cli->success('✅ Done.');
if (function_exists('janitor')) {
janitor()->data($cli->arg('command'), [
'status' => 200,
'message' => $success ? 'Unlocked' : 'Failed',
]);
}
},
],
'nitro:index' => [
'description' => 'Run Nitro Index',
'args' => [],
'command' => static function ($cli): void {
$cli->out('🚽 Flushing...');
nitro()->flush();
$cli->out('🔎 Indexing...');
$count = nitro()->modelIndex();
$cli->out($count.' models indexed.');
$cli->success('✅ Done.');
if (function_exists('janitor')) {
janitor()->data($cli->arg('command'), [
'status' => 200,
'message' => $count.' models indexed.',
]);
}
},
],
],
]);