Skip to content

Commit 78d1dce

Browse files
committed
[Refactor] Retrieve resource URI from the schema
The schema now has a `uriType` method which returns the resource type as it appears in URIs. This commit updates the routing implementation to use the value from the schema, and removes the `uri()` method on resource route registration as it is no longer needed.
1 parent b0bbab8 commit 78d1dce

File tree

5 files changed

+26
-73
lines changed

5 files changed

+26
-73
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ classes you are retaining.
3535
### Removed
3636
- **BREAKING** The `Arr` schema field has been removed - use the new `ArrayList` or `ArrayHash`
3737
fields instead.
38-
- **BREAKING** The `uri` method on relationship routes has been removed. Relationship URIs
39-
are now set on the schema field for the relationship (via the `withUriFieldName` method).
38+
- **BREAKING** The `uri` method on resource and relationship routes has been removed:
39+
- The resource type URI can now be set on the resource's schema (using the `$uriType` property).
40+
- Relationship URIs are now set on the schema field for the relationship (via the `withUriFieldName` method).
4041

4142
## [1.0.0-alpha.1] - 2021-01-25
4243

src/Routing/PendingResourceRegistration.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,6 @@ public function __construct(
8282
$this->options = [];
8383
}
8484

85-
/**
86-
* Set the URI for the resource type, if it is not the same as the resource type's name.
87-
*
88-
* @param string $uri
89-
* @return $this
90-
*/
91-
public function uri(string $uri): self
92-
{
93-
$this->options['resource_uri'] = $uri;
94-
95-
return $this;
96-
}
97-
9885
/**
9986
* Set the methods the controller should apply to.
10087
*

src/Routing/ResourceRegistrar.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function register(string $resourceType, string $controller, array $option
140140
*/
141141
protected function addResourceIndex(string $resourceType, string $controller, array $options): IlluminateRoute
142142
{
143-
$uri = $this->getResourceUri($resourceType, $options);
143+
$uri = $this->getResourceUri($resourceType);
144144
$action = $this->getResourceAction($resourceType, $controller, 'index', null, $options);
145145

146146
$route = $this->router->get($uri, $action);
@@ -159,7 +159,7 @@ protected function addResourceIndex(string $resourceType, string $controller, ar
159159
*/
160160
protected function addResourceStore(string $resourceType, string $controller, array $options): IlluminateRoute
161161
{
162-
$uri = $this->getResourceUri($resourceType, $options);
162+
$uri = $this->getResourceUri($resourceType);
163163
$action = $this->getResourceAction($resourceType, $controller, 'store', null, $options);
164164

165165
$route = $this->router->post($uri, $action);
@@ -179,7 +179,7 @@ protected function addResourceStore(string $resourceType, string $controller, ar
179179
protected function addResourceShow(string $resourceType, string $controller, array $options): IlluminateRoute
180180
{
181181
$parameter = $this->getResourceParameterName($resourceType, $options);
182-
$uri = $this->getResourceUri($resourceType, $options);
182+
$uri = $this->getResourceUri($resourceType);
183183
$action = $this->getResourceAction($resourceType, $controller, 'show', $parameter, $options);
184184

185185
$route = $this->router->get(sprintf('%s/{%s}', $uri, $parameter), $action);
@@ -200,7 +200,7 @@ protected function addResourceShow(string $resourceType, string $controller, arr
200200
protected function addResourceUpdate(string $resourceType, string $controller, array $options): IlluminateRoute
201201
{
202202
$parameter = $this->getResourceParameterName($resourceType, $options);
203-
$uri = $this->getResourceUri($resourceType, $options);
203+
$uri = $this->getResourceUri($resourceType);
204204
$action = $this->getResourceAction($resourceType, $controller, 'update', $parameter, $options);
205205

206206
$route = $this->router->patch(sprintf('%s/{%s}', $uri, $parameter), $action);
@@ -221,7 +221,7 @@ protected function addResourceUpdate(string $resourceType, string $controller, a
221221
protected function addResourceDestroy(string $resourceType, string $controller, array $options): IlluminateRoute
222222
{
223223
$parameter = $this->getResourceParameterName($resourceType, $options);
224-
$uri = $this->getResourceUri($resourceType, $options);
224+
$uri = $this->getResourceUri($resourceType);
225225
$action = $this->getResourceAction($resourceType, $controller, 'destroy', $parameter, $options);
226226

227227
$route = $this->router->delete(sprintf('%s/{%s}', $uri, $parameter), $action);
@@ -233,16 +233,14 @@ protected function addResourceDestroy(string $resourceType, string $controller,
233233

234234
/**
235235
* @param string $resourceType
236-
* @param array $options
237236
* @return string
238237
*/
239-
private function getResourceUri(string $resourceType, array $options): string
238+
private function getResourceUri(string $resourceType): string
240239
{
241-
if (isset($options['resource_uri'])) {
242-
return $options['resource_uri'];
243-
}
244-
245-
return Str::dasherize($resourceType);
240+
return $this->server
241+
->schemas()
242+
->schemaFor($resourceType)
243+
->uriType();
246244
}
247245

248246
/**
@@ -313,7 +311,7 @@ private function getResourceAction(
313311
*/
314312
private function getRelationshipsAction(string $resourceType, ?string $parameter, array $options)
315313
{
316-
$uri = $this->getResourceUri($resourceType, $options);
314+
$uri = $this->getResourceUri($resourceType);
317315

318316
$action = [
319317
'prefix' => sprintf('%s/{%s}', $uri, $parameter),

tests/lib/Integration/Routing/ResourceTest.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ public function testServerDomain(string $method, string $uri): void
281281
public function testResourceUri(string $method, string $uri): void
282282
{
283283
$server = $this->createServer('v1');
284-
$this->createSchema($server, 'blog:posts');
284+
$this->createSchema($server, 'blog:posts', null, 'posts');
285285

286286
$this->defaultApiRoutesWithNamespace(function () {
287287
JsonApiRoute::server('v1')
288288
->prefix('v1')
289289
->namespace('Api\\V1')
290290
->resources(function ($server) {
291-
$server->resource('blog:posts')->uri('posts');
291+
$server->resource('blog:posts');
292292
});
293293
});
294294

@@ -469,44 +469,4 @@ public function testReadOnly(): void
469469
]);
470470
}
471471

472-
/**
473-
* @return array
474-
*/
475-
public function multiWordProvider(): array
476-
{
477-
return [
478-
'dash' => ['blog-posts', 'blog-posts', 'blog_post'],
479-
'underscore' => ['blog_posts', 'blog-posts', 'blog_post'],
480-
'camel' => ['blogPosts', 'blog-posts', 'blogPost'],
481-
];
482-
}
483-
484-
/**
485-
* @param string $type
486-
* @param string $uri
487-
* @param string $parameter
488-
* @dataProvider multiWordProvider
489-
* @see https://github.com/cloudcreativity/laravel-json-api/issues/224
490-
*/
491-
public function testMultiWord(string $type, string $uri, string $parameter): void
492-
{
493-
$server = $this->createServer('v1');
494-
$this->createSchema($server, $type, '\d+');
495-
496-
$this->defaultApiRoutesWithNamespace(function () use ($type) {
497-
JsonApiRoute::server('v1')
498-
->prefix('v1')
499-
->namespace('Api\\V1')
500-
->resources(function ($server) use ($type) {
501-
$server->resource($type);
502-
});
503-
});
504-
505-
$route = $this->assertMatch('GET', "/api/v1/{$uri}/123");
506-
$this->assertSame("v1.{$type}.show", $route->getName());
507-
$this->assertSame($type, $route->parameter('resource_type'));
508-
$this->assertSame($parameter, $route->parameter('resource_id_name'));
509-
$this->assertSame('\d+', $route->action['where'][$parameter] ?? null);
510-
}
511-
512472
}

tests/lib/Integration/Routing/TestCase.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,21 @@ protected function createServer(string $name): Server
6767
/**
6868
* @param Server|MockObject $server
6969
* @param string $name
70-
* @param string $pattern
70+
* @param string|null $pattern
71+
* @param string|null $uriType
7172
* @return Schema|MockObject
7273
*/
73-
protected function createSchema(Server $server, string $name, string $pattern = '[0-9]+'): Schema
74+
protected function createSchema(
75+
Server $server,
76+
string $name,
77+
string $pattern = null,
78+
string $uriType = null
79+
): Schema
7480
{
7581
$schema = $this->createMock(Schema::class);
82+
$schema->method('uriType')->willReturn($uriType ?: $name);
7683
$schema->method('id')->willReturn($id = $this->createMock(ID::class));
77-
$id->method('pattern')->willReturn($pattern);
84+
$id->method('pattern')->willReturn($pattern ?: '[0-9]+');
7885

7986
$schemas = $this->createMock(Container::class);
8087
$schemas->method('schemaFor')->with($name)->willReturn($schema);

0 commit comments

Comments
 (0)