Skip to content

Commit ff58875

Browse files
committed
fixing merge
2 parents 124a781 + aceabb9 commit ff58875

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

laravel/helpers.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,4 +595,47 @@ function get_file_size($size)
595595
{
596596
$units = array('Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB');
597597
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2).' '.$units[$i];
598-
}
598+
}
599+
600+
/**
601+
* Attempt to call a function with associate array values as parameters.
602+
*
603+
* @param Closure|array $callback
604+
* @param array $parameters
605+
* @return mixed
606+
*/
607+
function call_user_func_assoc($callback, $parameters)
608+
{
609+
$method = null;
610+
611+
if (is_array($callback))
612+
{
613+
$reflector = new \ReflectionClass($callback[0]);
614+
$method = $reflector->getMethod($callback[1]);
615+
}
616+
else
617+
{
618+
$method = new \ReflectionFunction($callback);
619+
}
620+
621+
$ordered = array();
622+
foreach ($method->getParameters() as $param)
623+
{
624+
if (isset($parameters[$param->name]))
625+
{
626+
$ordered[] = $parameters[$param->name];
627+
}
628+
else
629+
{
630+
$ordered = null;
631+
break;
632+
}
633+
}
634+
635+
if ( ! is_null($ordered))
636+
{
637+
return call_user_func_array($callback, $ordered);
638+
}
639+
640+
return call_user_func_array($callback, $parameters);
641+
}

laravel/routing/controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public function response($method, $parameters = array())
320320
$action = "action_{$method}";
321321
}
322322

323-
$response = call_user_func_array(array($this, $action), $parameters);
323+
$response = call_user_func_assoc(array($this, $action), $parameters);
324324

325325
// If the controller has specified a layout view the response
326326
// returned by the controller method will be bound to that

laravel/routing/route.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ protected function parameters($action, $parameters)
9494
{
9595
$defaults = (array) array_get($action, 'defaults');
9696

97+
// Move the non-numeric indices from the parameters array
98+
// to the names array for use later.
99+
$names = array();
100+
foreach (array_keys($parameters) as $key)
101+
{
102+
if ( ! is_numeric($key))
103+
{
104+
$names[$key] = $parameters[$key];
105+
unset($parameters[$key]);
106+
}
107+
}
108+
97109
// If there are less parameters than wildcards, we will figure out how
98110
// many parameters we need to inject from the array of defaults and
99111
// merge them into the main array for the route.
@@ -104,7 +116,8 @@ protected function parameters($action, $parameters)
104116
$parameters = array_merge($parameters, $defaults);
105117
}
106118

107-
$this->parameters = $parameters;
119+
// Stick the named parameters on the end so that they don't interfere.
120+
$this->parameters = array_merge($parameters, $names);
108121
}
109122

110123
/**
@@ -160,7 +173,7 @@ public function response()
160173

161174
if ( ! is_null($handler))
162175
{
163-
return call_user_func_array($handler, $this->parameters);
176+
return call_user_func_assoc($handler, $this->parameters);
164177
}
165178
}
166179

@@ -270,6 +283,21 @@ public function is($name)
270283
return array_get($this->action, 'as') === $name;
271284
}
272285

286+
/**
287+
* Determine if the route is valid for the parameters.
288+
*
289+
* @return bool
290+
*/
291+
public function validates()
292+
{
293+
if ( ! empty($this->action['rules']))
294+
{
295+
return \Validator::make($this->parameters, $this->action['rules'])->passes();
296+
}
297+
298+
return true;
299+
}
300+
273301
/**
274302
* Register a controller with the router.
275303
*

laravel/routing/router.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,18 @@ protected static function match($method, $uri)
491491
{
492492
$pattern = '#^'.static::wildcards($route).'$#';
493493

494+
$pattern = static::names($pattern);
495+
494496
// If we get a match we'll return the route and slice off the first
495497
// parameter match, as preg_match sets the first array item to the
496498
// full-text match of the pattern.
497499
if (preg_match($pattern, $uri, $parameters))
498500
{
499-
return new Route($method, $route, $action, array_slice($parameters, 1));
501+
$match = new Route($method, $route, $action, array_slice($parameters, 1));
502+
if ($match->validates())
503+
{
504+
return $match;
505+
}
500506
}
501507
}
502508
}
@@ -525,6 +531,18 @@ protected static function wildcards($key)
525531
return strtr($key, static::$patterns);
526532
}
527533

534+
/**
535+
* Translate unmatched URI parameters into named parameters.
536+
*
537+
* @param string $key
538+
* @return string
539+
*/
540+
protected static function names($key)
541+
{
542+
$pattern = trim(static::$patterns['(:any)'], '()');
543+
return preg_replace('/\(:([^\)]+)\)/', "(?P<$1>".$pattern.')', $key);
544+
}
545+
528546
/**
529547
* Get all of the registered routes, with fallbacks at the end.
530548
*

0 commit comments

Comments
 (0)