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
175a7421
Commit
175a7421
authored
Dec 25, 2017
by
pupi1985
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid recreating params arrays
parent
419c30ab
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
3 deletions
+27
-3
Router.php
qa-src/Http/Router.php
+27
-3
No files found.
qa-src/Http/Router.php
View file @
175a7421
...
...
@@ -20,11 +20,25 @@ namespace Q2A\Http;
class
Router
{
/** @var array */
protected
$routes
;
/** @var array */
private
$paramsConverterKeys
;
/** @var array */
private
$paramsConverterValues
;
public
function
__construct
(
$routeData
=
array
())
{
$this
->
routes
=
$routeData
;
$paramsConverter
=
array
(
'{str}'
=>
'([^/]+)'
,
'{int}'
=>
'([0-9]+)'
,
);
$this
->
paramsConverterKeys
=
array_keys
(
$paramsConverter
);
$this
->
paramsConverterValues
=
array_values
(
$paramsConverter
);
}
public
function
addRoute
(
$id
,
$httpMethod
,
$routePath
,
$class
,
$func
)
...
...
@@ -34,7 +48,7 @@ class Router
public
function
match
(
$request
)
{
foreach
(
$this
->
routes
as
$id
=>
$route
)
{
foreach
(
$this
->
routes
as
$id
=>
$route
)
{
if
(
count
(
$route
)
!==
4
)
{
continue
;
}
...
...
@@ -45,17 +59,27 @@ class Router
continue
;
}
$pathRegex
=
'#^'
.
str_replace
(
array
(
'{str}'
,
'{int}'
),
array
(
'([^/]+)'
,
'([0-9]+)'
),
$routePath
)
.
'$#'
;
$pathRegex
=
$this
->
buildPathRegex
(
$routePath
)
;
if
(
preg_match
(
$pathRegex
,
$request
,
$matches
))
{
return
array
(
'id'
=>
$id
,
'controller'
=>
$callClass
,
'function'
=>
$callFunc
,
'params'
=>
array_slice
(
$matches
,
1
)
'params'
=>
array_slice
(
$matches
,
1
)
,
);
}
}
return
false
;
}
/**
* @param $routePath
*
* @return string
*/
private
function
buildPathRegex
(
$routePath
)
{
return
'#^'
.
str_replace
(
$this
->
paramsConverterKeys
,
$this
->
paramsConverterValues
,
$routePath
)
.
'$#'
;
}
}
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