-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathcomponents.php
427 lines (366 loc) · 9.91 KB
/
components.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
use Kirby\Cms\App;
use Kirby\Cms\Collection;
use Kirby\Cms\File;
use Kirby\Cms\FileVersion;
use Kirby\Cms\Page;
use Kirby\Cms\User;
use Kirby\Data\Data;
use Kirby\Email\PHPMailer as Emailer;
use Kirby\Exception\NotFoundException;
use Kirby\Filesystem\F;
use Kirby\Filesystem\Filename;
use Kirby\Http\Uri;
use Kirby\Http\Url;
use Kirby\Image\Darkroom;
use Kirby\Template\Snippet;
use Kirby\Template\Template;
use Kirby\Text\Markdown;
use Kirby\Text\SmartyPants;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use Kirby\Uuid\Uuid;
return [
/**
* Used by the `css()` helper
*
* @param string $url Relative or absolute URL
* @param string|array $options An array of attributes for the link tag or a media attribute string
*/
'css' => fn (App $kirby, string $url, $options = null): string => $url,
/**
* Add your own email provider
*/
'email' => function (
App $kirby,
array $props = [],
bool $debug = false
) {
return new Emailer($props, $debug);
},
/**
* Modify URLs for file objects
*
* @param \Kirby\Cms\File $file The original file object
*/
'file::url' => function (
App $kirby,
File $file
): string {
return $file->mediaUrl();
},
/**
* Adapt file characteristics
*
* @param \Kirby\Cms\File|\Kirby\Filesystem\Asset $file The file object
* @param array $options All thumb options (width, height, crop, blur, grayscale)
* @return \Kirby\Cms\File|\Kirby\Cms\FileVersion|\Kirby\Filesystem\Asset
*/
'file::version' => function (
App $kirby,
$file,
array $options = []
) {
// if file is not resizable, return
if ($file->isResizable() === false) {
return $file;
}
// create url and root
$mediaRoot = dirname($file->mediaRoot());
$template = $mediaRoot . '/{{ name }}{{ attributes }}.{{ extension }}';
$thumbRoot = (new Filename($file->root(), $template, $options))->toString();
$thumbName = basename($thumbRoot);
// check if the thumb already exists
if (file_exists($thumbRoot) === false) {
// if not, create job file
$job = $mediaRoot . '/.jobs/' . $thumbName . '.json';
try {
Data::write($job, array_merge($options, [
'filename' => $file->filename()
]));
} catch (Throwable) {
// if thumb doesn't exist yet and job file cannot
// be created, return
return $file;
}
}
return new FileVersion([
'modifications' => $options,
'original' => $file,
'root' => $thumbRoot,
'url' => dirname($file->mediaUrl()) . '/' . $thumbName,
]);
},
/**
* Used by the `js()` helper
*
* @param string $url Relative or absolute URL
* @param string|array $options An array of attributes for the link tag or a media attribute string
*/
'js' => fn (App $kirby, string $url, $options = null): string => $url,
/**
* Add your own Markdown parser
*
* @param string $text Text to parse
* @param array $options Markdown options
*/
'markdown' => function (
App $kirby,
string $text = null,
array $options = []
): string {
static $markdown;
static $config;
// if the config options have changed or the component is called for the first time,
// (re-)initialize the parser object
if ($config !== $options) {
$markdown = new Markdown($options);
$config = $options;
}
return $markdown->parse($text, $options['inline'] ?? false);
},
/**
* Add your own search engine
*
* @param \Kirby\Cms\Collection $collection Collection of searchable models
*/
'search' => function (
App $kirby,
Collection $collection,
string|null $query = null,
string|array $params = []
): Collection {
if (is_string($params) === true) {
$params = ['fields' => Str::split($params, '|')];
}
$defaults = [
'fields' => [],
'minlength' => 2,
'score' => [],
'words' => false,
];
$collection = clone $collection;
$options = array_merge($defaults, $params);
$query = trim($query ?? '');
// empty or too short search query
if (Str::length($query) < $options['minlength']) {
return $collection->limit(0);
}
$words = preg_replace('/(\s)/u', ',', $query);
$words = Str::split($words, ',', $options['minlength']);
if (empty($options['stopwords']) === false) {
$words = array_diff($words, $options['stopwords']);
}
// returns an empty collection if there is no search word
if (empty($words) === true) {
return $collection->limit(0);
}
$words = A::map(
$words,
fn ($value) => Str::wrap(preg_quote($value), $options['words'] ? '\b' : '')
);
$exact = preg_quote($query);
if ($options['words']) {
$exact = '(\b' . $exact . '\b)';
}
$query = Str::lower($query);
$preg = '!(' . implode('|', $words) . ')!iu';
$scores = [];
$results = $collection->filter(function ($item) use ($query, $exact, $preg, $options, &$scores) {
$data = $item->content()->toArray();
$keys = array_keys($data);
$keys[] = 'id';
if ($item instanceof User) {
$keys[] = 'name';
$keys[] = 'email';
$keys[] = 'role';
} elseif ($item instanceof Page) {
// apply the default score for pages
$options['score'] = array_merge(
['id' => 64, 'title' => 64],
$options['score']
);
}
if (empty($options['fields']) === false) {
$fields = array_map('strtolower', $options['fields']);
$keys = array_intersect($keys, $fields);
}
$scoring = [
'hits' => 0,
'score' => 0
];
foreach ($keys as $key) {
$score = $options['score'][$key] ?? 1;
$value = $data[$key] ?? (string)$item->$key();
$lowerValue = Str::lower($value);
// check for exact matches
if ($query == $lowerValue) {
$scoring['score'] += 16 * $score;
$scoring['hits'] += 1;
// check for exact beginning matches
} elseif (
$options['words'] === false &&
Str::startsWith($lowerValue, $query) === true
) {
$scoring['score'] += 8 * $score;
$scoring['hits'] += 1;
// check for exact query matches
} elseif ($matches = preg_match_all('!' . $exact . '!ui', $value, $r)) {
$scoring['score'] += 2 * $score;
$scoring['hits'] += $matches;
}
// check for any match
if ($matches = preg_match_all($preg, $value, $r)) {
$scoring['score'] += $matches * $score;
$scoring['hits'] += $matches;
}
}
$scores[$item->id()] = $scoring;
return $scoring['hits'] > 0;
});
return $results->sort(
fn ($item) => $scores[$item->id()]['score'],
'desc'
);
},
/**
* Add your own SmartyPants parser
*
* @param string $text Text to parse
* @param array $options SmartyPants options
*/
'smartypants' => function (
App $kirby,
string $text = null,
array $options = []
): string {
static $smartypants;
static $config;
// if the config options have changed or the component is called for the first time,
// (re-)initialize the parser object
if ($config !== $options) {
$smartypants = new Smartypants($options);
$config = $options;
}
return $smartypants->parse($text);
},
/**
* Add your own snippet loader
*
* @param string|array $name Snippet name
* @param array $data Data array for the snippet
*/
'snippet' => function (
App $kirby,
string|array|null $name,
array $data = [],
bool $slots = false
): Snippet|string {
return Snippet::factory($name, $data, $slots);
},
/**
* Add your own template engine
*
* @param string $name Template name
* @param string $type Extension type
* @param string $defaultType Default extension type
* @return \Kirby\Template\Template
*/
'template' => function (
App $kirby,
string $name,
string $type = 'html',
string $defaultType = 'html'
) {
return new Template($name, $type, $defaultType);
},
/**
* Add your own thumb generator
*
* @param string $src Root of the original file
* @param string $dst Template string for the root to the desired destination
* @param array $options All thumb options that should be applied: `width`, `height`, `crop`, `blur`, `grayscale`
* @return string
*/
'thumb' => function (
App $kirby,
string $src,
string $dst,
array $options
): string {
$darkroom = Darkroom::factory(
$kirby->option('thumbs.driver', 'gd'),
$kirby->option('thumbs', [])
);
$options = $darkroom->preprocess($src, $options);
$root = (new Filename($src, $dst, $options))->toString();
F::copy($src, $root, true);
$darkroom->process($root, $options);
return $root;
},
/**
* Modify all URLs
*
* @param string|null $path URL path
* @param array|string|null $options Array of options for the Uri class
* @throws \Kirby\Exception\NotFoundException If an invalid UUID was passed
*/
'url' => function (
App $kirby,
string $path = null,
$options = null
): string {
$language = null;
// get language from simple string option
if (is_string($options) === true) {
$language = $options;
$options = null;
}
// get language from array
if (is_array($options) === true && isset($options['language']) === true) {
$language = $options['language'];
unset($options['language']);
}
// get a language url for the linked page, if the page can be found
if ($kirby->multilang() === true) {
$parts = Str::split($path, '#');
if ($parts[0] ?? null) {
$page = $kirby->site()->find($parts[0]);
} else {
$page = $kirby->site()->page();
}
if ($page) {
$path = $page->url($language);
if (isset($parts[1]) === true) {
$path .= '#' . $parts[1];
}
}
}
// keep relative urls
if (
$path !== null &&
(substr($path, 0, 2) === './' || substr($path, 0, 3) === '../')
) {
return $path;
}
// support UUIDs
if (
$path !== null &&
(
Uuid::is($path, 'page') === true ||
Uuid::is($path, 'file') === true
)
) {
$model = Uuid::for($path)->model();
if ($model === null) {
throw new NotFoundException('The model could not be found for "' . $path . '" uuid');
}
$path = $model->url();
}
$url = Url::makeAbsolute($path, $kirby->url());
if ($options === null) {
return $url;
}
return (new Uri($url, $options))->toString();
},
];