updated core to 1.7.15

This commit is contained in:
2021-05-27 18:17:50 +02:00
parent dc1fdf21c9
commit 19ecb285ab
552 changed files with 80743 additions and 16675 deletions

View File

@@ -1,14 +1,17 @@
<?php
/**
* @package Grav\Framework\Route
*
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
* @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Route;
use Grav\Framework\Uri\UriFactory;
use InvalidArgumentException;
use function array_slice;
/**
* Implements Grav Route.
@@ -19,16 +22,14 @@ class Route
{
/** @var string */
private $root = '';
/** @var string */
private $language = '';
/** @var string */
private $route = '';
/** @var string */
private $extension = '';
/** @var array */
private $gravParams = [];
/** @var array */
private $queryParams = [];
@@ -36,7 +37,7 @@ class Route
* You can use `RouteFactory` functions to create new `Route` objects.
*
* @param array $parts
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct(array $parts = [])
{
@@ -49,12 +50,13 @@ class Route
public function getParts()
{
return [
'path' => $this->getUriPath(),
'path' => $this->getUriPath(true),
'query' => $this->getUriQuery(),
'grav' => [
'root' => $this->root,
'language' => $this->language,
'route' => $this->route,
'extension' => $this->extension,
'grav_params' => $this->gravParams,
'query_params' => $this->queryParams,
],
@@ -69,6 +71,14 @@ class Route
return $this->root;
}
/**
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* @return string
*/
@@ -77,6 +87,25 @@ class Route
return $this->language !== '' ? '/' . $this->language : '';
}
/**
* @param string|null $language
* @return string
*/
public function getBase(string $language = null): string
{
$parts = [$this->root];
if (null === $language) {
$language = $this->language;
}
if ($language !== '') {
$parts[] = $language;
}
return implode('/', $parts);
}
/**
* @param int $offset
* @param int|null $length
@@ -91,6 +120,14 @@ class Route
return '/' . $this->route;
}
/**
* @return string
*/
public function getExtension()
{
return $this->extension;
}
/**
* @param int $offset
* @param int|null $length
@@ -141,16 +178,11 @@ class Route
* If the parameter exists in both, return Grav parameter.
*
* @param string $param
* @return string|null
* @return string|array|null
*/
public function getParam($param)
{
$value = $this->getGravParam($param);
if ($value === null) {
$value = $this->getQueryParam($param);
}
return $value;
return $this->getGravParam($param) ?? $this->getQueryParam($param);
}
/**
@@ -159,16 +191,80 @@ class Route
*/
public function getGravParam($param)
{
return isset($this->gravParams[$param]) ? $this->gravParams[$param] : null;
return $this->gravParams[$param] ?? null;
}
/**
* @param string $param
* @return string|null
* @return string|array|null
*/
public function getQueryParam($param)
{
return isset($this->queryParams[$param]) ? $this->queryParams[$param] : null;
return $this->queryParams[$param] ?? null;
}
/**
* Allow the ability to set the route to something else
*
* @param string $route
* @return Route
*/
public function withRoute($route)
{
$new = $this->copy();
$new->route = $route;
return $new;
}
/**
* Allow the ability to set the root to something else
*
* @param string $root
* @return Route
*/
public function withRoot($root)
{
$new = $this->copy();
$new->root = $root;
return $new;
}
/**
* @param string|null $language
* @return Route
*/
public function withLanguage($language)
{
$new = $this->copy();
$new->language = $language ?? '';
return $new;
}
/**
* @param string $path
* @return Route
*/
public function withAddedPath($path)
{
$new = $this->copy();
$new->route .= '/' . ltrim($path, '/');
return $new;
}
/**
* @param string $extension
* @return Route
*/
public function withExtension($extension)
{
$new = $this->copy();
$new->extension = $extension;
return $new;
}
/**
@@ -191,6 +287,36 @@ class Route
return $this->withParam('queryParams', $param, $value);
}
/**
* @return Route
*/
public function withoutParams()
{
return $this->withoutGravParams()->withoutQueryParams();
}
/**
* @return Route
*/
public function withoutGravParams()
{
$new = $this->copy();
$new->gravParams = [];
return $new;
}
/**
* @return Route
*/
public function withoutQueryParams()
{
$new = $this->copy();
$new->queryParams = [];
return $new;
}
/**
* @return \Grav\Framework\Uri\Uri
*/
@@ -200,57 +326,80 @@ class Route
}
/**
* @param bool $includeRoot
* @return string
*/
public function __toString()
public function toString(bool $includeRoot = false)
{
$url = $this->getUriPath();
$url = $this->getUriPath($includeRoot);
if ($this->queryParams) {
$url .= '?' . $this->getUriQuery();
}
return $url;
return rtrim($url,'/');
}
/**
* @return string
* @deprecated 1.6 Use ->toString(true) or ->getUri() instead.
*/
public function __toString()
{
user_error(__CLASS__ . '::' . __FUNCTION__ . '() will change in the future to return route, not relative url: use ->toString(true) or ->getUri() instead.', E_USER_DEPRECATED);
return $this->toString(true);
}
/**
* @param string $type
* @param string $param
* @param mixed $value
* @return static
* @return Route
*/
protected function withParam($type, $param, $value)
{
$oldValue = isset($this->{$type}[$param]) ? $this->{$type}[$param] : null;
$values = $this->{$type} ?? [];
$oldValue = $values[$param] ?? null;
if ($oldValue === $value) {
return $this;
}
$new = clone $this;
$new = $this->copy();
if ($value === null) {
unset($new->{$type}[$param]);
unset($values[$param]);
} else {
$new->{$type}[$param] = $value;
$values[$param] = $value;
}
$new->{$type} = $values;
return $new;
}
/**
* @return Route
*/
protected function copy()
{
return clone $this;
}
/**
* @param bool $includeRoot
* @return string
*/
protected function getUriPath()
protected function getUriPath($includeRoot = false)
{
$parts = [$this->root];
$parts = $includeRoot ? [$this->root] : [''];
if ($this->language !== '') {
$parts[] = $this->language;
}
if ($this->route !== '') {
$parts[] = $this->route;
}
$parts[] = $this->extension ? $this->route . '.' . $this->extension : $this->route;
if ($this->gravParams) {
$parts[] = RouteFactory::buildParams($this->gravParams);
@@ -269,6 +418,7 @@ class Route
/**
* @param array $parts
* @return void
*/
protected function initParts(array $parts)
{
@@ -277,14 +427,14 @@ class Route
$this->root = $gravParts['root'];
$this->language = $gravParts['language'];
$this->route = $gravParts['route'];
$this->gravParams = $gravParts['params'];
$this->queryParams = $parts['query_params'];
$this->extension = $gravParts['extension'] ?? '';
$this->gravParams = $gravParts['params'] ?? [];
$this->queryParams = $parts['query_params'] ?? [];
} else {
$this->root = RouteFactory::getRoot();
$this->language = RouteFactory::getLanguage();
$path = isset($parts['path']) ? $parts['path'] : '/';
$path = $parts['path'] ?? '/';
if (isset($parts['params'])) {
$this->route = trim(rawurldecode($path), '/');
$this->gravParams = $parts['params'];

View File

@@ -1,13 +1,18 @@
<?php
/**
* @package Grav\Framework\Route
*
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
* @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Route;
use Grav\Common\Uri;
use function dirname;
use function strlen;
/**
* Class RouteFactory
* @package Grav\Framework\Route
@@ -16,21 +21,52 @@ class RouteFactory
{
/** @var string */
private static $root = '';
/** @var string */
private static $language = '';
/** @var string */
private static $delimiter = ':';
public static function createFromParts($parts)
/**
* @param array $parts
* @return Route
*/
public static function createFromParts(array $parts): Route
{
return new Route($parts);
}
public static function createFromString($path)
/**
* @param Uri $uri
* @return Route
*/
public static function createFromLegacyUri(Uri $uri): Route
{
$parts = $uri->toArray();
$parts += [
'grav' => []
];
$path = $parts['path'] ?? '';
$parts['grav'] += [
'root' => self::$root,
'language' => self::$language,
'route' => trim($path, '/'),
'params' => $parts['params'] ?? [],
];
return static::createFromParts($parts);
}
/**
* @param string $path
* @return Route
*/
public static function createFromString(string $path): Route
{
$path = ltrim($path, '/');
if (self::$language && mb_strpos($path, self::$language) === 0) {
$path = ltrim(mb_substr($path, mb_strlen(self::$language)), '/');
}
$parts = [
'path' => $path,
'query' => '',
@@ -38,48 +74,67 @@ class RouteFactory
'grav' => [
'root' => self::$root,
'language' => self::$language,
'route' => $path,
'params' => ''
'route' => static::trimParams($path),
'params' => static::getParams($path)
],
];
return new Route($parts);
}
public static function getRoot()
/**
* @return string
*/
public static function getRoot(): string
{
return self::$root;
}
public static function setRoot($root)
/**
* @param string $root
*/
public static function setRoot($root): void
{
self::$root = rtrim($root, '/');
}
public static function getLanguage()
/**
* @return string
*/
public static function getLanguage(): string
{
return self::$language;
}
public static function setLanguage($language)
/**
* @param string $language
*/
public static function setLanguage(string $language): void
{
self::$language = trim($language, '/');
}
public static function getParamValueDelimiter()
/**
* @return string
*/
public static function getParamValueDelimiter(): string
{
return self::$delimiter;
}
public static function setParamValueDelimiter($delimiter)
/**
* @param string $delimiter
*/
public static function setParamValueDelimiter(string $delimiter): void
{
self::$delimiter = $delimiter;
self::$delimiter = $delimiter ?: ':';
}
/**
* @param array $params
* @return string
*/
public static function buildParams(array $params)
public static function buildParams(array $params): string
{
if (!$params) {
return '';
@@ -100,7 +155,7 @@ class RouteFactory
* @param bool $decode
* @return string
*/
public static function stripParams($path, $decode = false)
public static function stripParams(string $path, bool $decode = false): string
{
$pos = strpos($path, self::$delimiter);
@@ -120,7 +175,7 @@ class RouteFactory
* @param string $path
* @return array
*/
public static function getParams($path)
public static function getParams(string $path): array
{
$params = ltrim(substr($path, strlen(static::stripParams($path))), '/');
@@ -129,20 +184,53 @@ class RouteFactory
/**
* @param string $str
* @return array
* @return string
*/
public static function parseParams($str)
public static function trimParams(string $str): string
{
if ($str === '') {
return $str;
}
$delimiter = self::$delimiter;
/** @var array $params */
$params = explode('/', $str);
foreach ($params as &$param) {
$parts = explode($delimiter, $param, 2);
if (isset($parts[1])) {
$param[rawurldecode($parts[0])] = rawurldecode($parts[1]);
$list = [];
foreach ($params as $param) {
if (mb_strpos($param, $delimiter) === false) {
$list[] = $param;
}
}
return $params;
return implode('/', $list);
}
/**
* @param string $str
* @return array
*/
public static function parseParams(string $str): array
{
if ($str === '') {
return [];
}
$delimiter = self::$delimiter;
/** @var array $params */
$params = explode('/', $str);
$list = [];
foreach ($params as &$param) {
/** @var array $parts */
$parts = explode($delimiter, $param, 2);
if (isset($parts[1])) {
$var = rawurldecode($parts[0]);
$val = rawurldecode($parts[1]);
$list[$var] = $val;
}
}
return $list;
}
}