julienmonnerie/kirby/src/Cms/Find.php

176 lines
4.2 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cms;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\Str;
/**
* The Find class is used in the API and
* the Panel to find models and parents
* based on request paths
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Find
{
2022-08-31 15:02:43 +02:00
/**
* Returns the file object for the given
* parent path and filename
*
2024-12-20 12:37:52 +01:00
* @param string $path Path to file's parent model
2022-08-31 15:02:43 +02:00
* @throws \Kirby\Exception\NotFoundException if the file cannot be found
*/
2025-04-21 18:57:21 +02:00
public static function file(
string $path,
string $filename
): File|null {
2022-08-31 15:02:43 +02:00
$filename = urldecode($filename);
2025-04-21 18:57:21 +02:00
$parent = empty($path) ? null : static::parent($path);
$file = App::instance()->file($filename, $parent);
2022-08-31 15:02:43 +02:00
2025-04-21 18:57:21 +02:00
if ($file?->isAccessible() === true) {
2022-08-31 15:02:43 +02:00
return $file;
}
throw new NotFoundException([
'key' => 'file.notFound',
'data' => [
'filename' => $filename
]
]);
}
/**
* Returns the language object for the given code
*
* @param string $code Language code
* @throws \Kirby\Exception\NotFoundException if the language cannot be found
*/
2025-04-21 18:57:21 +02:00
public static function language(string $code): Language|null
2022-08-31 15:02:43 +02:00
{
if ($language = App::instance()->language($code)) {
return $language;
}
throw new NotFoundException([
'key' => 'language.notFound',
'data' => [
'code' => $code
]
]);
}
/**
* Returns the page object for the given id
*
* @param string $id Page's id
* @throws \Kirby\Exception\NotFoundException if the page cannot be found
*/
2025-04-21 18:57:21 +02:00
public static function page(string $id): Page|null
2022-08-31 15:02:43 +02:00
{
2025-04-21 18:57:21 +02:00
// decode API ID encoding
$id = str_replace(['+', ' '], '/', $id);
$kirby = App::instance();
$page = $kirby->page($id, null, true);
2022-08-31 15:02:43 +02:00
2025-04-21 18:57:21 +02:00
if ($page?->isAccessible() === true) {
2022-08-31 15:02:43 +02:00
return $page;
}
throw new NotFoundException([
'key' => 'page.notFound',
'data' => [
'slug' => $id
]
]);
}
/**
* Returns the model's object for the given path
*
* @param string $path Path to parent model
* @throws \Kirby\Exception\InvalidArgumentException if the model type is invalid
* @throws \Kirby\Exception\NotFoundException if the model cannot be found
*/
2025-04-21 18:57:21 +02:00
public static function parent(string $path): ModelWithContent
2022-08-31 15:02:43 +02:00
{
$path = trim($path, '/');
$modelType = in_array($path, ['site', 'account']) ? $path : trim(dirname($path), '/');
$modelTypes = [
'site' => 'site',
'users' => 'user',
'pages' => 'page',
'account' => 'account'
];
$modelName = $modelTypes[$modelType] ?? null;
if (Str::endsWith($modelType, '/files') === true) {
$modelName = 'file';
}
$kirby = App::instance();
2022-12-19 14:56:05 +01:00
$model = match ($modelName) {
'site' => $kirby->site(),
'account' => static::user(),
'page' => static::page(basename($path)),
2024-12-20 12:37:52 +01:00
// regular expression to split the path at the last
// occurrence of /files/ which separates parent path
// and filename
'file' => static::file(...preg_split('$.*\K(/files/)$', $path)),
2022-12-19 14:56:05 +01:00
'user' => $kirby->user(basename($path)),
default => throw new InvalidArgumentException('Invalid model type: ' . $modelType)
};
2022-08-31 15:02:43 +02:00
2022-12-19 14:56:05 +01:00
return $model ?? throw new NotFoundException([
2022-08-31 15:02:43 +02:00
'key' => $modelName . '.undefined'
]);
}
/**
* Returns the user object for the given id or
* returns the current authenticated user if no
* id is passed
*
* @param string|null $id User's id
* @throws \Kirby\Exception\NotFoundException if the user for the given id cannot be found
*/
2025-04-21 18:57:21 +02:00
public static function user(string|null $id = null): User|null
2022-08-31 15:02:43 +02:00
{
// account is a reserved word to find the current
// user. It's used in various API and area routes.
if ($id === 'account') {
$id = null;
}
$kirby = App::instance();
// get the authenticated user
if ($id === null) {
2022-12-19 14:56:05 +01:00
$user = $kirby->user(
null,
$kirby->option('api.allowImpersonation', false)
);
2022-08-31 15:02:43 +02:00
2022-12-19 14:56:05 +01:00
return $user ?? throw new NotFoundException([
2022-08-31 15:02:43 +02:00
'key' => 'user.undefined'
]);
}
// get a specific user by id
2022-12-19 14:56:05 +01:00
return $kirby->user($id) ?? throw new NotFoundException([
2022-08-31 15:02:43 +02:00
'key' => 'user.notFound',
'data' => [
'name' => $id
]
]);
}
2022-06-17 17:51:59 +02:00
}