Download the PHP package fruit/routekit without Composer
On this page you can find all versions of the php package fruit/routekit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package routekit
RouteKit
This package is part of Fruit Framework, requires PHP 7.
RouteKit is a fast router implementation. It stores your routing rules in a tree structure, and you can make it even faster by generating a router class with builtin class generator.
Synopsis
Why fast
RouteKit gains performance in two ways: better data structure for rule-matching, and ability to convert dynamic call to static call.
Matching process
RouteKit store routing rules in tree structure, so the matching speed will not be affected by how many rules you have. In other words, the matching process has constant time complexity.
More further, in generated router, we use a Finite State Machine to do the matching process, eliminates all possible function calls. Function calls are much slower comparing to opcode actions (if
, switch
, assignments, arithmatic operations, etc.) and hashtable manuplating. In practice, the FSM approach can run more than 20x faster comparing to array implementations.
Static call
Most of router implementations splits the dispatching work into two pieces: matching with rules to grab correct handler, and execute it with cufa; And cufa is notorious for its great performance.
By generating custom router, we generate codes according to the information you provided in routing rules. No more cufa, no more reflection, so no more performance penalty.
Since codes are mostly generated using var_export
, some cases are not supported:
- Classes cannot be restored using
__set_state
. - Closures and anonymouse functions.
- Not accessible methods. (Methods not
public
) - Special variables like resources.
Generated class should be thread-safe, since all properties and methods are static.
Type converting
You can add type hintings to your handler, RouteKit will automatically do the type checking and converting for you. Currently we support only int
, float
, bool
and string
. We will not check/convert the parameters without type-hinted.
In non-compiled version, this is done by ReflectionParameter::getType()
, so it would cost some performance for fetching reflection data.
In compiled version, we grab the type info at compile time, so there is no performance penalty excepts the type checking and converting works.
Injector and filter
Injectors
Injector is mean to solve "global variable" problem: you can inject global data into your handler object via the interface you defined.
Input filters
Input filter is mean to filter the control flow. Permission validating layer should be implemented here.
An input filter accepts exactly 4 parameters:
- HTTP method
- URL
- A callable represents the handler
- Array of parameters going to be passed to handler.
Returning anything other than NULL
causes dispatching process to be interrupted, and the result of filtering is returned immediately.
While PHP syntax allowing you to modify the 4 params, you SHOULD NOT modify them. That's because input filter is not mean to filtering input data.
Output filters
Output filter is mean to filter the result. If you want to refine the result or HTTP headers, you're at the right place.
An output filter accepts only 1 prameter, which is the returned data from handler: you modify it, send or delete some headers, and return the result back. The most common example is adding CORS headers or forge real output by templating system.
Dispatching flow
- Searching routing tree for currect handler and gather url parameters.
- Executing input filters, decide whether to execute further.
- If any input filter blocks execution, return immediately.
- Modifying handler's internal state using injector.
- Executes handler.
- Manipulate result by executing output filters.
License
Any version of MIT, GPL or LGPL.