Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webbasics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
webbasics
Commits
6c8b446f
Commit
6c8b446f
authored
12 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added Router class.
parent
1dee302c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
router.php
+94
-0
94 additions, 0 deletions
router.php
tests/test_router.php
+44
-0
44 additions, 0 deletions
tests/test_router.php
with
138 additions
and
0 deletions
router.php
0 → 100644
+
94
−
0
View file @
6c8b446f
<?php
/**
* Functions for URL routing: given an URL, call the corresponding handler
* function (the 'route' to the corresponding output).
*
* @author Taddeus Kroes
* @version 1.0
* @date 14-07-2012
*/
namespace
BasicWeb
;
require_once
'base.php'
;
/**
*
*
* <code>
*
* </code>
*
* @package BasicWeb
*/
class
Router
extends
Base
{
/**
* The regex delimiter that is added to the begin and end of patterns.
*
* @var string
*/
const
DELIMITER
=
'%'
;
/**
* An associative array of regex patterns pointing to handler functions.
*
* @var array
*/
private
$routes
=
array
();
/**
* Create a new Router instance.
*
* @param array $routes An initial list of routes to set.
*/
function
__construct
(
array
$routes
=
array
())
{
foreach
(
$routes
as
$pattern
=>
$handler
)
$this
->
add_route
(
$pattern
,
$handler
);
}
/**
* Add a route as a (pattern, handler) pair.
*
* The pattern is regular expression pattern without delimiters. The
* function adds '%^' at the begin and '$%' at the end of the pattern as
* delimiters.
*
* The handler function must receive no arguments if the regex pattern
* does not contain groups (which are contained in parentheses). If there
* are groups, the matches for these are passed to the handler fucntion in
* an array.
*
* @param string $pattern A regex pattern to mach URL's against.
* @param mixed $handler The handler function to call when $pattern is matched.
* @throws \InvalidArgumentException If $handler is not callable.
*/
function
add_route
(
$pattern
,
$handler
)
{
if
(
!
is_callable
(
$handler
)
)
throw
new
\InvalidArgumentException
(
sprintf
(
'Handler for patterns "%s" is not callable.'
,
$pattern
));
$this
->
routes
[
self
::
DELIMITER
.
'^'
.
$pattern
.
'$'
.
self
::
DELIMITER
]
=
$handler
;
}
/**
* Call the handler function corresponding to the specified url.
*
* If any groups are in the matched regex pattern, a list of matches is
* passed to the handler function.
*
* @param string $url An url to match the saved patterns against.
* @return mixed FALSE if no pattern was matched, the return value of the
* corresponding handler function otherwise.
*/
function
call_handler
(
$url
)
{
foreach
(
$this
->
routes
as
$pattern
=>
$handler
)
{
if
(
preg_match
(
$pattern
,
$url
,
$matches
)
)
{
array_shift
(
$matches
);
return
call_user_func_array
(
$handler
,
$matches
);
}
}
return
false
;
}
}
?>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/test_router.php
0 → 100644
+
44
−
0
View file @
6c8b446f
<?php
require_once
'router.php'
;
use
BasicWeb\Router
;
function
test_handler_no_args
()
{
return
true
;
}
function
test_handler_arg
(
$arg
)
{
return
$arg
;
}
function
test_handler_args
(
$arg0
,
$arg1
)
{
return
$arg1
.
$arg0
;
}
class
RouterTest
extends
PHPUnit_Framework_TestCase
{
function
setUp
()
{
$this
->
router
=
new
Router
(
array
(
'foo'
=>
'test_handler_no_args'
,
'(ba[rz])'
=>
'test_handler_arg'
,
'(ba[rz])(ba[rz])'
=>
'test_handler_args'
,
));
}
function
test_call_handler_success
()
{
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'foo'
),
true
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'bar'
),
'bar'
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'baz'
),
'baz'
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'bazbar'
),
'barbaz'
);
}
function
test_call_handler_failure
()
{
$this
->
assertFalse
(
$this
->
router
->
call_handler
(
'barfoo'
));
}
function
test_add_route
()
{
$this
->
router
->
add_route
(
'(foobar)'
,
'test_handler_arg'
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'foobar'
),
'foobar'
);
}
}
?>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment