Router
extends RouterBase
in package
implements
RouterInterface
Uses
RoutesSet, UrlParser
Router class
Interfaces, Classes and Traits
- RouterInterface
- Router interface
Table of Contents
- $middleware : array<string, array<string|int, callable>>
- Middleware for routes processing
- $staticRoutes : mixed
- List of static routes for all supported request methods
- $types : array<string, string>
- Supported types of URL parameters
- $bunchSize : int
- Bunch size
- $cachedParameters : array<string, array<string|int, string>>
- Cached parameters for route
- $cachedRegExps : array<string|int, string>
- Cache for regular expressions
- $calledRoute : string
- Called route
- $invalidRouteErrorHandler : callable|null
- Method wich handles invalid route error
- $regExpsWereCompiled : bool
- Were regexps compiled?
- __construct() : mixed
- Constructor
- addType() : void
- Method adds custom type
- callRoute() : mixed
- Processing specified router
- dumpOnDisk() : void
- Method dumps all routes and their names on disk
- fetchActions() : void
- Method fetches actions from the objects and creates GetRoutes for them
- getCallback() : mixed
- Method returns call back by it's router
- getCalledRoute() : string
- Method returns called route
- getNoProcessorErrorHandler() : callable
- Method returns error handler
- loadFromDisk() : void
- Method loads routes from disk
- noProcessorFoundErrorHandler() : void
- Method processes no processor found error
- registerMiddleware() : void
- Method registeres middleware for the router
- setNoProcessorFoundErrorHandler() : callable|null
- Method sets InvalidRouteErrorHandler function
- warmCache() : void
- Method warms cache
- addParamRoute() : void
- Method adds param router
- clearMiddleware() : void
- Method clears middleware
- clearOtherData() : void
- Method clears other data
- executeHandler() : mixed
- Method executes route handler
- getAllRoutesTrace() : string
- Method rturns all available routes
- getDynamicRouteProcessor() : mixed
- Method searches dynamic route processor
- getMiddlewareResult() : array<string|int, mixed>
- Method returns middleware processing result
- getParameters() : array<string|int, mixed>
- Method returns parameters
- getRequestMethod() : string
- Method returns request method
- getStaticRouteProcessor() : mixed
- Method returns route handler
- getStaticRoutes() : array<string|int, mixed>
- Method returns static routes handlers for the specified request methods
- getUniversalRouteProcessor() : mixed
- Method returns route handler
- paramRouteExists() : bool
- Method returns true if the param router exists
- setCalledRoute() : void
- Method sets $calledRoute
- setParameters() : void
- Method sets parameters
- _getParameterNames() : array<string|int, string>
- Method returns all parameter names in the route
- _getRouteMatcherRegExPattern() : string
- Method compiles route pattern string in regex string.
- compileRegexpForBunch() : void
- Method compiles regexp for the bunch of routes
- compileRegexpForBunches() : void
- Method compiles all regeps for all routes
- getRegExpAppendix() : string
- Generating appendix for the RegExp
- initDefaultTypes() : void
- Init types
Properties
$middleware
Middleware for routes processing
protected
array<string, array<string|int, callable>>
$middleware
= []
$staticRoutes
List of static routes for all supported request methods
protected
mixed
$staticRoutes
= ['GET' => [], 'POST' => [], 'PUT' => [], 'DELETE' => [], 'OPTION' => [], 'PATCH' => []]
$types
Supported types of URL parameters
protected
array<string, string>
$types
= []
$bunchSize
Bunch size
private
int
$bunchSize
= 100
$cachedParameters
Cached parameters for route
private
array<string, array<string|int, string>>
$cachedParameters
= []
$cachedRegExps
Cache for regular expressions
private
array<string|int, string>
$cachedRegExps
= []
$calledRoute
Called route
private
string
$calledRoute
= ''
$invalidRouteErrorHandler
Method wich handles invalid route error
private
callable|null
$invalidRouteErrorHandler
= null
$regExpsWereCompiled
Were regexps compiled?
private
bool
$regExpsWereCompiled
= false
Methods
__construct()
Constructor
public
__construct() : mixed
Return values
mixed —addType()
Method adds custom type
public
addType(string $typeName, string $className) : void
Parameters
- $typeName : string
-
type name
- $className : string
-
name of the class wich represents custom type
Return values
void —callRoute()
Processing specified router
public
callRoute(mixed $route) : mixed
Parameters
- $route : mixed
-
route
Tags
Return values
mixed —value returned by route handler
dumpOnDisk()
Method dumps all routes and their names on disk
public
dumpOnDisk([string $filePath = './cache/cache.php' ]) : void
Parameters
- $filePath : string = './cache/cache.php'
-
file path to cache
Tags
Return values
void —fetchActions()
Method fetches actions from the objects and creates GetRoutes for them
public
fetchActions(object $object[, array<string|int, mixed> $map = [] ]) : void
Parameters
- $object : object
-
object to be processed
- $map : array<string|int, mixed> = []
-
map
Return values
void —getCallback()
Method returns call back by it's router
public
getCallback(array<string|int, string>|string $route) : mixed
Parameters
- $route : array<string|int, string>|string
-
route
Tags
Return values
mixed —getCalledRoute()
Method returns called route
public
getCalledRoute() : string
Return values
string —called route
getNoProcessorErrorHandler()
Method returns error handler
public
getNoProcessorErrorHandler() : callable
Return values
callable —loadFromDisk()
Method loads routes from disk
public
loadFromDisk([string $filePath = './cache/cache.php' ]) : void
Parameters
- $filePath : string = './cache/cache.php'
-
file path to cache
Tags
Return values
void —noProcessorFoundErrorHandler()
Method processes no processor found error
public
noProcessorFoundErrorHandler(string $route) : void
Parameters
- $route : string
-
route
Return values
void —registerMiddleware()
Method registeres middleware for the router
public
registerMiddleware(string $router, callable $middleware) : void
Parameters
- $router : string
- $middleware : callable
-
middleware
Return values
void —setNoProcessorFoundErrorHandler()
Method sets InvalidRouteErrorHandler function
public
setNoProcessorFoundErrorHandler(callable $function) : callable|null
Parameters
- $function : callable
-
error handler
Return values
callable|null —old error handler
warmCache()
Method warms cache
public
warmCache() : void
Return values
void —addParamRoute()
Method adds param router
protected
addParamRoute(string $requestMethod, string $route, mixed $callback) : void
Parameters
- $requestMethod : string
-
request method
- $route : string
-
route
- $callback : mixed
Tags
Return values
void —clearMiddleware()
Method clears middleware
protected
clearMiddleware() : void
Return values
void —clearOtherData()
Method clears other data
protected
clearOtherData() : void
Return values
void —executeHandler()
Method executes route handler
protected
abstract executeHandler(mixed $processor, string $route) : mixed
Parameters
- $processor : mixed
- $route : string
-
route
Tags
Return values
mixed —route handler execution result
getAllRoutesTrace()
Method rturns all available routes
protected
abstract getAllRoutesTrace() : string
Tags
Return values
string —trace
getDynamicRouteProcessor()
Method searches dynamic route processor
protected
abstract getDynamicRouteProcessor(string $route[, string $requestMethod = '' ]) : mixed
Parameters
- $route : string
-
route
- $requestMethod : string = ''
-
request method
Return values
mixed —getMiddlewareResult()
Method returns middleware processing result
protected
getMiddlewareResult(string $route) : array<string|int, mixed>
Parameters
- $route : string
-
processed route
Return values
array<string|int, mixed> —middleware result
getParameters()
Method returns parameters
protected
abstract getParameters() : array<string|int, mixed>
Return values
array<string|int, mixed> —getRequestMethod()
Method returns request method
protected
getRequestMethod() : string
Return values
string —request method
getStaticRouteProcessor()
Method returns route handler
protected
getStaticRouteProcessor(string $route) : mixed
Parameters
- $route : string
-
routes
Return values
mixed —getStaticRoutes()
Method returns static routes handlers for the specified request methods
protected
getStaticRoutes(string $requestMethod) : array<string|int, mixed>
Parameters
- $requestMethod : string
-
request method
Return values
array<string|int, mixed> —getUniversalRouteProcessor()
Method returns route handler
protected
getUniversalRouteProcessor() : mixed
Return values
mixed —paramRouteExists()
Method returns true if the param router exists
protected
paramRouteExists(string $route, string $requestMethod) : bool
Parameters
- $route : string
-
checking route
- $requestMethod : string
-
HTTP request method
Return values
bool —true if the param router exists, false otherwise
setCalledRoute()
Method sets $calledRoute
protected
abstract setCalledRoute(string $calledRoute) : void
Parameters
- $calledRoute : string
-
called router
Return values
void —setParameters()
Method sets parameters
protected
abstract setParameters(array<string|int, mixed> $parameters) : void
Parameters
- $parameters : array<string|int, mixed>
-
parameters
Return values
void —_getParameterNames()
Method returns all parameter names in the route
private
_getParameterNames(string $routerPattern) : array<string|int, string>
Parameters
- $routerPattern : string
-
route
Return values
array<string|int, string> —names
_getRouteMatcherRegExPattern()
Method compiles route pattern string in regex string.
private
_getRouteMatcherRegExPattern(string $routerPattern) : string
For example [i:id]/some-str in ([[0-9]])/some-str
Parameters
- $routerPattern : string
-
router pattern
Tags
Return values
string —regexp pattern
compileRegexpForBunch()
Method compiles regexp for the bunch of routes
private
compileRegexpForBunch(array<string|int, mixed> &$bunch) : void
Parameters
- $bunch : array<string|int, mixed>
Return values
void —compileRegexpForBunches()
Method compiles all regeps for all routes
private
compileRegexpForBunches() : void
Return values
void —getRegExpAppendix()
Generating appendix for the RegExp
private
getRegExpAppendix(int $i) : string
Parameters
- $i : int
-
count of ()
Return values
string —appendix
initDefaultTypes()
Init types
private
initDefaultTypes() : void