Skip to content

Commit 9e4c51d

Browse files
committed
Implement the internal request dispatcher.
Signed-off-by: Jason Lewis <[email protected]>
1 parent b7dead2 commit 9e4c51d

File tree

8 files changed

+1097
-12
lines changed

8 files changed

+1097
-12
lines changed

src/Dispatcher.php

Lines changed: 646 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Dingo\Api\Exception;
4+
5+
use Exception;
6+
use Illuminate\Http\Response;
7+
use Symfony\Component\HttpKernel\Exception\HttpException;
8+
9+
class InternalHttpException extends HttpException
10+
{
11+
/**
12+
* Create a new internal HTTP exception instance.
13+
*
14+
* @param \Illuminate\Http\Response $response
15+
* @param string $message
16+
* @param \Exception $previous
17+
* @param array $headers
18+
* @param int $code
19+
*
20+
* @return void
21+
*/
22+
public function __construct(Response $response, $message = null, Exception $previous = null, array $headers = [], $code = 0)
23+
{
24+
$this->response = $response;
25+
26+
parent::__construct($response->getStatusCode(), $message, $previous, $headers, $code);
27+
}
28+
29+
/**
30+
* Get the response of the internal request.
31+
*
32+
* @return \Illuminate\Http\Response
33+
*/
34+
public function getResponse()
35+
{
36+
return $this->response;
37+
}
38+
}

src/Http/InternalRequest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Dingo\Api\Http;
4+
5+
class InternalRequest extends Request
6+
{
7+
8+
}

src/Routing/Route.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,16 @@ public function isProtected()
362362
return $this->protected === true;
363363
}
364364

365+
/**
366+
* Get the name of the route.
367+
*
368+
* @return string
369+
*/
370+
public function getName()
371+
{
372+
return array_get($this->action, 'as', null);
373+
}
374+
365375
/**
366376
* Get the route scopes.
367377
*

src/Routing/Router.php

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ class Router
8484
*/
8585
protected $currentRoute;
8686

87+
/**
88+
* The number of routes dispatched.
89+
*
90+
* @var int
91+
*/
92+
protected $routesDispatched = 0;
93+
8794
/**
8895
* Create a new router instance.
8996
*
@@ -511,28 +518,27 @@ protected function formatPrefix($new, $old)
511518
* Dispatch a request via the adapter.
512519
*
513520
* @param \Dingo\Api\Http\Request $request
521+
* @param bool $raw
514522
*
515523
* @return \Dingo\Api\Http\Response
516524
*/
517-
public function dispatch(Request $request)
525+
public function dispatch(Request $request, $raw = false)
518526
{
519527
$this->currentRoute = null;
520528

521529
$accept = $this->accept->parse($request);
522530

523531
$this->container->instance('Dingo\Api\Http\Request', $request);
524532

533+
$this->routesDispatched++;
534+
525535
try {
526536
$response = $this->adapter->dispatch($request, $accept['version']);
527-
528-
return $this->prepareResponse($response, $request, $accept['format']);
529537
} catch (Exception $exception) {
530-
return $this->prepareResponse(
531-
$this->exception->handle($exception),
532-
$request,
533-
$accept['format']
534-
);
538+
$response = $this->exception->handle($exception);
535539
}
540+
541+
return $this->prepareResponse($response, $request, $accept['format'], $raw);
536542
}
537543

538544
/**
@@ -541,10 +547,11 @@ public function dispatch(Request $request)
541547
* @param \Illuminate\Http\Response $response
542548
* @param \Dingo\Api\Http\Request $request
543549
* @param string $format
550+
* @param bool $raw
544551
*
545552
* @return \Dingo\Api\Http\Response
546553
*/
547-
protected function prepareResponse(IlluminateResponse $response, Request $request, $format)
554+
protected function prepareResponse(IlluminateResponse $response, Request $request, $format, $raw = false)
548555
{
549556
if (! $response instanceof Response) {
550557
$response = Response::makeFromExisting($response);
@@ -560,7 +567,9 @@ protected function prepareResponse(IlluminateResponse $response, Request $reques
560567
return $this->exception->handle($exception);
561568
}
562569

563-
$response = $response->morph($format);
570+
if (! $raw) {
571+
$response = $response->morph($format);
572+
}
564573

565574
if ($response->isSuccessful() && $this->requestIsConditional()) {
566575
if (! $response->headers->has('ETag')) {
@@ -614,13 +623,32 @@ public function getCurrentRoute()
614623
{
615624
if (isset($this->currentRoute)) {
616625
return $this->currentRoute;
626+
} elseif (! $this->hasDispatchedRoutes()) {
627+
return null;
617628
}
618629

619630
$request = $this->container['request'];
620631

621632
return $this->currentRoute = new Route($this->adapter, $this->container, $request, $request->route());
622633
}
623634

635+
/**
636+
* Set the current route instance.
637+
*
638+
* @param \Dingo\Api\Routing\Route $route
639+
*
640+
* @return void
641+
*/
642+
public function setCurrentRoute(Route $route)
643+
{
644+
$this->currentRoute = $route;
645+
}
646+
647+
/**
648+
* Determine if the router has a group stack.
649+
*
650+
* @return bool
651+
*/
624652
public function hasGroupStack()
625653
{
626654
return ! empty($this->groupStack);
@@ -653,4 +681,24 @@ public function getRoutes($version = null)
653681
{
654682
return $this->adapter->getRoutes($version);
655683
}
684+
685+
/**
686+
* Get the number of routes dispatched.
687+
*
688+
* @return int
689+
*/
690+
public function getRoutesDispatched()
691+
{
692+
return $this->routesDispatched;
693+
}
694+
695+
/**
696+
* Determine if the router has dispatched any routes.
697+
*
698+
* @return bool
699+
*/
700+
public function hasDispatchedRoutes()
701+
{
702+
return $this->routesDispatched > 0;
703+
}
656704
}

0 commit comments

Comments
 (0)