Upgrade to 3.4.3

This commit is contained in:
Bastian Allgeier
2020-09-15 10:25:09 +02:00
parent 54b9ba3047
commit d8e797dd9b
108 changed files with 1750 additions and 523 deletions

View File

@@ -32,7 +32,7 @@ class Api
/**
* Authentication callback
*
* @var Closure
* @var \Closure
*/
protected $authentication;
@@ -67,14 +67,14 @@ class Api
/**
* The current route
*
* @var Route
* @var \Kirby\Http\Route
*/
protected $route;
/**
* The Router instance
*
* @var Router
* @var \Kirby\Http\Router
*/
protected $router;
@@ -107,6 +107,7 @@ class Api
* @param string $method
* @param array $args
* @return mixed
* @throws \Kirby\Exception\NotFoundException
*/
public function __call(string $method, array $args = [])
{
@@ -141,7 +142,7 @@ class Api
/**
* Returns the authentication callback
*
* @return Closure|null
* @return \Closure|null
*/
public function authentication()
{
@@ -152,10 +153,12 @@ class Api
* Execute an API call for the given path,
* request method and optional request data
*
* @param string $path
* @param string|null $path
* @param string $method
* @param array $requestData
* @return mixed
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function call(string $path = null, string $method = 'GET', array $requestData = [])
{
@@ -209,7 +212,10 @@ class Api
// restore old pagination validation mode
Pagination::$validate = $validate;
if (is_object($output) === true && is_a($output, 'Kirby\\Http\\Response') !== true) {
if (
is_object($output) === true &&
is_a($output, 'Kirby\\Http\\Response') !== true
) {
return $this->resolve($output)->toResponse();
}
@@ -222,8 +228,8 @@ class Api
* @param string $name
* @param array|null $collection
* @return \Kirby\Api\Collection
*
* @throws \Kirby\Exception\NotFoundException If no collection for `$name` exists
* @throws \Exception
*/
public function collection(string $name, $collection = null)
{
@@ -314,7 +320,7 @@ class Api
/**
* Returns an API model instance by name
*
* @param string $name
* @param string|null $name
* @param mixed $object
* @return \Kirby\Api\Model
*
@@ -351,8 +357,8 @@ class Api
* Can either get all the data
* or certain parts of it.
*
* @param string $type
* @param string $key
* @param string|null $type
* @param string|null $key
* @param mixed $default
* @return mixed
*/
@@ -375,7 +381,7 @@ class Api
/**
* Returns the request body if available
*
* @param string $key
* @param string|null $key
* @param mixed $default
* @return mixed
*/
@@ -387,7 +393,7 @@ class Api
/**
* Returns the files from the request if available
*
* @param string $key
* @param string|null $key
* @param mixed $default
* @return mixed
*/
@@ -399,7 +405,7 @@ class Api
/**
* Returns all headers from the request if available
*
* @param string $key
* @param string|null $key
* @param mixed $default
* @return mixed
*/
@@ -421,7 +427,7 @@ class Api
/**
* Returns the request query if available
*
* @param string $key
* @param string|null $key
* @param mixed $default
* @return mixed
*/
@@ -469,7 +475,7 @@ class Api
/**
* Setter for the authentication callback
*
* @param Closure $authentication
* @param \Closure|null $authentication
* @return self
*/
protected function setAuthentication(Closure $authentication = null)
@@ -481,7 +487,7 @@ class Api
/**
* Setter for the collections definition
*
* @param array $collections
* @param array|null $collections
* @return self
*/
protected function setCollections(array $collections = null)
@@ -495,7 +501,7 @@ class Api
/**
* Setter for the injected data
*
* @param array $data
* @param array|null $data
* @return self
*/
protected function setData(array $data = null)
@@ -519,7 +525,7 @@ class Api
/**
* Setter for the model definitions
*
* @param array $models
* @param array|null $models
* @return self
*/
protected function setModels(array $models = null)
@@ -534,7 +540,7 @@ class Api
/**
* Setter for the request data
*
* @param array $requestData
* @param array|null $requestData
* @return self
*/
protected function setRequestData(array $requestData = null)
@@ -552,7 +558,7 @@ class Api
/**
* Setter for the request method
*
* @param string $requestMethod
* @param string|null $requestMethod
* @return self
*/
protected function setRequestMethod(string $requestMethod = null)
@@ -564,7 +570,7 @@ class Api
/**
* Setter for the route definitions
*
* @param array $routes
* @param array|null $routes
* @return self
*/
protected function setRoutes(array $routes = null)
@@ -668,10 +674,10 @@ class Api
* an exception. Kirby exceptions will
* have more information
*
* @param Exception $e
* @param \Throwable $e
* @return array
*/
public function responseForException($e): array
public function responseForException(Throwable $e): array
{
// prepare the result array for all exception types
$result = [
@@ -710,14 +716,17 @@ class Api
/**
* Upload helper method
*
* @param Closure $callback
* move_uploaded_file() not working with unit test
* Added debug parameter for testing purposes as we did in the Email class
*
* @param \Closure $callback
* @param bool $single
* @param bool $debug
* @return array
*
* @throws \Exception If request has no files
* @throws \Exception If there was an error with the upload
* @throws \Exception If request has no files or there was an error with the upload
*/
public function upload(Closure $callback, $single = false): array
public function upload(Closure $callback, $single = false, $debug = false): array
{
$trials = 0;
$uploads = [];
@@ -776,7 +785,7 @@ class Api
// move the file to a location including the extension,
// for better mime detection
if (move_uploaded_file($upload['tmp_name'], $source) === false) {
if ($debug === false && move_uploaded_file($upload['tmp_name'], $source) === false) {
throw new Exception(t('upload.error.cantMove'));
}

View File

@@ -19,12 +19,39 @@ use Kirby\Toolkit\Str;
*/
class Collection
{
/**
* @var \Kirby\Api\Api
*/
protected $api;
/**
* @var mixed|null
*/
protected $data;
/**
* @var mixed|null
*/
protected $model;
/**
* @var mixed|null
*/
protected $select;
/**
* @var mixed|null
*/
protected $view;
/**
* Collection constructor
*
* @param \Kirby\Api\Api $api
* @param mixed|null $data
* @param array $schema
* @throws \Exception
*/
public function __construct(Api $api, $data = null, array $schema)
{
$this->api = $api;
@@ -48,6 +75,11 @@ class Collection
}
}
/**
* @param string|array|null $keys
* @return self
* @throws \Exception
*/
public function select($keys = null)
{
if ($keys === false) {
@@ -66,6 +98,11 @@ class Collection
return $this;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toArray(): array
{
$result = [];
@@ -87,6 +124,11 @@ class Collection
return $result;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toResponse(): array
{
if ($query = $this->api->requestQuery('query')) {
@@ -124,6 +166,10 @@ class Collection
];
}
/**
* @param string $view
* @return self
*/
public function view(string $view)
{
$this->view = $view;

View File

@@ -3,13 +3,12 @@
namespace Kirby\Api;
use Exception;
use Kirby\Toolkit\Str;
/**
* The API Model class can be wrapped around any
* kind of object. Each model defines a set of properties that
* are availabel in REST calls. Those properties are defined as
* are available in REST calls. Those properties are defined as
* simple Closures which are resolved on demand. This is inspired
* by GraphQLs architecture and makes it possible to load
* only the model data that is needed for the current API call.
@@ -22,12 +21,39 @@ use Kirby\Toolkit\Str;
*/
class Model
{
/**
* @var \Kirby\Api\Api
*/
protected $api;
/**
* @var mixed|null
*/
protected $data;
/**
* @var array|mixed
*/
protected $fields;
/**
* @var mixed|null
*/
protected $select;
/**
* @var array|mixed
*/
protected $views;
/**
* Model constructor
*
* @param \Kirby\Api\Api $api
* @param null $data
* @param array $schema
* @throws \Exception
*/
public function __construct(Api $api, $data = null, array $schema)
{
$this->api = $api;
@@ -56,6 +82,11 @@ class Model
}
}
/**
* @param null $keys
* @return self
* @throws \Exception
*/
public function select($keys = null)
{
if ($keys === false) {
@@ -74,6 +105,10 @@ class Model
return $this;
}
/**
* @return array
* @throws \Exception
*/
public function selection(): array
{
$select = $this->select;
@@ -117,6 +152,11 @@ class Model
return $selection;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toArray(): array
{
$select = $this->selection();
@@ -158,6 +198,11 @@ class Model
return $result;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toResponse(): array
{
$model = $this;
@@ -178,6 +223,11 @@ class Model
];
}
/**
* @param string $name
* @return self
* @throws \Exception
*/
public function view(string $name)
{
if ($name === 'any') {