|
|
|
@ -63,7 +63,6 @@ use flight\net\Route;
|
|
|
|
|
* @method void etag($id, string $type = 'strong') Handles ETag HTTP caching.
|
|
|
|
|
* @method void lastModified(int $time) Handles last modified HTTP caching.
|
|
|
|
|
*/
|
|
|
|
|
// phpcs:ignoreFile Generic.Files.LineLength.TooLong, PSR2.Methods.MethodDeclaration.Underscore
|
|
|
|
|
class Engine
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
@ -243,13 +242,21 @@ class Engine
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers a class to a framework method.
|
|
|
|
|
* @template T of object
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param class-string<T> $class Class name
|
|
|
|
|
* @param array<int, mixed> $params Class initialization parameters
|
|
|
|
|
* @param ?callable(T $instance): void $callback Function to call after object instantiation
|
|
|
|
|
* # Usage example:
|
|
|
|
|
* ```
|
|
|
|
|
* $app = new Engine;
|
|
|
|
|
* $app->register('user', User::class);
|
|
|
|
|
*
|
|
|
|
|
* $app->user(); # <- Return a User instance
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param class-string<T> $class Class name
|
|
|
|
|
* @param array<int, mixed> $params Class initialization parameters
|
|
|
|
|
* @param ?Closure(T $instance): void $callback Function to call after object instantiation
|
|
|
|
|
*
|
|
|
|
|
* @template T of object
|
|
|
|
|
* @throws Exception If trying to map over a framework method
|
|
|
|
|
*/
|
|
|
|
|
public function register(string $name, string $class, array $params = [], ?callable $callback = null): void
|
|
|
|
@ -270,8 +277,8 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Adds a pre-filter to a method.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param Closure(array<int, mixed> &$params, string &$output): (void|false) $callback
|
|
|
|
|
*/
|
|
|
|
|
public function before(string $name, callable $callback): void
|
|
|
|
|
{
|
|
|
|
@ -281,8 +288,8 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Adds a post-filter to a method.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param Closure(array<int, mixed> &$params, string &$output): (void|false) $callback
|
|
|
|
|
*/
|
|
|
|
|
public function after(string $name, callable $callback): void
|
|
|
|
|
{
|
|
|
|
@ -292,9 +299,9 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Gets a variable.
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $key Key
|
|
|
|
|
* @param ?string $key Variable name
|
|
|
|
|
*
|
|
|
|
|
* @return array|mixed|null
|
|
|
|
|
* @return mixed Variable value or `null` if `$key` doesn't exists.
|
|
|
|
|
*/
|
|
|
|
|
public function get(?string $key = null)
|
|
|
|
|
{
|
|
|
|
@ -308,24 +315,27 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Sets a variable.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $key Key
|
|
|
|
|
* @param mixed|null $value Value
|
|
|
|
|
* @param string|iterable<string, mixed> $key
|
|
|
|
|
* Variable name as `string` or an iterable of `'varName' => $varValue`
|
|
|
|
|
* @param mixed $value Ignored if `$key` is an `iterable`
|
|
|
|
|
*/
|
|
|
|
|
public function set($key, $value = null): void
|
|
|
|
|
{
|
|
|
|
|
if (\is_array($key) || \is_object($key)) {
|
|
|
|
|
if (\is_iterable($key)) {
|
|
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
|
$this->vars[$k] = $v;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->vars[$key] = $value;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->vars[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if a variable has been set.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Key
|
|
|
|
|
* @param string $key Variable name
|
|
|
|
|
*
|
|
|
|
|
* @return bool Variable status
|
|
|
|
|
*/
|
|
|
|
@ -337,15 +347,16 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Unsets a variable. If no key is passed in, clear all variables.
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $key Key
|
|
|
|
|
* @param ?string $key Variable name, if `$key` isn't provided, it clear all variables.
|
|
|
|
|
*/
|
|
|
|
|
public function clear(?string $key = null): void
|
|
|
|
|
{
|
|
|
|
|
if (null === $key) {
|
|
|
|
|
$this->vars = [];
|
|
|
|
|
} else {
|
|
|
|
|
unset($this->vars[$key]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unset($this->vars[$key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -478,7 +489,7 @@ class Engine
|
|
|
|
|
*
|
|
|
|
|
* @param Throwable $e Thrown exception
|
|
|
|
|
*/
|
|
|
|
|
public function _error($e): void
|
|
|
|
|
public function _error(Throwable $e): void
|
|
|
|
|
{
|
|
|
|
|
$msg = sprintf(
|
|
|
|
|
'<h1>500 Internal Server Error</h1>' .
|
|
|
|
@ -505,7 +516,7 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Stops the framework and outputs the current response.
|
|
|
|
|
*
|
|
|
|
|
* @param int|null $code HTTP status code
|
|
|
|
|
* @param ?int $code HTTP status code
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
@ -558,9 +569,9 @@ class Engine
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
*/
|
|
|
|
|
public function _post(string $pattern, callable $callback, bool $pass_route = false): void
|
|
|
|
|
public function _post(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
|
|
|
|
|
{
|
|
|
|
|
$this->router()->map('POST ' . $pattern, $callback, $pass_route);
|
|
|
|
|
$this->router()->map('POST ' . $pattern, $callback, $pass_route, $route_alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -570,9 +581,9 @@ class Engine
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
*/
|
|
|
|
|
public function _put(string $pattern, callable $callback, bool $pass_route = false): void
|
|
|
|
|
public function _put(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
|
|
|
|
|
{
|
|
|
|
|
$this->router()->map('PUT ' . $pattern, $callback, $pass_route);
|
|
|
|
|
$this->router()->map('PUT ' . $pattern, $callback, $pass_route, $route_alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -582,9 +593,9 @@ class Engine
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
*/
|
|
|
|
|
public function _patch(string $pattern, callable $callback, bool $pass_route = false): void
|
|
|
|
|
public function _patch(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
|
|
|
|
|
{
|
|
|
|
|
$this->router()->map('PATCH ' . $pattern, $callback, $pass_route);
|
|
|
|
|
$this->router()->map('PATCH ' . $pattern, $callback, $pass_route, $route_alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -594,9 +605,9 @@ class Engine
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
*/
|
|
|
|
|
public function _delete(string $pattern, callable $callback, bool $pass_route = false): void
|
|
|
|
|
public function _delete(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
|
|
|
|
|
{
|
|
|
|
|
$this->router()->map('DELETE ' . $pattern, $callback, $pass_route);
|
|
|
|
|
$this->router()->map('DELETE ' . $pattern, $callback, $pass_route, $route_alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -618,9 +629,7 @@ class Engine
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends an HTTP 404 response when a URL is not found.
|
|
|
|
|
*/
|
|
|
|
|
/** Sends an HTTP 404 response when a URL is not found. */
|
|
|
|
|
public function _notFound(): void
|
|
|
|
|
{
|
|
|
|
|
$this->response()
|
|
|
|
@ -637,7 +646,7 @@ class Engine
|
|
|
|
|
* Redirects the current request to another URL.
|
|
|
|
|
*
|
|
|
|
|
* @param string $url URL
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
*/
|
|
|
|
|
public function _redirect(string $url, int $code = 303): void
|
|
|
|
|
{
|
|
|
|
@ -662,29 +671,30 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Renders a template.
|
|
|
|
|
*
|
|
|
|
|
* @param string $file Template file
|
|
|
|
|
* @param string $file Template file
|
|
|
|
|
* @param ?array<string, mixed> $data Template data
|
|
|
|
|
* @param string|null $key View variable name
|
|
|
|
|
* @param ?string $key View variable name
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @throws Exception If template file wasn't found
|
|
|
|
|
*/
|
|
|
|
|
public function _render(string $file, ?array $data = null, ?string $key = null): void
|
|
|
|
|
{
|
|
|
|
|
if (null !== $key) {
|
|
|
|
|
$this->view()->set($key, $this->view()->fetch($file, $data));
|
|
|
|
|
} else {
|
|
|
|
|
$this->view()->render($file, $data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->view()->render($file, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a JSON response.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $data JSON data
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param bool $encode Whether to perform JSON encoding
|
|
|
|
|
* @param mixed $data JSON data
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param bool $encode Whether to perform JSON encoding
|
|
|
|
|
* @param string $charset Charset
|
|
|
|
|
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
|
|
|
|
|
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
@ -707,12 +717,12 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Sends a JSONP response.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $data JSON data
|
|
|
|
|
* @param string $param Query parameter that specifies the callback name.
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param bool $encode Whether to perform JSON encoding
|
|
|
|
|
* @param mixed $data JSON data
|
|
|
|
|
* @param string $param Query parameter that specifies the callback name.
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param bool $encode Whether to perform JSON encoding
|
|
|
|
|
* @param string $charset Charset
|
|
|
|
|
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
|
|
|
|
|
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
@ -725,7 +735,6 @@ class Engine
|
|
|
|
|
int $option = 0
|
|
|
|
|
): void {
|
|
|
|
|
$json = $encode ? json_encode($data, $option) : $data;
|
|
|
|
|
|
|
|
|
|
$callback = $this->request()->query[$param];
|
|
|
|
|
|
|
|
|
|
$this->response()
|
|
|
|
@ -738,8 +747,8 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Handles ETag HTTP caching.
|
|
|
|
|
*
|
|
|
|
|
* @param string $id ETag identifier
|
|
|
|
|
* @param string $type ETag type
|
|
|
|
|
* @param string $id ETag identifier
|
|
|
|
|
* @param 'strong'|'weak' $type ETag type
|
|
|
|
|
*/
|
|
|
|
|
public function _etag(string $id, string $type = 'strong'): void
|
|
|
|
|
{
|
|
|
|
|