|
|
@ -16,90 +16,76 @@ require_once __DIR__ . '/autoload.php';
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The Flight class is a static representation of the framework.
|
|
|
|
* The Flight class is a static representation of the framework.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @license MIT, http://flightphp.com/license
|
|
|
|
* @license MIT, https://docs.flightphp.com/license
|
|
|
|
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
|
|
|
* @copyright Copyright (c) 2011-2025, Mike Cao <mike@mikecao.com>, n0nag0n <n0nag0n@sky-9.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @template T of object
|
|
|
|
* @method static void start()
|
|
|
|
*
|
|
|
|
* @method static void path(string $dir)
|
|
|
|
* # Core methods
|
|
|
|
* @method static void stop(int $code = null)
|
|
|
|
* @method static void start() Starts the framework.
|
|
|
|
|
|
|
|
* @method static void path(string $dir) Adds a path for autoloading classes.
|
|
|
|
|
|
|
|
* @method static void stop(?int $code = null) Stops the framework and sends a response.
|
|
|
|
|
|
|
|
* @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
|
|
|
|
* @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
|
|
|
|
* Stop the framework with an optional status code and message.
|
|
|
|
* @method static void register(string $name, string $class, array $params = [], callable $callback = null)
|
|
|
|
* @method static void register(string $name, string $class, array<int, mixed> $params = [], ?callable $callback = null)
|
|
|
|
|
|
|
|
* Registers a class to a framework method.
|
|
|
|
|
|
|
|
* @method static void unregister(string $methodName)
|
|
|
|
* @method static void unregister(string $methodName)
|
|
|
|
* Unregisters a class to a framework method.
|
|
|
|
* @method static void registerContainerHandler($containerHandler)
|
|
|
|
* @method static void registerContainerHandler(ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler) Registers a container handler.
|
|
|
|
* @method static EventDispatcher eventDispatcher()
|
|
|
|
*
|
|
|
|
* @method static Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* # Class registration
|
|
|
|
* @method static void group(string $pattern, callable $callback, array $group_middlewares = [])
|
|
|
|
* @method EventDispatcher eventDispatcher() Gets event dispatcher
|
|
|
|
* @method static Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
*
|
|
|
|
* @method static Route put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* # Class registration
|
|
|
|
* @method static Route patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method EventDispatcher eventDispatcher() Gets event dispatcher
|
|
|
|
* @method static Route delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
*
|
|
|
|
* @method static void resource(string $pattern, string $controllerClass, array $methods = [])
|
|
|
|
* # Routing
|
|
|
|
* @method static Router router()
|
|
|
|
* @method static Route route(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method static string getUrl(string $alias, array $params = [])
|
|
|
|
* Maps a URL pattern to a callback with all applicable methods.
|
|
|
|
* @method static void map(string $name, callable $callback)
|
|
|
|
* @method static void group(string $pattern, callable $callback, (class-string|callable|array{0: class-string, 1: string})[] $group_middlewares = [])
|
|
|
|
* @method static void before(string $name, Closure $callback)
|
|
|
|
* Groups a set of routes together under a common prefix.
|
|
|
|
* @method static void after(string $name, Closure $callback)
|
|
|
|
* @method static Route post(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method static void set($key, $value)
|
|
|
|
* Routes a POST URL to a callback function.
|
|
|
|
* @method static mixed get($key = null)
|
|
|
|
* @method static Route put(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method static bool has(string $key)
|
|
|
|
* Routes a PUT URL to a callback function.
|
|
|
|
* @method static void clear($key = null)
|
|
|
|
* @method static Route patch(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method static void render(string $file, array $data = null, string $key = null)
|
|
|
|
* Routes a PATCH URL to a callback function.
|
|
|
|
* @method static View view()
|
|
|
|
* @method static Route delete(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method void onEvent(string $event, callable $callback)
|
|
|
|
* Routes a DELETE URL to a callback function.
|
|
|
|
* @method void triggerEvent(string $event, ...$args)
|
|
|
|
* @method static void resource(string $pattern, class-string $controllerClass, array<string, string|array<string>> $methods = [])
|
|
|
|
* @method static Request request()
|
|
|
|
* Adds standardized RESTful routes for a controller.
|
|
|
|
* @method static Response response()
|
|
|
|
* @method static Router router() Returns Router instance.
|
|
|
|
* @method static void redirect(string $url, int $code = 303)
|
|
|
|
* @method static string getUrl(string $alias, array<string, mixed> $params = []) Gets a url from an alias
|
|
|
|
* @method static void json($data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
|
|
|
|
* @method static void map(string $name, callable $callback) Creates a custom framework method.
|
|
|
|
* @method static void jsonHalt($data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
|
|
|
|
*
|
|
|
|
* @method static void jsonp($data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
|
|
|
|
* # Filters
|
|
|
|
* @method static void error(Throwable $exception)
|
|
|
|
* @method static void before(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
|
|
|
|
* @method static void notFound()
|
|
|
|
* Adds a filter before a framework method.
|
|
|
|
* @method static void etag(string $id, string $type = 'strong')
|
|
|
|
* @method static void after(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
|
|
|
|
* @method static void lastModified(int $time)
|
|
|
|
* Adds a filter after a framework method.
|
|
|
|
* @method static void download(string $filePath)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* # Variables
|
|
|
|
* @phpstan-template FlightTemplate of object
|
|
|
|
* @method static void set(string|iterable<string, mixed> $key, mixed $value) Sets a variable.
|
|
|
|
* @phpstan-method static void register(string $name, class-string<FlightTemplate> $class, array<int|string, mixed> $params = [], (callable(class-string<FlightTemplate> $class, array<int|string, mixed> $params): void)|null $callback = null)
|
|
|
|
* @method static mixed get(?string $key) Gets a variable.
|
|
|
|
* @phpstan-method static void registerContainerHandler(ContainerInterface|callable(class-string<FlightTemplate> $id, array<int|string, mixed> $params): ?FlightTemplate $containerHandler)
|
|
|
|
* @method static bool has(string $key) Checks if a variable is set.
|
|
|
|
* @phpstan-method static Route route(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
* @method static void clear(?string $key = null) Clears a variable.
|
|
|
|
* @phpstan-method static void group(string $pattern, callable $callback, (class-string|callable|array{0: class-string, 1: string})[] $group_middlewares = [])
|
|
|
|
|
|
|
|
* @phpstan-method static Route post(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
|
|
|
|
* @phpstan-method static Route put(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
|
|
|
|
* @phpstan-method static Route patch(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
|
|
|
|
* @phpstan-method static Route delete(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
|
|
|
|
|
|
|
|
* @phpstan-method static void resource(string $pattern, class-string $controllerClass, array<string, string|array<string>> $methods = [])
|
|
|
|
|
|
|
|
* @phpstan-method static string getUrl(string $alias, array<string, mixed> $params = [])
|
|
|
|
|
|
|
|
* @phpstan-method static void before(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
|
|
|
|
|
|
|
|
* @phpstan-method static void after(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
|
|
|
|
|
|
|
|
* @phpstan-method static void set(string|iterable<string, mixed> $key, mixed $value)
|
|
|
|
|
|
|
|
* @phpstan-method static mixed get(?string $key)
|
|
|
|
|
|
|
|
* @phpstan-method static void render(string $file, ?array<string, mixed> $data = null, ?string $key = null)
|
|
|
|
|
|
|
|
* @phpstan-method static void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
|
|
|
|
|
|
|
|
* @phpstan-method static void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
|
|
|
|
|
|
|
|
* @phpstan-method static void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* # Views
|
|
|
|
* Note: IDEs will use standard @method tags for autocompletion, while PHPStan will use @phpstan-* tags for advanced type checking.
|
|
|
|
* @method static void render(string $file, ?array<string, mixed> $data = null, ?string $key = null)
|
|
|
|
|
|
|
|
* Renders a template file.
|
|
|
|
|
|
|
|
* @method static View view() Returns View instance.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* # Events
|
|
|
|
|
|
|
|
* @method void onEvent(string $event, callable $callback) Registers a callback for an event.
|
|
|
|
|
|
|
|
* @method void triggerEvent(string $event, ...$args) Triggers an event.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* # Request-Response
|
|
|
|
|
|
|
|
* @method static Request request() Returns Request instance.
|
|
|
|
|
|
|
|
* @method static Response response() Returns Response instance.
|
|
|
|
|
|
|
|
* @method static void redirect(string $url, int $code = 303) Redirects to another URL.
|
|
|
|
|
|
|
|
* @method static void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
|
|
|
|
|
|
|
|
* Sends a JSON response.
|
|
|
|
|
|
|
|
* @method static void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
|
|
|
|
|
|
|
|
* Sends a JSON response and immediately halts the request.
|
|
|
|
|
|
|
|
* @method static void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
|
|
|
|
|
|
|
|
* Sends a JSONP response.
|
|
|
|
|
|
|
|
* @method static void error(Throwable $exception) Sends an HTTP 500 response.
|
|
|
|
|
|
|
|
* @method static void notFound() Sends an HTTP 404 response.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* # HTTP methods
|
|
|
|
|
|
|
|
* @method static void etag(string $id, ('strong'|'weak') $type = 'strong') Performs ETag HTTP caching.
|
|
|
|
|
|
|
|
* @method static void lastModified(int $time) Performs last modified HTTP caching.
|
|
|
|
|
|
|
|
* @method static void download(string $filePath) Downloads a file
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class Flight
|
|
|
|
class Flight
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/** Framework engine. */
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @var Engine<FlightTemplate>
|
|
|
|
|
|
|
|
*/
|
|
|
|
private static Engine $engine;
|
|
|
|
private static Engine $engine;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -138,7 +124,7 @@ class Flight
|
|
|
|
return self::app()->{$name}(...$params);
|
|
|
|
return self::app()->{$name}(...$params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return Engine Application instance */
|
|
|
|
/** @return Engine<FlightTemplate> Application instance */
|
|
|
|
public static function app(): Engine
|
|
|
|
public static function app(): Engine
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return self::$engine ?? self::$engine = new Engine();
|
|
|
|
return self::$engine ?? self::$engine = new Engine();
|
|
|
@ -147,7 +133,7 @@ class Flight
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set the engine instance
|
|
|
|
* Set the engine instance
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param Engine $engine Vroom vroom!
|
|
|
|
* @param Engine<FlightTemplate> $engine Vroom vroom!
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static function setEngine(Engine $engine): void
|
|
|
|
public static function setEngine(Engine $engine): void
|
|
|
|
{
|
|
|
|
{
|
|
|
|