-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
140 lines (122 loc) · 4.17 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
use Bnomei\Khulan;
use Kirby\CLI\CLI;
use Kirby\Cms\File;
use Kirby\Cms\Files;
use Kirby\Cms\Pages;
use Kirby\Cms\User;
use Kirby\Filesystem\F;
use Kirby\Toolkit\Str;
use MongoDB\Collection;
@include_once __DIR__.'/vendor/autoload.php';
load([
'bnomei\\mongodb' => 'classes/Mongodb.php',
'bnomei\\khulan' => 'classes/Khulan.php',
'bnomei\\modelwithkhulan' => 'classes/ModelWithKhulan.php',
], __DIR__);
if (! function_exists('mongo')) {
function mongo(?string $collection = null): \Bnomei\Mongodb|Collection
{
if (! $collection) {
return \Bnomei\Mongodb::singleton();
}
return \Bnomei\Mongodb::singleton()->collection($collection);
}
}
if (! function_exists('khulan')) {
function khulan(string|array|null $search = null, ?array $options = null): mixed
{
$collection = \Bnomei\Mongodb::singleton()->contentCollection();
// only get these fields as it is faster and enough for kirby
$options ??= [
'projection' => [
'id' => 1,
'uuid' => 1,
'modelType' => 1,
],
];
if (is_array($search)) {
return Khulan::documentsToModels($collection->find($search, $options));
} elseif (is_string($search)) {
return Khulan::documentToModel($collection->findOne([
'$or' => [
['_id' => $search],
['id' => $search],
['uuid' => $search],
['email' => $search], // user
],
], $options));
}
return $collection;
}
}
Kirby::plugin('bnomei/mongodb', [
'options' => [
// plugin
'cache' => true,
// mongodb
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'kirby',
'username' => null,
'password' => null,
'uriOptions' => [],
'driverOptions' => [],
'auto-clean-cache' => true,
// collections
'collections' => [
'cache' => 'cache',
'content' => 'kirby',
],
// khulan
'khulan' => [ // cache for models
'read' => false, // mongodb is most likely slower than file system for the pages
'write' => true,
'patch-files-class' => true, // monkey patch files class
],
],
'cacheTypes' => [
'mongodb' => \Bnomei\Mongodb::class,
],
'commands' => [
'khulan:index' => [
'description' => 'Force indexing all pages/files/users',
'args' => [],
'command' => static function (CLI $cli): void {
$meta = Khulan::index(1);
$count = $meta['count'];
$time = round($meta['time'] * 1000);
$cli->success('Indexed '.$count.' in '.$time.'ms.'); // @phpstan-ignore-line
if (function_exists('janitor')) {
janitor()->data($cli->arg('command'), [
'status' => 200,
'message' => 'Indexed '.$count.' in '.$time.'ms.',
]);
}
},
],
],
'hooks' => [
'system.loadPlugins:after' => function () {
if ((option('bnomei.mongodb.khulan.read') ||
option('bnomei.mongodb.khulan.write')) &&
\Bnomei\Mongodb::singleton()->contentCollection()->countDocuments() === 0) {
Khulan::index();
}
if (option('bnomei.mongodb.khulan.patch-files-class')) {
$filesClass = (new ReflectionClass(Files::class))->getFileName();
if ($filesClass === false) {
return;
}
if (F::exists($filesClass) && F::isWritable($filesClass)) {
$code = F::read($filesClass);
if ($code && Str::contains($code, '\Bnomei\KhulanFile::factory') === false) {
$code = str_replace('File::factory(', '\Bnomei\KhulanFile::factory(', $code);
F::write($filesClass, $code);
}
}
}
},
],
]);