Skip to content

Commit

Permalink
Merge pull request #180 from zf-fr/fix-capture
Browse files Browse the repository at this point in the history
Fix entrypoints
  • Loading branch information
bakura10 committed Oct 19, 2014
2 parents ea12a93 + 0df13e4 commit c31696b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.3.4

* Fix a bug with entry points. Previously, if you had an entry point configured as "/users", ZfrRest used to
match URLs like "/userssssss".

## 0.3.3

* Fix an issue with camelCased associations when rendering a resource
Expand Down
9 changes: 5 additions & 4 deletions src/ZfrRest/Router/Http/ResourceGraphRoute.php
Expand Up @@ -160,15 +160,16 @@ public function match(RequestInterface $request, $pathOffset = 0)
$path = substr($path, strlen(trim($baseUrl, '/')));
}

// If the URI does not begin by the route, we can stop immediately
if (substr($path, 0, strlen($this->route)) !== $this->route) {
$pathParts = explode('/', trim($path, '/'), 2);

// If the first path part does not match the entry point, we can stop immediately
if ($pathParts[0] !== trim($this->route, '/')) {
return null;
}

// If we have only one segment (for instance "users"), then the next path to analyze is in fact
// an empty string, hence the ternary condition
$pathParts = explode('/', trim($path, '/'), 2);
$subPath = count($pathParts) === 1 ? '' : end($pathParts);
$subPath = count($pathParts) === 1 ? '' : end($pathParts);

if (!$match = $this->subPathMatcher->matchSubPath($this->getResource(), $subPath)) {
// Although this is an error, we still want to create a route match, so that the request
Expand Down
Expand Up @@ -25,6 +25,7 @@
/**
* @ORM\Entity
* @REST\Resource(hydrator="ClassMethods")
* @REST\Collection(controller="UserListController")
*/
class User
{
Expand Down
25 changes: 25 additions & 0 deletions tests/ZfrRestTest/Router/Http/ResourceGraphRouteTest.php
Expand Up @@ -143,6 +143,31 @@ public function testCanAssembleWithResourceAndAssociation()
]));
}

public function testCanOnlyMatchExactEntryPoint()
{
$serviceManager = ServiceManagerFactory::getServiceManager();
$resourceGraphRoute = new ResourceGraphRoute(
$serviceManager->get('ZfrRest\Resource\Metadata\ResourceMetadataFactory'),
$serviceManager->get('ZfrRest\Resource\ResourcePluginManager'),
$serviceManager->get('ZfrRest\Router\Http\Matcher\BaseSubPathMatcher'),
'ZfrRestTest\Asset\Resource\Metadata\Annotation\User',
'/users'
);

$httpRequest = new HttpRequest();
$httpRequest->setUri('/users/');

$match = $resourceGraphRoute->match($httpRequest);
$this->assertInternalType('string', $match->getParam('controller'));

// Now, with the entrypoint "/userssssss/", it should fail!

$httpRequest->setUri('/usersssss/');

$match = $resourceGraphRoute->match($httpRequest);
$this->assertNull($match);
}

public function testCanMatchControllerWhenOverridenOnAssociation()
{
$serviceManager = ServiceManagerFactory::getServiceManager();
Expand Down

0 comments on commit c31696b

Please sign in to comment.