Skip to content

Commit

Permalink
Add getActionName() method to Route
Browse files Browse the repository at this point in the history
  • Loading branch information
joelambert committed Jul 13, 2018
1 parent 7103578 commit 41715f4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Route
private $invoker = null;
private $middleware = [];
private $paramConstraints = [];
private $controllerName = null;
private $controllerMethod = null;

public function __construct(array $methods, string $uri, $action, Invoker $invoker = null)
{
Expand Down Expand Up @@ -50,6 +52,9 @@ private function setAction($action)

private function convertClassStringToClosure($string)
{
$this->controllerName = null;
$this->controllerMethod = null;

@list($className, $method) = explode('@', $string);

if (!isset($className) || !isset($method)) {
Expand All @@ -68,6 +73,9 @@ private function convertClassStringToClosure($string)
throw new RouteClassStringMethodNotFoundException('Route controller class: `' . $className . '` does not have a `' . $method . '` method');
}

$this->controllerName = $className;
$this->controllerMethod = $method;

return function ($params = null) use ($className, $method) {
$controller = new $className;
return $controller->$method($params);
Expand Down Expand Up @@ -170,4 +178,19 @@ public function getName()
{
return $this->name;
}

public function getActionName()
{
$callableName = null;

if (isset($this->controllerName)) {
return $this->controllerName;
}

if (is_callable($this->action, false, $callableName)) {
list($controller, $method) = explode('::', $callableName);

return $controller;
}
}
}
43 changes: 43 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,47 @@ public function where_function_throws_exception_when_no_params_provided()

$this->assertInstanceOf(Route::class, $router->get('test/{id}', function () {})->where());
}

/** @test */
public function can_get_route_action_name_when_closure()
{
$router = new Router;
$route = $router->get('test/123', function () {});

$this->assertSame('Closure', $route->getActionName());
}

/** @test */
public function can_get_route_action_name_when_callable()
{
$router = new Router;
$route = $router->get('test/123', [TestCallableController::class, 'testStatic']);

$this->assertSame(TestCallableController::class, $route->getActionName());
}

/** @test */
public function can_get_route_action_name_when_callable_instance()
{
$router = new Router;
$controller = new TestCallableController;
$route = $router->get('test/123', [$controller, 'test']);

$this->assertSame(TestCallableController::class, $route->getActionName());
}

/** @test */
public function can_get_route_action_name_when_controller_string()
{
$router = new Router;
$route = $router->get('test/123', TestCallableController::class.'@test');

$this->assertSame(TestCallableController::class, $route->getActionName());
}
}

class TestCallableController
{
public static function testStatic() {}
public function test() {}
}

0 comments on commit 41715f4

Please sign in to comment.