Upgrade to 3.6.6

This commit is contained in:
Bastian Allgeier
2022-05-05 13:58:33 +02:00
parent 5c64df9e2b
commit d515908e2e
12 changed files with 190 additions and 24 deletions

View File

@@ -289,13 +289,16 @@ class Blueprint
$file = $kirby->extension('blueprints', $name);
}
// callback option can be return array or blueprint file path
if (is_callable($file) === true) {
$file = $file($kirby);
}
// now ensure that we always return the data array
if (is_string($file) === true && F::exists($file) === true) {
return static::$loaded[$name] = Data::read($file);
} elseif (is_array($file) === true) {
return static::$loaded[$name] = $file;
} elseif (is_callable($file) === true) {
return static::$loaded[$name] = $file($kirby);
}
// neither a valid file nor array data

View File

@@ -97,14 +97,20 @@ class Roles extends Collection
*/
public static function load(string $root = null, array $inject = [])
{
$kirby = App::instance();
$roles = new static();
// load roles from plugins
foreach (App::instance()->extensions('blueprints') as $blueprintName => $blueprint) {
foreach ($kirby->extensions('blueprints') as $blueprintName => $blueprint) {
if (substr($blueprintName, 0, 6) !== 'users/') {
continue;
}
// callback option can be return array or blueprint file path
if (is_callable($blueprint) === true) {
$blueprint = $blueprint($kirby);
}
if (is_array($blueprint) === true) {
$role = Role::factory($blueprint, $inject);
} else {

View File

@@ -130,7 +130,7 @@ class Uri
* Creates a new URI object
*
* @param array|string $props
* @param array $inject
* @param array $inject Additional props to inject if a URL string is passed
*/
public function __construct($props = [], array $inject = [])
{
@@ -144,10 +144,7 @@ class Uri
// parse the path and extract params
if (empty($props['path']) === false) {
$extract = Params::extract($props['path']);
$props['params'] ??= $extract['params'];
$props['path'] = $extract['path'];
$props['slash'] ??= $extract['slash'];
$props = static::parsePath($props);
}
$this->setProperties($this->props = $props);
@@ -372,11 +369,17 @@ class Uri
}
/**
* @param \Kirby\Http\Params|string|array|null $params
* @param \Kirby\Http\Params|string|array|false|null $params
* @return $this
*/
public function setParams($params = null)
{
// ensure that the special constructor value of `false`
// is never passed through as it's not supported by `Params`
if ($params === false) {
$params = [];
}
$this->params = is_a($params, 'Kirby\Http\Params') === true ? $params : new Params($params);
return $this;
}
@@ -539,4 +542,33 @@ class Uri
}
return $this;
}
/**
* Parses the path inside the props and extracts
* the params unless disabled
*
* @param array $props
* @return array Modified props array
*/
protected static function parsePath(array $props): array
{
// extract params, the rest is the path;
// only do this if not explicitly disabled (set to `false`)
if (isset($props['params']) === false || $props['params'] !== false) {
$extract = Params::extract($props['path']);
$props['params'] ??= $extract['params'];
$props['path'] = $extract['path'];
$props['slash'] ??= $extract['slash'];
return $props;
}
// use the full path;
// automatically detect the trailing slash from it if possible
if (is_string($props['path']) === true) {
$props['slash'] = substr($props['path'], -1, 1) === '/';
}
return $props;
}
}

View File

@@ -335,6 +335,29 @@ class A
return array_pop($array);
}
/**
* Returns a number of random elements from an array,
* either in original or shuffled order
*
* @param array $array
* @param int $count
* @param bool $shuffle
* @return array
*/
public static function random(array $array, int $count = 1, bool $shuffle = false): array
{
if ($shuffle) {
return array_slice(self::shuffle($array), 0, $count);
}
if ($count === 1) {
$key = array_rand($array);
return [$key => $array[$key]];
}
return self::get($array, array_rand($array, $count));
}
/**
* Fills an array up with additional elements to certain amount.
*

View File

@@ -870,6 +870,25 @@ class Collection extends Iterator implements Countable
return $result;
}
/**
* Returns a new collection consisting of random elements,
* from the original collection, shuffled or ordered
*
* @param int $count
* @param bool $shuffle
* @return static
*/
public function random(int $count = 1, bool $shuffle = false)
{
if ($shuffle) {
return $this->shuffle()->slice(0, $count);
}
$collection = clone $this;
$collection->data = A::random($collection->data, $count);
return $collection;
}
/**
* Removes an element from the array by key
*

View File

@@ -2,6 +2,7 @@
namespace Kirby\Toolkit;
use Kirby\Exception\InvalidArgumentException;
use stdClass;
/**
@@ -62,14 +63,31 @@ class Obj extends stdClass
}
/**
* Property Getter
* Gets one or multiple properties of the object
*
* @param string $property
* @param mixed $fallback
* @param string|array $property
* @param mixed $fallback If multiple properties are requested:
* Associative array of fallback values per key
* @return mixed
*/
public function get(string $property, $fallback = null)
public function get($property, $fallback = null)
{
if (is_array($property)) {
if ($fallback === null) {
$fallback = [];
}
if (!is_array($fallback)) {
throw new InvalidArgumentException('The fallback value must be an array when getting multiple properties');
}
$result = [];
foreach ($property as $key) {
$result[$key] = $this->$key ?? $fallback[$key] ?? null;
}
return $result;
}
return $this->$property ?? $fallback;
}