|
|
|
@ -32,7 +32,7 @@ use flight\net\Route;
|
|
|
|
|
* # Routing
|
|
|
|
|
* @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
|
* Routes a URL to a callback function with all applicable methods
|
|
|
|
|
* @method void group(string $pattern, callable $callback, array $group_middlewares = [])
|
|
|
|
|
* @method void group(string $pattern, callable $callback, array<int, callable|object> $group_middlewares = [])
|
|
|
|
|
* Groups a set of routes together under a common prefix.
|
|
|
|
|
* @method Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
|
* Routes a POST URL to a callback function.
|
|
|
|
@ -46,7 +46,7 @@ use flight\net\Route;
|
|
|
|
|
* @method string getUrl(string $alias) Gets a url from an alias
|
|
|
|
|
*
|
|
|
|
|
* # Views
|
|
|
|
|
* @method void render(string $file, array $data = null, string $key = null) Renders template
|
|
|
|
|
* @method void render(string $file, ?array $data = null, ?string $key = null) Renders template
|
|
|
|
|
* @method View view() Gets current view
|
|
|
|
|
*
|
|
|
|
|
* # Request-Response
|
|
|
|
@ -57,59 +57,41 @@ use flight\net\Route;
|
|
|
|
|
* @method void redirect(string $url, int $code = 303) Redirects the current request to another URL.
|
|
|
|
|
* @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
|
|
|
|
|
* Sends a JSON response.
|
|
|
|
|
* @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) Sends a JSONP response.
|
|
|
|
|
* @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
|
|
|
|
|
* Sends a JSONP response.
|
|
|
|
|
*
|
|
|
|
|
* # HTTP caching
|
|
|
|
|
* @method void etag($id, string $type = 'strong') Handles ETag HTTP caching.
|
|
|
|
|
* @method void etag(string $id, ('strong'|'weak') $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
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Stored variables.
|
|
|
|
|
* @var array<string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
protected array $vars;
|
|
|
|
|
/** @var array<string, mixed> Stored variables. */
|
|
|
|
|
protected array $vars = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class loader.
|
|
|
|
|
*/
|
|
|
|
|
/** Class loader. */
|
|
|
|
|
protected Loader $loader;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event dispatcher.
|
|
|
|
|
*/
|
|
|
|
|
/** Event dispatcher. */
|
|
|
|
|
protected Dispatcher $dispatcher;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the framework has been initialized or not
|
|
|
|
|
*
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
/** If the framework has been initialized or not. */
|
|
|
|
|
protected bool $initialized = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->vars = [];
|
|
|
|
|
|
|
|
|
|
$this->loader = new Loader();
|
|
|
|
|
$this->dispatcher = new Dispatcher();
|
|
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles calls to class methods.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param array<int, mixed> $params Method parameters
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param array<int, mixed> $params Method parameters
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*
|
|
|
|
|
* @return mixed Callback results
|
|
|
|
|
*/
|
|
|
|
|
public function __call(string $name, array $params)
|
|
|
|
@ -121,7 +103,7 @@ class Engine
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->loader->get($name)) {
|
|
|
|
|
throw new Exception("{$name} must be a mapped method.");
|
|
|
|
|
throw new Exception("$name must be a mapped method.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$shared = empty($params) || $params[0];
|
|
|
|
@ -129,11 +111,11 @@ class Engine
|
|
|
|
|
return $this->loader->load($name, $shared);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Core Methods
|
|
|
|
|
//////////////////
|
|
|
|
|
// Core Methods //
|
|
|
|
|
//////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the framework.
|
|
|
|
|
*/
|
|
|
|
|
/** Initializes the framework. */
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
|
|
|
|
$initialized = $this->initialized;
|
|
|
|
@ -149,7 +131,8 @@ class Engine
|
|
|
|
|
$this->loader->register('request', Request::class);
|
|
|
|
|
$this->loader->register('response', Response::class);
|
|
|
|
|
$this->loader->register('router', Router::class);
|
|
|
|
|
$this->loader->register('view', View::class, [], function ($view) use ($self) {
|
|
|
|
|
|
|
|
|
|
$this->loader->register('view', View::class, [], function (View $view) use ($self) {
|
|
|
|
|
$view->path = $self->get('flight.views.path');
|
|
|
|
|
$view->extension = $self->get('flight.views.extension');
|
|
|
|
|
});
|
|
|
|
@ -160,8 +143,9 @@ class Engine
|
|
|
|
|
'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp',
|
|
|
|
|
'post', 'put', 'patch', 'delete', 'group', 'getUrl',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($methods as $name) {
|
|
|
|
|
$this->dispatcher->set($name, [$this, '_' . $name]);
|
|
|
|
|
$this->dispatcher->set($name, [$this, "_$name"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default configuration settings
|
|
|
|
@ -193,15 +177,15 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Custom error handler. Converts errors into exceptions.
|
|
|
|
|
*
|
|
|
|
|
* @param int $errno Error number
|
|
|
|
|
* @param string $errstr Error string
|
|
|
|
|
* @param int $errno Error number
|
|
|
|
|
* @param string $errstr Error string
|
|
|
|
|
* @param string $errfile Error file name
|
|
|
|
|
* @param int $errline Error file line number
|
|
|
|
|
* @param int $errline Error file line number
|
|
|
|
|
*
|
|
|
|
|
* @return false
|
|
|
|
|
* @throws ErrorException
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function handleError(int $errno, string $errstr, string $errfile, int $errline)
|
|
|
|
|
public function handleError(int $errno, string $errstr, string $errfile, int $errline): bool
|
|
|
|
|
{
|
|
|
|
|
if ($errno & error_reporting()) {
|
|
|
|
|
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
|
|
|
|
@ -215,7 +199,7 @@ class Engine
|
|
|
|
|
*
|
|
|
|
|
* @param Throwable $e Thrown exception
|
|
|
|
|
*/
|
|
|
|
|
public function handleException($e): void
|
|
|
|
|
public function handleException(Throwable $e): void
|
|
|
|
|
{
|
|
|
|
|
if ($this->get('flight.log_errors')) {
|
|
|
|
|
error_log($e->getMessage()); // @codeCoverageIgnore
|
|
|
|
@ -227,7 +211,7 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Maps a callback to a framework method.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param string $name Method name
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception If trying to map over a framework method
|
|
|
|
@ -243,13 +227,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 +262,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 +273,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 +284,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 +300,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 +332,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 +474,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 +501,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
|
|
|
|
|
*/
|
|
|
|
@ -528,11 +524,10 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Routes a URL to a callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
* @param string $alias the alias for the route
|
|
|
|
|
* @return Route
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
* @param string $alias The alias for the route
|
|
|
|
|
*/
|
|
|
|
|
public function _route(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route
|
|
|
|
|
{
|
|
|
|
@ -542,9 +537,9 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Routes a URL to a callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function that includes the Router class as first parameter
|
|
|
|
|
* @param array<callable> $group_middlewares The middleware to be applied to the route
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function that includes the Router class as first parameter
|
|
|
|
|
* @param array<int, callable|object> $group_middlewares The middleware to be applied to the route
|
|
|
|
|
*/
|
|
|
|
|
public function _group(string $pattern, callable $callback, array $group_middlewares = []): void
|
|
|
|
|
{
|
|
|
|
@ -554,55 +549,55 @@ class Engine
|
|
|
|
|
/**
|
|
|
|
|
* Routes a URL to a callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Routes a URL to a callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Routes a URL to a callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Routes a URL to a callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @param callable $callback Callback function
|
|
|
|
|
* @param bool $pass_route Pass the matching route object to the callback
|
|
|
|
|
* @param string $pattern URL pattern to match
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stops processing and returns a given response.
|
|
|
|
|
*
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param string $message Response message
|
|
|
|
|
*/
|
|
|
|
|
public function _halt(int $code = 200, string $message = ''): void
|
|
|
|
@ -618,26 +613,22 @@ 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
|
|
|
|
|
{
|
|
|
|
|
$output = '<h1>404 Not Found</h1><h3>The page you have requested could not be found.</h3>';
|
|
|
|
|
|
|
|
|
|
$this->response()
|
|
|
|
|
->clear()
|
|
|
|
|
->status(404)
|
|
|
|
|
->write(
|
|
|
|
|
'<h1>404 Not Found</h1>' .
|
|
|
|
|
'<h3>The page you have requested could not be found.</h3>'
|
|
|
|
|
)
|
|
|
|
|
->write($output)
|
|
|
|
|
->send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 +653,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 +699,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 +717,6 @@ class Engine
|
|
|
|
|
int $option = 0
|
|
|
|
|
): void {
|
|
|
|
|
$json = $encode ? json_encode($data, $option) : $data;
|
|
|
|
|
|
|
|
|
|
$callback = $this->request()->query[$param];
|
|
|
|
|
|
|
|
|
|
$this->response()
|
|
|
|
@ -738,8 +729,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
|
|
|
|
|
{
|
|
|
|
|