Skip to content

Commit

Permalink
Fixed instance style callable routes
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Oct 27, 2020
1 parent 302766f commit dad177a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Illuminate/Routing/RouteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Routing;

use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
use LogicException;
use UnexpectedValueException;
Expand All @@ -28,7 +29,7 @@ public static function parse($uri, $action)
// If the action is already a Closure instance, we will just set that instance
// as the "uses" property, because there is nothing else we need to do when
// it is available. Otherwise we will need to find it in the action list.
if (is_callable($action, true)) {
if (static::isCallable($action)) {
return ! is_array($action) ? ['uses' => $action] : [
'uses' => $action[0].'@'.$action[1],
'controller' => $action[0].'@'.$action[1],
Expand Down Expand Up @@ -73,10 +74,25 @@ protected static function missingAction($uri)
protected static function findCallable(array $action)
{
return Arr::first($action, function ($value, $key) {
return is_callable($value) && is_numeric($key);
return static::isCallable($value) && is_numeric($key);
});
}

/**
* Determine if the given action is callable as a route.
*
* @param mixed $action
* @return bool
*/
public static function isCallable($action)
{
if (! is_array($action)) {
return is_callable($action);
}

return isset($action[0], $action[1]) && is_string($action[0]) && is_string($action[1]) && Reflector::isMethodCallable($action[0], $action[1]);
}

/**
* Make an action for an invokable controller.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Illuminate/Support/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support;

use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;

class Reflector
Expand Down Expand Up @@ -51,4 +52,28 @@ public static function isParameterSubclassOf($parameter, $className)
? (new ReflectionClass($paramClassName))->isSubclassOf($className)
: false;
}

/**
* Determine if the class method is callable.
*
* @param string $class
* @param string $method
* @return bool
*/
public static function isMethodCallable($class, $method)
{
if (! class_exists($class)) {
return false;
}

if (method_exists($class, $method)) {
return (new ReflectionMethod($class, $method))->isPublic();
}

if (method_exists($class, '__call')) {
return (new ReflectionMethod($class, '__call'))->isPublic();
}

return false;
}
}

0 comments on commit dad177a

Please sign in to comment.