Skip to content

Commit c31696b

Browse files
committed
Merge pull request #180 from zf-fr/fix-capture
Fix entrypoints
2 parents ea12a93 + 0df13e4 commit c31696b

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 0.3.4
4+
5+
* Fix a bug with entry points. Previously, if you had an entry point configured as "/users", ZfrRest used to
6+
match URLs like "/userssssss".
7+
38
## 0.3.3
49

510
* Fix an issue with camelCased associations when rendering a resource

src/ZfrRest/Router/Http/ResourceGraphRoute.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,16 @@ public function match(RequestInterface $request, $pathOffset = 0)
160160
$path = substr($path, strlen(trim($baseUrl, '/')));
161161
}
162162

163-
// If the URI does not begin by the route, we can stop immediately
164-
if (substr($path, 0, strlen($this->route)) !== $this->route) {
163+
$pathParts = explode('/', trim($path, '/'), 2);
164+
165+
// If the first path part does not match the entry point, we can stop immediately
166+
if ($pathParts[0] !== trim($this->route, '/')) {
165167
return null;
166168
}
167169

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

173174
if (!$match = $this->subPathMatcher->matchSubPath($this->getResource(), $subPath)) {
174175
// Although this is an error, we still want to create a route match, so that the request

tests/ZfrRestTest/Asset/Resource/Metadata/Annotation/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/**
2626
* @ORM\Entity
2727
* @REST\Resource(hydrator="ClassMethods")
28+
* @REST\Collection(controller="UserListController")
2829
*/
2930
class User
3031
{

tests/ZfrRestTest/Router/Http/ResourceGraphRouteTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,31 @@ public function testCanAssembleWithResourceAndAssociation()
143143
]));
144144
}
145145

146+
public function testCanOnlyMatchExactEntryPoint()
147+
{
148+
$serviceManager = ServiceManagerFactory::getServiceManager();
149+
$resourceGraphRoute = new ResourceGraphRoute(
150+
$serviceManager->get('ZfrRest\Resource\Metadata\ResourceMetadataFactory'),
151+
$serviceManager->get('ZfrRest\Resource\ResourcePluginManager'),
152+
$serviceManager->get('ZfrRest\Router\Http\Matcher\BaseSubPathMatcher'),
153+
'ZfrRestTest\Asset\Resource\Metadata\Annotation\User',
154+
'/users'
155+
);
156+
157+
$httpRequest = new HttpRequest();
158+
$httpRequest->setUri('/users/');
159+
160+
$match = $resourceGraphRoute->match($httpRequest);
161+
$this->assertInternalType('string', $match->getParam('controller'));
162+
163+
// Now, with the entrypoint "/userssssss/", it should fail!
164+
165+
$httpRequest->setUri('/usersssss/');
166+
167+
$match = $resourceGraphRoute->match($httpRequest);
168+
$this->assertNull($match);
169+
}
170+
146171
public function testCanMatchControllerWhenOverridenOnAssociation()
147172
{
148173
$serviceManager = ServiceManagerFactory::getServiceManager();

0 commit comments

Comments
 (0)