Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
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
Yeray Santana Hualde
syncEnrollments
Commits
bae9553a
Commit
bae9553a
authored
Sep 14, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring router route delegation.
parent
cd609d9b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
50 deletions
+62
-50
arr.php
laravel/arr.php
+20
-0
caller.php
laravel/routing/caller.php
+5
-15
route.php
laravel/routing/route.php
+37
-35
No files found.
laravel/arr.php
View file @
bae9553a
...
...
@@ -77,4 +77,24 @@ class Arr {
$array
[
array_shift
(
$keys
)]
=
$value
;
}
/**
* Return the first element in an array which passes a given truth test.
*
* <code>
* // Get the first element in an array that is less than 2
* $value = Arr::first(array(4, 3, 2, 1), function($key, $value) {return $value < 2;});
* </code>
*
* @param array $array
* @param Closure $callback
* @return mixed
*/
public
static
function
first
(
$array
,
$callback
)
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
call_user_func
(
$callback
,
$key
,
$value
))
return
$value
;
}
}
}
\ No newline at end of file
laravel/routing/caller.php
View file @
bae9553a
...
...
@@ -50,11 +50,6 @@ class Caller {
*/
public
function
call
(
Route
$route
)
{
if
(
!
$route
->
callback
instanceof
\Closure
and
!
is_array
(
$route
->
callback
))
{
throw
new
\Exception
(
'Invalid route defined for URI ['
.
$route
->
key
.
']'
);
}
// Since "before" filters can halt the request cycle, we will return any response
// from the before filters. Allowing the filters to halt the request cycle makes
// common tasks like authorization convenient to implement.
...
...
@@ -65,10 +60,10 @@ class Caller {
if
(
!
is_null
(
$response
=
$route
->
call
()))
{
// If a route returns a
n array, it means that
the route is delegating the
// If a route returns a
string, it also means
the route is delegating the
// handling of the request to a controller method. So, we will pass the
//
array to the route delegator and let it resolve the controller
.
if
(
is_
array
(
$response
))
$response
=
$this
->
delegate
(
$route
,
$response
);
//
string to the route delegator, exploding on "::"
.
if
(
is_
string
(
$response
))
$response
=
$this
->
delegate
(
$route
,
explode
(
'::'
,
$response
)
);
return
$this
->
finish
(
$route
,
$response
);
}
...
...
@@ -101,15 +96,10 @@ class Caller {
* @param array $delegate
* @return mixed
*/
p
ublic
function
delegate
(
Route
$route
,
$delegate
)
p
rotected
function
delegate
(
Route
$route
,
$delegate
)
{
list
(
$controller
,
$method
)
=
array
(
$delegate
[
0
],
$delegate
[
1
]);
// A route delegate may contain an array of parameters that should be passed to
// the controller method. If it does, we will merge those parameters in with
// the other route parameters that were detected by the router.
$parameters
=
(
isset
(
$delegate
[
2
]))
?
array_merge
((
array
)
$delegate
[
2
],
$route
->
parameters
)
:
$route
->
parameters
;
$controller
=
$this
->
resolve
(
$controller
);
// If the controller doesn't exist or the request is to an invalid method, we will
...
...
@@ -127,7 +117,7 @@ class Caller {
// will not be used to handle the request to the application.
$response
=
$controller
->
before
();
return
(
is_null
(
$response
))
?
call_user_func_array
(
array
(
$controller
,
$method
),
$parameters
)
:
$response
;
return
(
is_null
(
$response
))
?
call_user_func_array
(
array
(
$controller
,
$method
),
$
route
->
parameters
)
:
$response
;
}
/**
...
...
laravel/routing/route.php
View file @
bae9553a
<?php
namespace
Laravel\Routing
;
use
Closure
;
use
Laravel\Arr
;
class
Route
{
...
...
@@ -45,54 +46,55 @@ class Route {
$this
->
key
=
$key
;
$this
->
callback
=
$callback
;
$this
->
parameters
=
$parameters
;
$this
->
uris
=
$this
->
parse_uris
(
$key
);
}
/**
* Parse the route key and return an array of URIs the route responds to.
*
* @param string $key
* @return array
*/
protected
function
parse_uris
(
$key
)
// Extract each URI out of the route key. Since the route key has the request
// method, we will extract the method off of the string. If the URI points to
// the root of the application, a single forward slash will be returned.
// Otherwise, the leading slash will be removed.
foreach
(
explode
(
', '
,
$key
)
as
$segment
)
{
if
(
strpos
(
$key
,
', '
)
===
false
)
return
array
(
$this
->
extract_uri
(
$key
));
$this
->
uris
[]
=
(
$segment
=
(
substr
(
$segment
,
strpos
(
$segment
,
' '
)
+
1
))
!==
'/'
)
?
trim
(
$segment
,
'/'
)
:
$segment
;
}
// The extractor closure will retrieve the URI from a given route destination.
// If the request is to the root of the application, a single forward slash
// will be returned, otherwise the leading slash will be removed.
$extractor
=
function
(
$segment
)
// The route callback must be either a Closure, an array, or a string. Closures
// obviously handle the requests to the route. An array can contain filters, as
// well as a Closure to handle requests to the route. A string, delegates control
// of the request to a controller method.
if
(
!
$this
->
callback
instanceof
\Closure
and
!
is_array
(
$this
->
callback
)
and
!
is_string
(
$this
->
callback
))
{
$segment
=
substr
(
$segment
,
strpos
(
$segment
,
' '
)
+
1
);
return
(
$segment
!==
'/'
)
?
trim
(
$segment
,
'/'
)
:
$segment
;
};
return
array_map
(
function
(
$segment
)
use
(
$extractor
)
{
return
$extractor
(
$segment
);
},
explode
(
', '
,
$key
));
throw
new
\Exception
(
'Invalid route defined for URI ['
.
$this
->
key
.
']'
);
}
}
/**
* Call the route closure.
*
* If no closure is defined for the route, null will be returned.
*
* @return mixed
*/
public
function
call
()
{
return
(
!
is_null
(
$closure
=
$this
->
closure
()))
?
call_user_func_array
(
$closure
,
$this
->
parameters
)
:
null
;
// If the value defined for a route is a Closure, we simply call the closure with the
// route's parameters and return the response.
if
(
$this
->
callback
instanceof
Closure
)
{
return
call_user_func_array
(
$this
->
callback
,
$this
->
parameters
);
}
/**
* Extract the route closure from the route.
*
* @return Closure|null
*/
protected
function
closure
()
// Otherwise, we will assume the route is an array and will return the first value with
// a key of "do", or the first instance of a Closure. If the value is a string, the route
// is delegating the responsibility for handling the request to a controller.
elseif
(
is_array
(
$this
->
callback
))
{
if
(
$this
->
callback
instanceof
Closure
)
return
$this
->
callback
;
return
Arr
::
first
(
$this
->
callback
,
function
(
$key
,
$value
)
{
return
$key
==
'do'
or
$value
instanceof
Closure
;});
}
foreach
(
$this
->
callback
as
$value
)
{
if
(
$value
instanceof
Closure
)
return
$value
;
}
// If a value defined for a route is a string, it means the route is delegating control
// of the request to a controller. If that is the case, we will simply return the string
// for the route caller to parse and delegate.
elseif
(
is_string
(
$this
->
callback
))
{
return
$this
->
callback
;
}
}
/**
...
...
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