Upgrade to 3.8.2

This commit is contained in:
Bastian Allgeier
2022-11-15 13:26:12 +01:00
parent fe2baa1b7a
commit f9e812cb0c
45 changed files with 760 additions and 346 deletions

View File

@@ -383,7 +383,7 @@ class App
*/
public function collections()
{
return $this->collections = $this->collections ?? new Collections();
return $this->collections ??= new Collections();
}
/**
@@ -564,7 +564,7 @@ class App
*/
public function defaultLanguage()
{
return $this->defaultLanguage = $this->defaultLanguage ?? $this->languages()->default();
return $this->defaultLanguage ??= $this->languages()->default();
}
/**
@@ -928,11 +928,15 @@ class App
return $this->defaultLanguage();
}
// if requesting a non-default language,
// find it but don't cache it
if ($code !== null) {
return $this->languages()->find($code);
}
return $this->language = $this->language ?? $this->defaultLanguage();
// otherwise return language set by `AppTranslation::setCurrentLanguage`
// or default language
return $this->language ??= $this->defaultLanguage();
}
/**
@@ -1025,7 +1029,7 @@ class App
*/
public function nonce(): string
{
return $this->nonce = $this->nonce ?? base64_encode(random_bytes(20));
return $this->nonce ??= base64_encode(random_bytes(20));
}
/**
@@ -1345,7 +1349,7 @@ class App
*/
public function response()
{
return $this->response = $this->response ?? new Responder();
return $this->response ??= new Responder();
}
/**
@@ -1355,7 +1359,7 @@ class App
*/
public function roles()
{
return $this->roles = $this->roles ?? Roles::load($this->root('roles'));
return $this->roles ??= Roles::load($this->root('roles'));
}
/**
@@ -1578,7 +1582,7 @@ class App
*/
public function site()
{
return $this->site = $this->site ?? new Site([
return $this->site ??= new Site([
'errorPageId' => $this->options['error'] ?? 'error',
'homePageId' => $this->options['home'] ?? 'home',
'kirby' => $this,
@@ -1647,7 +1651,7 @@ class App
*/
public function system()
{
return $this->system = $this->system ?? new System($this);
return $this->system ??= new System($this);
}
/**
@@ -1771,7 +1775,7 @@ class App
public static function version(): string|null
{
try {
return static::$version = static::$version ?? Data::read(dirname(__DIR__, 2) . '/composer.json')['version'] ?? null;
return static::$version ??= Data::read(dirname(__DIR__, 2) . '/composer.json')['version'] ?? null;
} catch (Throwable) {
throw new LogicException('The Kirby version cannot be detected. The composer.json is probably missing or not readable.');
}
@@ -1794,6 +1798,6 @@ class App
*/
public function visitor()
{
return $this->visitor = $this->visitor ?? new Visitor();
return $this->visitor ??= new Visitor();
}
}

View File

@@ -140,10 +140,7 @@ trait AppPlugins
protected function extendAreas(array $areas): array
{
foreach ($areas as $id => $area) {
if (isset($this->extensions['areas'][$id]) === false) {
$this->extensions['areas'][$id] = [];
}
$this->extensions['areas'][$id] ??= [];
$this->extensions['areas'][$id][] = $area;
}
@@ -388,9 +385,7 @@ trait AppPlugins
protected function extendHooks(array $hooks): array
{
foreach ($hooks as $name => $callbacks) {
if (isset($this->extensions['hooks'][$name]) === false) {
$this->extensions['hooks'][$name] = [];
}
$this->extensions['hooks'][$name] ??= [];
if (is_array($callbacks) === false) {
$callbacks = [$callbacks];

View File

@@ -31,7 +31,7 @@ trait AppUsers
*/
public function auth()
{
return $this->auth = $this->auth ?? new Auth($this);
return $this->auth ??= new Auth($this);
}
/**

View File

@@ -63,9 +63,7 @@ class Collections
public function get(string $name, array $data = [])
{
// if not yet loaded
if (isset($this->collections[$name]) === false) {
$this->collections[$name] = $this->load($name);
}
$this->collections[$name] ??= $this->load($name);
// if not yet cached
if (

View File

@@ -164,13 +164,11 @@ class Content
$key = strtolower($key);
if (isset($this->fields[$key])) {
return $this->fields[$key];
}
$value = $this->data()[$key] ?? null;
return $this->fields[$key] = new Field($this->parent, $key, $value);
return $this->fields[$key] ??= new Field(
$this->parent,
$key,
$this->data()[$key] ?? null
);
}
/**

View File

@@ -84,26 +84,17 @@ class ContentTranslation
*/
public function content(): array
{
$parent = $this->parent();
if ($this->content === null) {
$this->content = $parent->readContent($this->code());
}
$content = $this->content;
$parent = $this->parent();
$content = $this->content ??= $parent->readContent($this->code());
// merge with the default content
if (
$this->isDefault() === false &&
$defaultLanguage = $parent->kirby()->defaultLanguage()
) {
$default = [];
if ($defaultTranslation = $parent->translation($defaultLanguage->code())) {
$default = $defaultTranslation->content();
if ($default = $parent->translation($defaultLanguage->code())?->content()) {
$content = array_merge($default, $content);
}
$content = array_merge($default, $content);
}
return $content;

View File

@@ -49,14 +49,10 @@ class Email
$this->props = array_merge($preset, $props);
// add transport settings
if (isset($this->props['transport']) === false) {
$this->props['transport'] = $this->options['transport'] ?? [];
}
$this->props['transport'] ??= $this->options['transport'] ?? [];
// add predefined beforeSend option
if (isset($this->props['beforeSend']) === false) {
$this->props['beforeSend'] = $this->options['beforeSend'] ?? null;
}
$this->props['beforeSend'] ??= $this->options['beforeSend'] ?? null;
// transform model objects to values
$this->transformUserSingle('from', 'fromName');
@@ -235,12 +231,7 @@ class Email
$this->props[$addressProp] = $address;
// only use the name from the user if no custom name was set
if (
isset($this->props[$nameProp]) === false ||
$this->props[$nameProp] === null
) {
$this->props[$nameProp] = $name;
}
$this->props[$nameProp] ??= $name;
}
/**

View File

@@ -295,11 +295,7 @@ class File extends ModelWithContent
$template = $this->template();
if (isset($readable[$template]) === true) {
return $readable[$template];
}
return $readable[$template] = $this->permissions()->can('read');
return $readable[$template] ??= $this->permissions()->can('read');
}
/**

View File

@@ -777,11 +777,7 @@ class Page extends ModelWithContent
$template = $this->intendedTemplate()->name();
if (isset($readable[$template]) === true) {
return $readable[$template];
}
return $readable[$template] = $this->permissions()->can('read');
return $readable[$template] ??= $this->permissions()->can('read');
}
/**

View File

@@ -27,6 +27,55 @@ use Kirby\Uuid\Uuids;
*/
trait PageActions
{
/**
* Adapts necessary modifications which page uuid, page slug and files uuid
* of copy objects for single or multilang environments
*/
protected function adaptCopy(Page $copy, bool $files = false): Page
{
if ($this->kirby()->multilang() === true) {
foreach ($this->kirby()->languages() as $language) {
// overwrite with new UUID for the page and files
// for default language (remove old, add new)
if (
Uuids::enabled() === true &&
$language->isDefault() === true
) {
$copy = $copy->save(['uuid' => Uuid::generate()], $language->code());
if ($files !== false) {
foreach ($copy->files() as $file) {
$file->save(['uuid' => Uuid::generate()], $language->code());
}
}
}
// remove all translated slugs
if (
$language->isDefault() === false &&
$copy->translation($language)->exists() === true
) {
$copy = $copy->save(['slug' => null], $language->code());
}
}
return $copy;
}
// overwrite with new UUID for the page and files (remove old, add new)
if (Uuids::enabled() === true) {
$copy = $copy->save(['uuid' => Uuid::generate()]);
if ($files !== false) {
foreach ($copy->files() as $file) {
$file->save(['uuid' => Uuid::generate()]);
}
}
}
return $copy;
}
/**
* Changes the sorting number.
* The sorting number must already be correct
@@ -434,19 +483,8 @@ trait PageActions
$copy = $parentModel->clone()->findPageOrDraft($slug);
// remove all translated slugs
if ($this->kirby()->multilang() === true) {
foreach ($this->kirby()->languages() as $language) {
if ($language->isDefault() === false && $copy->translation($language)->exists() === true) {
$copy = $copy->save(['slug' => null], $language->code());
}
}
}
// overwrite with new UUID (remove old, add new)
if (Uuids::enabled() === true) {
$copy = $copy->save(['uuid' => Uuid::generate()]);
}
// normalize copy object
$copy = $this->adaptCopy($copy, $files);
// add copy to siblings
static::updateParentCollections($copy, 'append', $parentModel);
@@ -776,9 +814,9 @@ trait PageActions
foreach ($sorted as $key => $id) {
if ($id === $this->id()) {
continue;
} elseif ($sibling = $siblings->get($id)) {
$sibling->changeNum($key + 1);
}
$siblings->get($id)?->changeNum($key + 1);
}
$parent = $this->parentModel();

View File

@@ -79,11 +79,7 @@ class PageBlueprint extends Blueprint
'sort' => 'default',
];
if (isset($aliases[$num]) === true) {
return $aliases[$num];
}
return $num;
return $aliases[$num] ?? $num;
}
/**
@@ -144,9 +140,7 @@ class PageBlueprint extends Blueprint
}
// also make sure to have the text field set
if (isset($status[$key]['text']) === false) {
$status[$key]['text'] = null;
}
$status[$key]['text'] ??= null;
// translate text and label if necessary
$status[$key]['label'] = $this->i18n($status[$key]['label'], $status[$key]['label']);

View File

@@ -19,20 +19,11 @@ class Body
{
use Properties;
/**
* @var string
*/
protected $html;
/**
* @var string
*/
protected $text;
protected string|null $html = null;
protected string|null $text = null;
/**
* Email body constructor
*
* @param array $props
*/
public function __construct(array $props = [])
{
@@ -41,20 +32,16 @@ class Body
/**
* Returns the HTML content of the email body
*
* @return string
*/
public function html()
public function html(): string
{
return $this->html ?? '';
}
/**
* Returns the plain text content of the email body
*
* @return string
*/
public function text()
public function text(): string
{
return $this->text ?? '';
}
@@ -62,10 +49,9 @@ class Body
/**
* Sets the HTML content for the email body
*
* @param string|null $html
* @return $this
*/
protected function setHtml(string $html = null)
protected function setHtml(string|null $html = null): static
{
$this->html = $html;
return $this;
@@ -74,10 +60,9 @@ class Body
/**
* Sets the plain text content for the email body
*
* @param string|null $text
* @return $this
*/
protected function setText(string $text = null)
protected function setText(string|null $text = null): static
{
$this->text = $text;
return $this;

View File

@@ -24,89 +24,61 @@ class Email
/**
* If set to `true`, the debug mode is enabled
* for all emails
*
* @var bool
*/
public static $debug = false;
public static bool $debug = false;
/**
* Store for sent emails when `Email::$debug`
* is set to `true`
*
*/
public static array $emails = [];
/**
* @var array
*/
public static $emails = [];
protected array|null $attachments = null;
protected Body|null $body = null;
/**
* @var array|null
* @var array
*/
protected $attachments;
protected array|null $bcc = null;
protected Closure|null $beforeSend = null;
/**
* @var \Kirby\Email\Body|null
* @var array
*/
protected $body;
protected array|null $cc = null;
/**
* @var array|null
* @var string
*/
protected $bcc;
protected string|null $from = null;
protected string|null $fromName = null;
protected bool $isSent = false;
/**
* @var \Closure|null
* @var string
*/
protected $beforeSend;
protected string|null $replyTo = null;
protected string|null $replyToName = null;
/**
* @var array|null
* @var string
*/
protected $cc;
protected string|null $subject = null;
/**
* @var string|null
* @var array
*/
protected $from;
/**
* @var string|null
*/
protected $fromName;
/**
* @var string|null
*/
protected $replyTo;
/**
* @var string|null
*/
protected $replyToName;
/**
* @var bool
*/
protected $isSent = false;
/**
* @var string|null
*/
protected $subject;
/**
* @var array|null
*/
protected $to;
/**
* @var array|null
*/
protected $transport;
protected array|null $to = null;
protected array|null $transport = null;
/**
* Email constructor
*
* @param array $props
* @param bool $debug
*/
public function __construct(array $props = [], bool $debug = false)
{
@@ -123,8 +95,6 @@ class Email
/**
* Returns the email attachments
*
* @return array
*/
public function attachments(): array
{
@@ -133,18 +103,14 @@ class Email
/**
* Returns the email body
*
* @return \Kirby\Email\Body|null
*/
public function body()
public function body(): Body|null
{
return $this->body;
}
/**
* Returns "bcc" recipients
*
* @return array
*/
public function bcc(): array
{
@@ -154,8 +120,6 @@ class Email
/**
* Returns the beforeSend callback closure,
* which has access to the PHPMailer instance
*
* @return \Closure|null
*/
public function beforeSend(): Closure|null
{
@@ -164,8 +128,6 @@ class Email
/**
* Returns "cc" recipients
*
* @return array
*/
public function cc(): array
{
@@ -174,8 +136,6 @@ class Email
/**
* Returns default transport settings
*
* @return array
*/
protected function defaultTransport(): array
{
@@ -186,8 +146,6 @@ class Email
/**
* Returns the "from" email address
*
* @return string
*/
public function from(): string
{
@@ -196,8 +154,6 @@ class Email
/**
* Returns the "from" name
*
* @return string|null
*/
public function fromName(): string|null
{
@@ -206,18 +162,14 @@ class Email
/**
* Checks if the email has an HTML body
*
* @return bool
*/
public function isHtml()
public function isHtml(): bool
{
return empty($this->body()->html()) === false;
}
/**
* Checks if the email has been sent successfully
*
* @return bool
*/
public function isSent(): bool
{
@@ -226,8 +178,6 @@ class Email
/**
* Returns the "reply to" email address
*
* @return string
*/
public function replyTo(): string
{
@@ -236,8 +186,6 @@ class Email
/**
* Returns the "reply to" name
*
* @return string|null
*/
public function replyToName(): string|null
{
@@ -247,13 +195,12 @@ class Email
/**
* Converts single or multiple email addresses to a sanitized format
*
* @param string|array|null $email
* @param bool $multiple
* @return array|mixed|string
* @throws \Exception
*/
protected function resolveEmail($email = null, bool $multiple = true)
{
protected function resolveEmail(
string|array|null $email = null,
bool $multiple = true
): array|string {
if ($email === null) {
return $multiple === true ? [] : '';
}
@@ -284,8 +231,6 @@ class Email
/**
* Sends the email
*
* @return bool
*/
public function send(): bool
{
@@ -295,10 +240,9 @@ class Email
/**
* Sets the email attachments
*
* @param array|null $attachments
* @return $this
*/
protected function setAttachments($attachments = null)
protected function setAttachments(array|null $attachments = null): static
{
$this->attachments = $attachments ?? [];
return $this;
@@ -307,10 +251,9 @@ class Email
/**
* Sets the email body
*
* @param string|array $body
* @return $this
*/
protected function setBody($body)
protected function setBody(string|array $body): static
{
if (is_string($body) === true) {
$body = ['text' => $body];
@@ -323,10 +266,9 @@ class Email
/**
* Sets "bcc" recipients
*
* @param string|array|null $bcc
* @return $this
*/
protected function setBcc($bcc = null)
protected function setBcc(string|array|null $bcc = null): static
{
$this->bcc = $this->resolveEmail($bcc);
return $this;
@@ -335,10 +277,9 @@ class Email
/**
* Sets the "beforeSend" callback
*
* @param \Closure|null $beforeSend
* @return $this
*/
protected function setBeforeSend(Closure|null $beforeSend = null)
protected function setBeforeSend(Closure|null $beforeSend = null): static
{
$this->beforeSend = $beforeSend;
return $this;
@@ -347,10 +288,9 @@ class Email
/**
* Sets "cc" recipients
*
* @param string|array|null $cc
* @return $this
*/
protected function setCc($cc = null)
protected function setCc(string|array|null $cc = null): static
{
$this->cc = $this->resolveEmail($cc);
return $this;
@@ -359,10 +299,9 @@ class Email
/**
* Sets the "from" email address
*
* @param string $from
* @return $this
*/
protected function setFrom(string $from)
protected function setFrom(string $from): static
{
$this->from = $this->resolveEmail($from, false);
return $this;
@@ -371,10 +310,9 @@ class Email
/**
* Sets the "from" name
*
* @param string|null $fromName
* @return $this
*/
protected function setFromName(string $fromName = null)
protected function setFromName(string|null $fromName = null): static
{
$this->fromName = $fromName;
return $this;
@@ -383,10 +321,9 @@ class Email
/**
* Sets the "reply to" email address
*
* @param string|null $replyTo
* @return $this
*/
protected function setReplyTo(string $replyTo = null)
protected function setReplyTo(string|null $replyTo = null): static
{
$this->replyTo = $this->resolveEmail($replyTo, false);
return $this;
@@ -395,10 +332,9 @@ class Email
/**
* Sets the "reply to" name
*
* @param string|null $replyToName
* @return $this
*/
protected function setReplyToName(string $replyToName = null)
protected function setReplyToName(string|null $replyToName = null): static
{
$this->replyToName = $replyToName;
return $this;
@@ -407,10 +343,9 @@ class Email
/**
* Sets the email subject
*
* @param string $subject
* @return $this
*/
protected function setSubject(string $subject)
protected function setSubject(string $subject): static
{
$this->subject = $subject;
return $this;
@@ -419,10 +354,9 @@ class Email
/**
* Sets the recipients of the email
*
* @param string|array $to
* @return $this
*/
protected function setTo($to)
protected function setTo(string|array $to): static
{
$this->to = $this->resolveEmail($to);
return $this;
@@ -431,10 +365,9 @@ class Email
/**
* Sets the email transport settings
*
* @param array|null $transport
* @return $this
*/
protected function setTransport($transport = null)
protected function setTransport(array|null $transport = null): static
{
$this->transport = $transport;
return $this;
@@ -442,8 +375,6 @@ class Email
/**
* Returns the email subject
*
* @return string
*/
public function subject(): string
{
@@ -452,8 +383,6 @@ class Email
/**
* Returns the email recipients
*
* @return array
*/
public function to(): array
{
@@ -462,8 +391,6 @@ class Email
/**
* Returns the email transports settings
*
* @return array
*/
public function transport(): array
{

View File

@@ -21,8 +21,6 @@ class PHPMailer extends Email
/**
* Sends email via PHPMailer library
*
* @param bool $debug
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function send(bool $debug = false): bool

View File

@@ -109,9 +109,7 @@ class Form
$input = array_merge($values, $input);
foreach ($input as $key => $value) {
if (isset($this->values[$key]) === false) {
$this->values[$key] = $value;
}
$this->values[$key] ??= $value;
}
}
}

View File

@@ -7,8 +7,8 @@ use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Http\Remote;
use Kirby\Http\Url;
use Kirby\Query\Query;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Query;
use Kirby\Toolkit\Str;
/**
@@ -134,7 +134,7 @@ class OptionsApi
throw new InvalidArgumentException('Invalid options format');
}
$result = (new Query($this->fetch(), Nest::create($data)))->result();
$result = (new Query($this->fetch()))->resolve(Nest::create($data));
$options = [];
foreach ($result as $item) {

View File

@@ -5,10 +5,10 @@ namespace Kirby\Form;
use Kirby\Cms\Field;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Query\Query;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Obj;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Query;
use Kirby\Toolkit\Str;
/**
@@ -117,8 +117,8 @@ class OptionsQuery
}
$data = $this->data();
$query = new Query($this->query(), $data);
$result = $query->result();
$query = new Query($this->query());
$result = $query->resolve($data);
$result = $this->resultToCollection($result);
$options = [];

View File

@@ -47,7 +47,7 @@ class Url
*/
public static function current(): string
{
return static::$current = static::$current ?? static::toObject()->toString();
return static::$current ??= static::toObject()->toString();
}
/**

View File

@@ -8,7 +8,7 @@ use Kirby\Data\Json;
use Kirby\Exception\NotFoundException;
use Kirby\Http\Remote;
use Kirby\Http\Url;
use Kirby\Toolkit\Query;
use Kirby\Query\Query;
/**
* Options fetched from any REST API
@@ -108,7 +108,9 @@ class OptionsApi extends OptionsProvider
throw new NotFoundException('Options could not be loaded from API: ' . $model->toSafeString($this->url));
}
$data = (new Query($this->query, Nest::create($data)))->result();
// turn data into Nest so that it can be queried
$data = Nest::create($data);
$data = Query::factory($this->query)->resolve($data);
// create options by resolving text and value query strings
// for each item from the data

View File

@@ -159,9 +159,7 @@ abstract class Model
}
}
if (isset($settings['query']) === true) {
unset($settings['query']);
}
unset($settings['query']);
// resolve remaining options defined as query
return A::map($settings, function ($option) {

View File

@@ -0,0 +1,100 @@
<?php
namespace Kirby\Query;
use Closure;
use Kirby\Toolkit\Str;
/**
* The Argument class represents a single
* parameter passed to a method in a chained query
*
* @package Kirby Query
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
final class Argument
{
public function __construct(
public mixed $value
) {
}
/**
* Sanitizes argument string into
* PHP type/object as new Argument instance
*/
public static function factory(string $argument): static
{
$argument = trim($argument);
// string with single or double quotes
if (
(
Str::startsWith($argument, '"') &&
Str::endsWith($argument, '"')
) || (
Str::startsWith($argument, "'") &&
Str::endsWith($argument, "'")
)
) {
$string = substr($argument, 1, -1);
$string = str_replace(['\"', "\'"], ['"', "'"], $string);
return new static($string);
}
// array: split and recursive sanitizing
if (
Str::startsWith($argument, '[') &&
Str::endsWith($argument, ']')
) {
$array = substr($argument, 1, -1);
$array = Arguments::factory($array);
return new static($array);
}
// numeric
if (is_numeric($argument) === true) {
return new static((float)$argument);
}
// Closure
if (Str::startsWith($argument, '() =>')) {
$query = Str::after($argument, '() =>');
$query = trim($query);
return new static(fn () => $query);
}
return new static(match ($argument) {
'null' => null,
'true' => true,
'false' => false,
// resolve parameter for objects and methods itself
default => new Query($argument)
});
}
/**
* Return the argument value and
* resolves nested objects to scaler types
*/
public function resolve(array|object $data = []): mixed
{
// don't resolve the Closure immediately, instead
// resolve it to the sub-query and create a new Closure
// that resolves the sub-query with the same data set once called
if ($this->value instanceof Closure) {
$query = ($this->value)();
return fn () => static::factory($query)->resolve($data);
}
if (is_object($this->value) === true) {
return $this->value->resolve($data);
}
return $this->value;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Kirby\Query;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Collection;
/**
* The Argument class represents a single
* parameter passed to a method in a chained query
*
* @package Kirby Query
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
final class Arguments extends Collection
{
public const NO_PNTH = '\([^(]+\)(*SKIP)(*FAIL)';
public const NO_SQBR = '\[[^]]+\](*SKIP)(*FAIL)';
public const NO_DLQU = '\"(?:[^"\\\\]|\\\\.)*\"(*SKIP)(*FAIL)';
public const NO_SLQU = '\'(?:[^\'\\\\]|\\\\.)*\'(*SKIP)(*FAIL)';
public static function factory(string $arguments): static
{
$arguments = A::map(
// split by comma, but not inside skip groups
preg_split('!,|' . self::NO_PNTH . '|' . self::NO_SQBR . '|' .
self::NO_DLQU . '|' . self::NO_SLQU . '!', $arguments),
fn ($argument) => Argument::factory($argument)
);
return new static($arguments);
}
public function resolve(array|object $data = []): array
{
return A::map(
$this->data,
fn ($argument) => $argument->resolve($data)
);
}
}

131
kirby/src/Query/Query.php Normal file
View File

@@ -0,0 +1,131 @@
<?php
namespace Kirby\Query;
use Closure;
use Kirby\Cms\App;
use Kirby\Cms\Collection;
use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Cms\User;
use Kirby\Toolkit\I18n;
/**
* The Query class can be used to
* query arrays and objects, including their
* methods with a very simple string-based syntax.
*
* @package Kirby Query
* @author Bastian Allgeier <bastian@getkirby.com>,
* Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Query
{
/**
* Default data entries
*/
public static array $entries = [];
/**
* Creates a new Query object
*/
public function __construct(
public string|null $query = null
) {
if ($query !== null) {
$this->query = trim($query);
}
}
/**
* Creates a new Query object
*/
public static function factory(string $query): static
{
return new static(query: $query);
}
/**
* Method to help classes that extend Query
* to intercept a segment's result.
*/
public function intercept(mixed $result): mixed
{
return $result;
}
/**
* Returns the query result if anything
* can be found, otherwise returns null
*
* @throws \Kirby\Exception\BadMethodCallException If an invalid method is accessed by the query
*/
public function resolve(array|object $data = []): mixed
{
if (empty($this->query) === true) {
return $data;
}
// merge data with default entries
if (is_array($data) === true) {
$data = array_merge(static::$entries, $data);
}
// direct data array access via key
if (
is_array($data) === true &&
array_key_exists($this->query, $data) === true
) {
$value = $data[$this->query];
if ($value instanceof Closure) {
$value = $value();
}
return $value;
}
// loop through all segments to resolve query
return Segments::factory($this->query, $this)->resolve($data);
}
}
/**
* Default entries/functions
*/
Query::$entries['kirby'] = function (): App {
return App::instance();
};
Query::$entries['collection'] = function (string $name): Collection|null {
return App::instance()->collection($name);
};
Query::$entries['file'] = function (string $id): File|null {
return App::instance()->file($id);
};
Query::$entries['page'] = function (string $id): Page|null {
return App::instance()->site()->find($id);
};
Query::$entries['site'] = function (): Site {
return App::instance()->site();
};
Query::$entries['t'] = function (
string $key,
string|array $fallback = null,
string $locale = null
): string|null {
return I18n::translate($key, $fallback, $locale);
};
Query::$entries['user'] = function (string $id = null): User|null {
return App::instance()->user($id);
};

144
kirby/src/Query/Segment.php Normal file
View File

@@ -0,0 +1,144 @@
<?php
namespace Kirby\Query;
use Closure;
use Kirby\Exception\BadMethodCallException;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Str;
/**
* The Segment class represents a single
* part of a chained query
*
* @package Kirby Query
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Segment
{
public function __construct(
public string $method,
public int $position,
public Arguments|null $arguments = null,
) {
}
/**
* Throws an exception for an access to an invalid method
*
* @param mixed $data Variable on which the access was tried
* @param string $name Name of the method/property that was accessed
* @param string $label Type of the name (`method`, `property` or `method/property`)
*
* @throws \Kirby\Exception\BadMethodCallException
*/
public static function error(mixed $data, string $name, string $label): void
{
$type = strtolower(gettype($data));
if ($type === 'double') {
$type = 'float';
}
$nonExisting = in_array($type, ['array', 'object']) ? 'non-existing ' : '';
$error = 'Access to ' . $nonExisting . $label . ' ' . $name . ' on ' . $type;
throw new BadMethodCallException($error);
}
public static function factory(
string $segment,
int $position = 0
): static {
if (Str::endsWith($segment, ')') === false) {
return new static(method: $segment, position: $position);
}
// the args are everything inside the *outer* parentheses
$args = Str::substr($segment, Str::position($segment, '(') + 1, -1);
return new static(
method: Str::before($segment, '('),
position: $position,
arguments: Arguments::factory($args)
);
}
public function resolve(mixed $base = null, array|object $data = []): mixed
{
// resolve arguments to array
$args = $this->arguments?->resolve($data) ?? [];
// 1st segment, start from $data array
if ($this->position === 0) {
if (is_array($data) == true) {
return $this->resolveArray($data, $args);
}
return $this->resolveObject($data, $args);
}
if (is_array($base) === true) {
return $this->resolveArray($base, $args);
}
if (is_object($base) === true) {
return $this->resolveObject($base, $args);
}
// trying to access further segments on a scalar/null value
static::error($base, $this->method, 'method/property');
}
/**
* Resolves segment by calling the corresponding array key
*/
protected function resolveArray(array $array, array $args): mixed
{
if (array_key_exists($this->method, $array) === false) {
static::error($array, $this->method, 'property');
}
$value = $array[$this->method];
if ($value instanceof Closure) {
return $value(...$args);
}
if ($args !== []) {
throw new InvalidArgumentException('Cannot access array element ' . $this->method . ' with arguments');
}
return $value;
}
/**
* Resolves segment by calling the method/accessing the property
* on the base object
*/
protected function resolveObject(object $object, array $args): mixed
{
if (
method_exists($object, $this->method) === true ||
method_exists($object, '__call') === true
) {
return $object->{$this->method}(...$args);
}
if (
$args === [] && (
property_exists($object, $this->method) === true ||
method_exists($object, '__get') === true
)
) {
return $object->{$this->method};
}
$label = ($args === []) ? 'method/property' : 'method';
static::error($object, $this->method, $label);
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Kirby\Query;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Collection;
/**
* The Segments class helps splitting a
* query string into processable segments
*
* @package Kirby Query
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
final class Segments extends Collection
{
public function __construct(
array $data = [],
protected Query|null $parent = null,
) {
parent::__construct($data);
}
/**
* Split query string into segments by dot
* but not inside (nested) parens
*/
public static function factory(string $query, Query $parent = null): static
{
$segments = preg_split(
'!\.|(\(([^()]+|(?1))*+\))(*SKIP)(*FAIL)!',
trim($query),
-1,
PREG_SPLIT_NO_EMPTY
);
$segments = A::map(
array_keys($segments),
fn ($index) => Segment::factory($segments[$index], $index)
);
return new static($segments, $parent);
}
public function resolve(array|object $data = [])
{
$value = null;
foreach ($this->data as $segment) {
// offer possibility to intercept on objects
if ($value !== null) {
$value = $this->parent?->intercept($value) ?? $value;
}
$value = $segment->resolve($value, $data);
}
return $value;
}
}

View File

@@ -157,7 +157,9 @@ class FileSessionStore extends SessionStore
// check if the file is already unlocked or doesn't exist
if (!isset($this->isLocked[$name])) {
return;
} elseif ($this->exists($expiryTime, $id) === false) {
}
if ($this->exists($expiryTime, $id) === false) {
unset($this->isLocked[$name]);
return;
}

View File

@@ -101,9 +101,7 @@ class Sessions
public function create(array $options = [])
{
// fall back to default mode
if (!isset($options['mode'])) {
$options['mode'] = $this->mode;
}
$options['mode'] ??= $this->mode;
return new Session($this, null, $options);
}
@@ -117,11 +115,7 @@ class Sessions
*/
public function get(string $token, string $mode = null)
{
if (isset($this->cache[$token])) {
return $this->cache[$token];
}
return $this->cache[$token] = new Session($this, $token, ['mode' => $mode ?? $this->mode]);
return $this->cache[$token] ??= new Session($this, $token, ['mode' => $mode ?? $this->mode]);
}
/**

View File

@@ -602,14 +602,19 @@ class Html extends Xml
default:
// short URLs
if (Str::contains($uri->host(), 'youtu.be') === true && $isYoutubeId($first) === true) {
if (
Str::contains($uri->host(), 'youtu.be') === true &&
$isYoutubeId($first) === true
) {
$src = 'https://www.youtube.com/embed/' . $first;
$query->start = $query->t;
unset($query->t);
// embedded video URLs
} elseif ($first === 'embed' && $isYoutubeId($second) === true) {
} elseif (
in_array($first, ['embed', 'shorts']) === true &&
$isYoutubeId($second) === true
) {
// embedded and shorts video URLs
$src = $host . '/' . $second;
}
}

View File

@@ -16,6 +16,10 @@ use Kirby\Exception\InvalidArgumentException;
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @deprecated 3.8.2 Use `Kirby\Query\Query` instead
* // TODO: throw warnings in 3.9.0
* // TODO: Remove in 3.10.0
*/
class Query
{

View File

@@ -7,6 +7,8 @@ use DateTime;
use Exception;
use IntlDateFormatter;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Query\Query;
use Throwable;
/**
* The String class provides a set
@@ -665,7 +667,7 @@ class Str
*/
public static function query(string $query, array $data = [])
{
return (new Query($query, $data))->result();
return Query::factory($query)->resolve($data);
}
/**
@@ -1181,40 +1183,37 @@ class Str
// make sure $string is string
$string ??= '';
return preg_replace_callback('!' . $start . '(.*?)' . $end . '!', function ($match) use ($data, $fallback, $callback) {
$query = trim($match[1]);
return preg_replace_callback(
'!' . $start . '(.*?)' . $end . '!',
function ($match) use ($data, $fallback, $callback) {
$query = trim($match[1]);
// if the placeholder contains a dot, it is a query
if (strpos($query, '.') !== false) {
try {
$result = (new Query($match[1], $data))->result();
} catch (Exception) {
$result = Query::factory($query)->resolve($data);
} catch (Throwable) {
$result = null;
}
} else {
$result = $data[$query] ?? null;
}
// if we don't have a result, use the fallback if given
if ($result === null && $fallback !== null) {
$result = $fallback;
}
// if we don't have a result, use the fallback if given
$result ??= $fallback;
// callback on result if given
if ($callback !== null) {
$callbackResult = $callback((string)$result, $query, $data);
// callback on result if given
if ($callback !== null) {
$callbackResult = $callback((string)$result, $query, $data);
if ($result === null && $callbackResult === '') {
// the empty string came just from string casting,
// keep the null value and ignore the callback result
} else {
$result = $callbackResult;
if ($result === null && $callbackResult === '') {
// the empty string came just from string casting,
// keep the null value and ignore the callback result
} else {
$result = $callbackResult;
}
}
}
// if we still don't have a result, keep the original placeholder
return $result ?? $match[0];
}, $string);
// if we still don't have a result, keep the original placeholder
return $result ?? $match[0];
},
$string
);
}
/**

View File

@@ -38,7 +38,7 @@ class UserUuid extends Uuid
/**
* Returns the user object
*/
public function model(bool $lazy = false): User
public function model(bool $lazy = false): User|null
{
return $this->model ??= App::instance()->user($this->id());
}