Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fixed instance style callable routes #34994

Merged
merged 2 commits into from Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Illuminate/Routing/RouteAction.php
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
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;
}
}
9 changes: 9 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Expand Up @@ -129,6 +129,15 @@ public function testCanRegisterRouteWithControllerAction()
$this->seeMiddleware('controller-middleware');
}

public function testCanRegisterRouteWithControllerActionArray()
{
$this->router->middleware('controller-middleware')
->get('users', [RouteRegistrarControllerStub::class, 'index']);

$this->seeResponse('controller', Request::create('users', 'GET'));
$this->seeMiddleware('controller-middleware');
}

public function testCanRegisterRouteWithArrayAndControllerAction()
{
$this->router->middleware('controller-middleware')->put('users', [
Expand Down