Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
7fc25bec
Commit
7fc25bec
authored
Dec 30, 2017
by
pupi1985
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use strtr function to map placeholders to regular expressions
parent
17f1cfcf
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
9 deletions
+15
-9
Router.php
qa-src/Http/Routing/Router.php
+15
-9
No files found.
qa-src/Http/Routing/Router.php
View file @
7fc25bec
...
...
@@ -24,10 +24,7 @@ class Router
protected
$routes
;
/** @var array */
private
$paramsConverterKeys
;
/** @var array */
private
$paramsConverterValues
;
private
$paramsConverter
;
/** @var string */
private
$httpMethod
;
...
...
@@ -36,12 +33,10 @@ class Router
{
$this
->
routes
=
$routeData
;
$paramsConverter
=
array
(
$
this
->
paramsConverter
=
array
(
'{str}'
=>
'([^/]+)'
,
'{int}'
=>
'([0-9]+)'
,
);
$this
->
paramsConverterKeys
=
array_keys
(
$paramsConverter
);
$this
->
paramsConverterValues
=
array_values
(
$paramsConverter
);
$this
->
httpMethod
=
strtoupper
(
$_SERVER
[
'REQUEST_METHOD'
]);
}
...
...
@@ -51,6 +46,14 @@ class Router
$this
->
routes
[]
=
new
Route
(
$id
,
$httpMethod
,
$routePath
,
$class
,
$func
);
}
/**
* Return the route definition that matches the given request. If none is found then null is
* returned.
*
* @param string $request Request that will be looked for a match
*
* @return Route|null
*/
public
function
match
(
$request
)
{
foreach
(
$this
->
routes
as
$route
)
{
...
...
@@ -70,13 +73,16 @@ class Router
}
/**
* @param $routePath
* Build the final regular expression to match the request. This method replaces all the
* parameters' placeholders with the given regular expression.
*
* @param string $routePath Route that might have placeholders
*
* @return string
*/
private
function
buildPathRegex
(
$routePath
)
{
return
'#^'
.
str
_replace
(
$this
->
paramsConverterKeys
,
$this
->
paramsConverterValues
,
$routePath
)
.
'$#'
;
return
'#^'
.
str
tr
(
$routePath
,
$this
->
paramsConverter
)
.
'$#'
;
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment