Skip to content

Commit c43bd7e

Browse files
Merge pull request dingo#1619 from specialtactics/feature/implement-param-pattern-matching
Feature/implement param pattern matching
2 parents e0c8648 + c1e0eb8 commit c43bd7e

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

src/Routing/Adapter/Laravel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class Laravel implements Adapter
4646
*/
4747
protected $oldRoutes;
4848

49+
/**
50+
* The globally available parameter patterns.
51+
*
52+
* @var array
53+
*/
54+
protected $patterns = [];
55+
4956
/**
5057
* Create a new laravel routing adapter instance.
5158
*
@@ -245,4 +252,14 @@ public function getRouter()
245252
{
246253
return $this->router;
247254
}
255+
256+
/**
257+
* Get the global "where" patterns.
258+
*
259+
* @return array
260+
*/
261+
public function getPatterns()
262+
{
263+
return $this->patterns;
264+
}
248265
}

src/Routing/Router.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Illuminate\Container\Container;
1515
use Dingo\Api\Contract\Routing\Adapter;
1616
use Dingo\Api\Contract\Debug\ExceptionHandler;
17+
use Illuminate\Routing\Route as IlluminateRoute;
1718
use Illuminate\Http\Response as IlluminateResponse;
19+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1820
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
1921

2022
class Router
@@ -622,6 +624,17 @@ public function getCurrentRoute()
622624
return;
623625
}
624626

627+
// We need to recompile the route, adding the where clause (for pattern restrictions) and check again
628+
if (is_object($route) && $route instanceof IlluminateRoute) {
629+
$route->compiled = false;
630+
$this->addWhereClausesToRoute($route);
631+
632+
// If the matching fails, it would be due to a parameter format validation check fail
633+
if (! $route->matches($this->container['request'])) {
634+
throw new NotFoundHttpException('Not Found!');
635+
}
636+
}
637+
625638
return $this->currentRoute = $this->createRoute($route);
626639
}
627640

@@ -844,4 +857,22 @@ public function currentRouteUses($action)
844857
{
845858
return $this->currentRouteAction() == $action;
846859
}
860+
861+
/**
862+
* Add the necessary where clauses to the route based on its initial registration.
863+
*
864+
* @param \Illuminate\Routing\Route $route
865+
*
866+
* @return \Illuminate\Routing\Route
867+
*/
868+
protected function addWhereClausesToRoute($route)
869+
{
870+
$patterns = app()->make(\Illuminate\Routing\Router::class)->getPatterns();
871+
872+
$route->where(array_merge(
873+
$patterns, $route->getAction()['where'] ?? []
874+
));
875+
876+
return $route;
877+
}
847878
}

tests/DispatcherTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function setUp()
4343
$this->auth = new Auth($this->router, $this->container, []);
4444
$this->dispatcher = new Dispatcher($this->container, new Filesystem, $this->router, $this->auth);
4545

46+
app()->instance(\Illuminate\Routing\Router::class, $this->adapter, true);
47+
4648
$this->dispatcher->setSubtype('api');
4749
$this->dispatcher->setStandardsTree('vnd');
4850
$this->dispatcher->setDefaultVersion('v1');

tests/Routing/Adapter/BaseAdapterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function setUp()
2929
$this->adapter = $this->getAdapterInstance();
3030
$this->exception = m::mock(\Dingo\Api\Exception\Handler::class);
3131
$this->router = new Router($this->adapter, $this->exception, $this->container, null, null);
32+
app()->instance(\Illuminate\Routing\Router::class, $this->adapter, true);
3233

3334
Http\Response::setFormatters(['json' => new Http\Response\Format\Json]);
3435
}

tests/Stubs/RoutingAdapterStub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public function pattern($key, $pattern)
111111
$this->patterns[$key] = $pattern;
112112
}
113113

114+
public function getPatterns()
115+
{
116+
return $this->patterns;
117+
}
118+
114119
protected function createRouteCollections(array $versions)
115120
{
116121
foreach ($versions as $version) {

0 commit comments

Comments
 (0)