Skip to content

Commit

Permalink
Implemented API::route and API::action for name-based and action-base…
Browse files Browse the repository at this point in the history
…d requests. Closes #12.

Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
  • Loading branch information
jasonlewis committed Apr 26, 2014
1 parent 3448298 commit 7d35539
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/ApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function registerDispatcher()
{
$this->app['dingo.api.dispatcher'] = $this->app->share(function($app)
{
return new Dispatcher($app['request'], $app['router'], $app['dingo.api.authentication']);
return new Dispatcher($app['request'], $app['url'], $app['router'], $app['dingo.api.authentication']);
});
}

Expand Down
71 changes: 60 additions & 11 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Dingo\Api\Routing\Router;
use Illuminate\Auth\GenericUser;
use Dingo\Api\Http\InternalRequest;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
Expand All @@ -20,6 +21,13 @@ class Dispatcher {
*/
protected $request;

/**
* Illuminate url generator instance.
*
* @var \Illuminate\Routing\UrlGenerator
*/
protected $url;

/**
* API router instance.
*
Expand Down Expand Up @@ -73,13 +81,15 @@ class Dispatcher {
* Create a new dispatcher instance.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Routing\UrlGenerator $url
* @param \Dingo\Api\Routing\Router $router
* @param \Dingo\Api\Authentication $auth
* @return void
*/
public function __construct(Request $request, Router $router, Authentication $auth)
public function __construct(Request $request, UrlGenerator $url, Router $router, Authentication $auth)
{
$this->request = $request;
$this->url = $url;
$this->router = $router;
$this->auth = $auth;
}
Expand Down Expand Up @@ -130,21 +140,59 @@ public function version($version)
/**
* Set the parameters to be sent on the next API request.
*
* @param array $parameters
* @param string|array $parameters
* @return \Dingo\Api\Dispatcher
*/
public function with(array $parameters)
public function with($parameters)
{
$this->parameters = $parameters;
$this->parameters = is_array($parameters) ? $parameters : func_get_args();

return $this;
}

/**
* Perform an API request to a named route.
*
* @param string $name
* @param string|array $routeParameters
* @param string|array $parameters
* @return mixed
*/
public function route($name, $routeParameters = [], $parameters = [])
{
$version = $this->version ?: $this->router->getDefaultVersion();

$route = $this->router->getApiRouteCollection($version)->getByName($name);

$uri = $this->url->route($name, $routeParameters, false, $route);

return $this->queueRequest($route->methods()[0], $uri, $parameters);
}

/**
* Perform an API request to a controller action.
*
* @param string $name
* @param string|array $actionParameters
* @param string|array $parameters
* @return mixed
*/
public function action($action, $actionParameters = [], $parameters = [])
{
$version = $this->version ?: $this->router->getDefaultVersion();

$route = $this->router->getApiRouteCollection($version)->getByAction($action);

$uri = $this->url->route($action, $actionParameters, false, $route);

return $this->queueRequest($route->methods()[0], $uri, $parameters);
}

/**
* Perform API GET request.
*
* @param string $uri
* @param array $parameters
* @param string|array $parameters
* @return mixed
*/
public function get($uri, $parameters = [])
Expand All @@ -156,7 +204,7 @@ public function get($uri, $parameters = [])
* Perform API POST request.
*
* @param string $uri
* @param array $parameters
* @param string|array $parameters
* @return mixed
*/
public function post($uri, $parameters = [])
Expand All @@ -168,7 +216,7 @@ public function post($uri, $parameters = [])
* Perform API PUT request.
*
* @param string $uri
* @param array $parameters
* @param string|array $parameters
* @return mixed
*/
public function put($uri, $parameters = [])
Expand All @@ -180,7 +228,7 @@ public function put($uri, $parameters = [])
* Perform API PATCH request.
*
* @param string $uri
* @param array $parameters
* @param string|array $parameters
* @return mixed
*/
public function patch($uri, $parameters = [])
Expand All @@ -192,7 +240,7 @@ public function patch($uri, $parameters = [])
* Perform API DELETE request.
*
* @param string $uri
* @param array $parameters
* @param string|array $parameters
* @return mixed
*/
public function delete($uri, $parameters = [])
Expand All @@ -205,7 +253,7 @@ public function delete($uri, $parameters = [])
*
* @param string $verb
* @param string $uri
* @param array $parameters
* @param string|array $parameters
* @return mixed
*/
protected function queueRequest($verb, $uri, $parameters)
Expand All @@ -224,6 +272,7 @@ protected function queueRequest($verb, $uri, $parameters)
*
* @param string $verb
* @param string $uri
* @param string|array $parameters
* @return \Dingo\Api\Http\InternalRequest
*/
protected function createRequest($verb, $uri, $parameters)
Expand All @@ -247,7 +296,7 @@ protected function createRequest($verb, $uri, $parameters)
$uri = "{$prefix}/{$uri}";
}

$parameters = array_merge($this->parameters, $parameters);
$parameters = array_merge($this->parameters, (array) $parameters);

$request = InternalRequest::create($uri, $verb, $parameters, $cookies, $files, $server);

Expand Down
35 changes: 34 additions & 1 deletion tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dingo\Api\Http\Response;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Router;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Events\Dispatcher as EventsDispatcher;
use Dingo\Api\Http\ResponseFormat\JsonResponseFormat;

Expand All @@ -18,7 +19,8 @@ public function setUp()
$this->router->setDefaultVersion('v1');
$this->router->setVendor('test');
$this->auth = m::mock('Dingo\Api\Authentication');
$this->dispatcher = new Dispatcher($this->request, $this->router, $this->auth);
$this->url = new UrlGenerator($this->router->getRoutes(), $this->request);
$this->dispatcher = new Dispatcher($this->request, $this->url, $this->router, $this->auth);

Response::setFormatters(['json' => new JsonResponseFormat]);
}
Expand Down Expand Up @@ -145,6 +147,37 @@ public function testPretendingToBeUserForSingleRequest()

$this->dispatcher->be($user)->once()->get('test');
}


public function testInternalRequestUsingRouteName()
{
$this->router->api(['version' => 'v1'], function()
{
$this->router->get('test', ['as' => 'test', function()
{
return 'foo';
}]);

$this->router->get('test/{foo}', ['as' => 'testparameters', function($parameter)
{
return $parameter;
}]);
});

$this->assertEquals('foo', $this->dispatcher->route('test'));
$this->assertEquals('bar', $this->dispatcher->route('testparameters', 'bar'));
}


public function testInternalRequestUsingControllerAction()
{
$this->router->api(['version' => 'v1'], function()
{
$this->router->get('test', 'InternalControllerActionRoutingStub@index');
});

$this->assertEquals('foo', $this->dispatcher->action('InternalControllerActionRoutingStub@index'));
}


}
11 changes: 10 additions & 1 deletion tests/stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,13 @@ public function index() {}

class StubHttpException extends Symfony\Component\HttpKernel\Exception\HttpException {

}
}

class InternalControllerActionRoutingStub extends Illuminate\Routing\Controller {

public function index()
{
return 'foo';
}

}

0 comments on commit 7d35539

Please sign in to comment.