Mapper
in package
The mapper class handles URL generation and recognition for web applications
The mapper class is built by handling associated arrays of information and passing associated arrays back to the application for it to handle and dispatch the appropriate scripts.
Table of Contents
- $alwaysScan : bool
- Call controllerScan callback before every route match?
- $appendSlash : bool
- Append trailing slash ('/') to generated routes?
- $cache : Horde_Cache
- Cache
- $cacheLifetime : int
- Cache lifetime for the same value of $this->matchList
- $controllerScan : callable
- Callback function used to get array of controller names
- $debug : bool
- Collect debug information during route match?
- $decodeErrors : string
- What to do on decoding errors? 'ignore' or 'replace'
- $directory : string
- Path to controller directory passed to controllerScan function
- $domainMatch : string
- Partial regexp used to match domain part of the end of URLs to match
- $encoding : string
- Encoding of routes URLs (not yet supported)
- $environ : array<string|int, mixed>
- Filtered request environment with keys like SCRIPT_NAME
- $explicit : bool
- Disable route memory and implicit defaults?
- $matchList : array<string|int, mixed>
- Array of connected routes
- $maxKeys : array<string|int, mixed>
- Array of all connected routes, indexed by the serialized array of all keys that each route could utilize.
- $minKeys : array<string|int, mixed>
- Array of all connected routes, indexed by the serialized array of the minimum keys that each route needs.
- $prefix : null|string
- Prefix to strip during matching and to append during generation
- $routeNames : array<string|int, mixed>
- Array of connected named routes, indexed by name
- $subDomains : bool
- Use sub-domain support?
- $subDomainsIgnore : array<string|int, mixed>
- Array of sub-domains to ignore if using sub-domain support
- $urlCache : array<string|int, mixed>
- Cache of URLs used in generate()
- $utils : Utils
- Utility functions like urlFor() and redirectTo() for this Mapper
- $_createdRegs : bool
- Have regular expressions been created for all connected routes?
- $_keysortTmp : array<string|int, mixed>
- Temporary variable used to pass array of keys into _keysort() callback
- $_regPrefix : string
- Regular expression generated to match after the prefix
- $createdGens : bool
- Have generation hashes been created for all connected routes?
- $gendict : array<string|int, mixed>
- Generation hashes created for all connected routes
- __construct() : mixed
- Constructor.
- connect() : void
- Create and connect a new Route to the Mapper.
- createRegs() : void
- Creates the regexes for all connected routes
- generate() : null|string
- Generates the URL from a given set of keywords Returns the URL text, or null if no URL could be generated.
- match() : array<string|int, mixed>|null
- Match a URL against one of the routes contained.
- resource() : void
- Generate routes for a controller resource
- routematch() : array<string|int, mixed>|null
- Match a URL against one of the routes contained.
- setCache() : mixed
- Set an optional Horde_Cache object for the created rules.
- _arrayUnion() : array<string|int, mixed>
- Create a union of two arrays.
- _cmp() : int
- Equivalent of Python's cmp() function.
- _createGens() : void
- Create the generation hashes (arrays) for route lookups
- _keycmp() : int
- Compare two Horde_Route_Routes objects by their keys against the instance variable $keysortTmp. Used by _keysort().
- _keysort() : void
- Sort an array of Horde_Routes_Routes to using _keycmp() for the comparision to order them ideally for matching.
- _match() : null|array<string|int, mixed>
- Internal Route matcher
- _requirementsFor() : array<string|int, mixed>
- Returns a new dict to be used for all route creation as the route options.
- _stripSlashes() : string
- Trims slashes from the beginning or end of a part/URL.
- _swap() : array<string|int, mixed>
- Swap the keys and values in the dict, and uppercase the values from the dict during the swap.
Properties
$alwaysScan
Call controllerScan callback before every route match?
public
bool
$alwaysScan
$appendSlash
Append trailing slash ('/') to generated routes?
public
bool
$appendSlash
= false
$cache
Cache
public
Horde_Cache
$cache
$cacheLifetime
Cache lifetime for the same value of $this->matchList
public
int
$cacheLifetime
= 86400
$controllerScan
Callback function used to get array of controller names
public
callable
$controllerScan
$debug
Collect debug information during route match?
public
bool
$debug
= false
$decodeErrors
What to do on decoding errors? 'ignore' or 'replace'
public
string
$decodeErrors
= 'ignore'
$directory
Path to controller directory passed to controllerScan function
public
string
$directory
$domainMatch
Partial regexp used to match domain part of the end of URLs to match
public
string
$domainMatch
= '[^\.\/]+?\.[^\.\/]+'
$encoding
Encoding of routes URLs (not yet supported)
public
string
$encoding
= 'utf-8'
$environ
Filtered request environment with keys like SCRIPT_NAME
public
array<string|int, mixed>
$environ
= []
$explicit
Disable route memory and implicit defaults?
public
bool
$explicit
$matchList
Array of connected routes
public
array<string|int, mixed>
$matchList
= []
$maxKeys
Array of all connected routes, indexed by the serialized array of all keys that each route could utilize.
public
array<string|int, mixed>
$maxKeys
= []
$minKeys
Array of all connected routes, indexed by the serialized array of the minimum keys that each route needs.
public
array<string|int, mixed>
$minKeys
= []
$prefix
Prefix to strip during matching and to append during generation
public
null|string
$prefix
= null
$routeNames
Array of connected named routes, indexed by name
public
array<string|int, mixed>
$routeNames
= []
$subDomains
Use sub-domain support?
public
bool
$subDomains
= false
$subDomainsIgnore
Array of sub-domains to ignore if using sub-domain support
public
array<string|int, mixed>
$subDomainsIgnore
= []
$urlCache
Cache of URLs used in generate()
public
array<string|int, mixed>
$urlCache
= []
$utils
Utility functions like urlFor() and redirectTo() for this Mapper
public
Utils
$utils
$_createdRegs
Have regular expressions been created for all connected routes?
protected
bool
$_createdRegs
= false
$_keysortTmp
Temporary variable used to pass array of keys into _keysort() callback
protected
array<string|int, mixed>
$_keysortTmp
$_regPrefix
Regular expression generated to match after the prefix
protected
string
$_regPrefix
= null
$createdGens
Have generation hashes been created for all connected routes?
protected
bool
$createdGens
= false
$gendict
Generation hashes created for all connected routes
protected
array<string|int, mixed>
$gendict
Methods
__construct()
Constructor.
public
__construct([mixed $kargs = [] ]) : mixed
Keyword arguments ($kargs):
controllerScan
(callback)
Function to return an array of valid controllers
redirect
(callback)
Function to perform a redirect for Horde_Routes_Utils->redirectTo()
directory
(string)
Path to the directory that will be passed to the
controllerScan callback
alwaysScan
(boolean)
Should the controllerScan callback be called
before every URL match?
explicit
(boolean)
Should routes be connected with the implicit defaults of
array('controller'=>'content', 'action'=>'index', 'id'=>null)?
When set to True, these will not be added to route connections.
Parameters
- $kargs : mixed = []
Return values
mixed —connect()
Create and connect a new Route to the Mapper.
public
connect(mixed $first[, mixed $second = null ][, mixed $third = null ]) : void
Usage: $m = new Horde_Routes_Mapper(); $m->connect(':controller/:action/:id'); $m->connect('date/:year/:month/:day', array('controller' => "blog", 'action' => 'view'); $m->connect('archives/:page', array('controller' => 'blog', 'action' => 'by_page', ' requirements' => array('page' => '\d{1,2}'))); $m->connect('category_list', 'archives/category/:section', array('controller' => 'blog', 'action' => 'category', 'section' => 'home', 'type' => 'list')); $m->connect('home', '', array('controller' => 'blog', 'action' => 'view', 'section' => 'home'));
Parameters
- $first : mixed
-
First argument in vargs, see usage above.
- $second : mixed = null
-
Second argument in varags
- $third : mixed = null
-
Third argument in varargs
Return values
void —createRegs()
Creates the regexes for all connected routes
public
createRegs([array<string|int, mixed> $clist = null ]) : void
Parameters
- $clist : array<string|int, mixed> = null
-
controller list, controller_scan will be used otherwise
Return values
void —generate()
Generates the URL from a given set of keywords Returns the URL text, or null if no URL could be generated.
public
generate([array<string|int, mixed>|null $first = null ][, array<string|int, mixed>|null $second = null ]) : null|string
Usage: $m->generate(array('controller' => 'content', 'action' => 'view', 'id' => 10));
Parameters
- $first : array<string|int, mixed>|null = null
-
Optional explicit route list
- $second : array<string|int, mixed>|null = null
-
Keyword arguments (key/value pairs)
Return values
null|string —URL text or null
match()
Match a URL against one of the routes contained.
public
match(string $url) : array<string|int, mixed>|null
It will return null if no valid match is found.
Usage: $resultdict = $m->match('/joe/sixpack');
Parameters
- $url : string
-
URL to match
Return values
array<string|int, mixed>|null —Array if matched, otherwise null
resource()
Generate routes for a controller resource
public
resource(string $memberName, string $collectionName[, array<string|int, mixed> $kargs = [] ]) : void
The $memberName name should be the appropriate singular version of the resource given your locale and used with members of the collection.
The $collectionName name will be used to refer to the resource collection methods and should be a plural version of the $memberName argument. By default, the $memberName name will also be assumed to map to a controller you create.
The concept of a web resource maps somewhat directly to 'CRUD' operations. The overlying things to keep in mind is that mapping a resource is about handling creating, viewing, and editing that resource.
All keyword arguments ($kargs) are optional.
controller
If specified in the keyword args, the controller will be the actual
controller used, but the rest of the naming conventions used for
the route names and URL paths are unchanged.
collection
Additional action mappings used to manipulate/view the entire set of
resources provided by the controller.
Example::
$map->resource('message', 'messages',
array('collection' => array('rss' => 'GET)));
# GET /message;rss (maps to the rss action)
# also adds named route "rss_message"
member
Additional action mappings used to access an individual 'member'
of this controllers resources.
Example::
$map->resource('message', 'messages',
array('member' => array('mark' => 'POST')));
# POST /message/1;mark (maps to the mark action)
# also adds named route "mark_message"
new
Action mappings that involve dealing with a new member in the
controller resources.
Example::
$map->resource('message', 'messages',
array('new' => array('preview' => 'POST')));
# POST /message/new;preview (maps to the preview action)
# also adds a url named "preview_new_message"
pathPrefix
Prepends the URL path for the Route with the pathPrefix given.
This is most useful for cases where you want to mix resources
or relations between resources.
namePrefix
Perpends the route names that are generated with the namePrefix
given. Combined with the pathPrefix option, it's easy to
generate route names and paths that represent resources that are
in relations.
Example::
map.resource('message', 'messages',
array('controller' => 'categories',
'pathPrefix' => '/category/:category_id',
'namePrefix' => 'category_')));
# GET /category/7/message/1
# has named route "category_message"
parentResource
An assoc. array containing information about the parent resource,
for creating a nested resource. It should contain the $memberName
and collectionName
of the parent resource. This assoc. array will
be available via the associated Route
object which can be
accessed during a request via request.environ['routes.route']
If ``parentResource`` is supplied and ``pathPrefix`` isn't,
``pathPrefix`` will be generated from ``parentResource`` as
"<parent collection name>/:<parent member name>_id".
If ``parentResource`` is supplied and ``namePrefix`` isn't,
``namePrefix`` will be generated from ``parentResource`` as
"<parent member name>_".
Example::
$m = new Horde_Routes_Mapper();
$utils = $m->utils;
$m->resource('location', 'locations',
array('parentResource' =>
array('memberName' => 'region',
'collectionName' => 'regions'))));
# pathPrefix is "regions/:region_id"
# namePrefix is "region_"
$utils->urlFor('region_locations', array('region_id'=>13));
# '/regions/13/locations'
$utils->urlFor('region_new_location', array('region_id'=>13));
# '/regions/13/locations/new'
$utils->urlFor('region_location',
array('region_id'=>13, 'id'=>60));
# '/regions/13/locations/60'
$utils->urlFor('region_edit_location',
array('region_id'=>13, 'id'=>60));
# '/regions/13/locations/60/edit'
Overriding generated pathPrefix
::
$m = new Horde_Routes_Mapper();
$utils = new Horde_Routes_Utils();
$m->resource('location', 'locations',
array('parentResource' =>
array('memberName' => 'region',
'collectionName' => 'regions'),
'pathPrefix' => 'areas/:area_id')));
# name prefix is "region_"
$utils->urlFor('region_locations', array('area_id'=>51));
# '/areas/51/locations'
Overriding generated namePrefix
::
$m = new Horde_Routes_Mapper
$m->resource('location', 'locations',
array('parentResource' =>
array('memberName' => 'region',
'collectionName' => 'regions'),
'namePrefix' => '')));
# pathPrefix is "regions/:region_id"
$utils->urlFor('locations', array('region_id'=>51));
# '/regions/51/locations'
Note: Since Horde Routes 0.2.0 and Python Routes 1.8, this method is not compatible with earlier versions inasmuch as the semicolon is no longer used to delimit custom actions. This was a change in Rails itself (http://dev.rubyonrails.org/changeset/6485) and adopting it here allows us to keep parity with Rails and ActiveResource.
Parameters
- $memberName : string
-
Singular version of the resource name
- $collectionName : string
-
Collection name (plural of $memberName)
- $kargs : array<string|int, mixed> = []
-
Keyword arguments (see above)
Return values
void —routematch()
Match a URL against one of the routes contained.
public
routematch(string $url) : array<string|int, mixed>|null
It will return null if no valid match is found, otherwise a result dict (array) and a route object is returned.
Usage: list($resultdict, $resultobj) = $m->match('/joe/sixpack');
Parameters
- $url : string
-
URL to match
Return values
array<string|int, mixed>|null —Array if matched, otherwise null
setCache()
Set an optional Horde_Cache object for the created rules.
public
setCache(Horde_Cache $cache) : mixed
Parameters
- $cache : Horde_Cache
-
Cache object
Return values
mixed —_arrayUnion()
Create a union of two arrays.
protected
_arrayUnion(array<string|int, mixed> $a, array<string|int, mixed> $b) : array<string|int, mixed>
Parameters
- $a : array<string|int, mixed>
-
First array
- $b : array<string|int, mixed>
-
Second array
Return values
array<string|int, mixed> —Union of $a and $b
_cmp()
Equivalent of Python's cmp() function.
protected
_cmp(int|float $a, int|float $b) : int
Parameters
- $a : int|float
-
First item to compare
- $b : int|float
-
Second item to compare
Return values
int —Result of comparison
_createGens()
Create the generation hashes (arrays) for route lookups
protected
_createGens() : void
Return values
void —_keycmp()
Compare two Horde_Route_Routes objects by their keys against the instance variable $keysortTmp. Used by _keysort().
protected
_keycmp(Route $a, Route $b) : int
Parameters
Return values
int —_keysort()
Sort an array of Horde_Routes_Routes to using _keycmp() for the comparision to order them ideally for matching.
protected
_keysort(array<string|int, mixed> &$array) : void
An unfortunate property of PHP's usort() is that if two members compare equal, their order in the sorted array is undefined (see PHP manual). This is unsuitable for us because the order that the routes were connected to the mapper is significant.
Uses this method uses merge sort algorithm based on the comments in http://www.php.net/usort
Parameters
- $array : array<string|int, mixed>
-
Array Horde_Routes_Route objects to sort (by reference)
Return values
void —_match()
Internal Route matcher
protected
_match(string $url) : null|array<string|int, mixed>
Matches a URL against a route, and returns a tuple (array) of the match dict (array) and the route object if a match is successful, otherwise it returns null.
Parameters
- $url : string
-
URL to match
Return values
null|array<string|int, mixed> —Match data if matched, otherwise null
_requirementsFor()
Returns a new dict to be used for all route creation as the route options.
protected
_requirementsFor(string $method, array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $method : string
-
Request method ('get', 'post', etc.) or 'any'
- $options : array<string|int, mixed>
-
Assoc. array to populate with 'conditions' key
Tags
Return values
array<string|int, mixed> —$options populated
_stripSlashes()
Trims slashes from the beginning or end of a part/URL.
protected
_stripSlashes(string $name) : string
Parameters
- $name : string
-
Part or URL with slash at begin/end
Return values
string —Part or URL with begin/end slashes removed
_swap()
Swap the keys and values in the dict, and uppercase the values from the dict during the swap.
protected
_swap(array<string|int, mixed> $dct, array<string|int, mixed> $newdct) : array<string|int, mixed>
Parameters
- $dct : array<string|int, mixed>
-
Input dict (assoc. array)
- $newdct : array<string|int, mixed>
-
Output dict to populate
Tags
Return values
array<string|int, mixed> —$newdct populated