Skip to content

Commit 759d512

Browse files
committed
fix: defining relation resources with the same name as parent
1 parent 07b92c4 commit 759d512

File tree

4 files changed

+194
-21
lines changed

4 files changed

+194
-21
lines changed

src/Http/Routing/BelongsToManyRelationResourceRegistrar.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class BelongsToManyRelationResourceRegistrar extends RelationResourceRegistrar
2222
* @param array $options
2323
* @return Route
2424
*/
25-
protected function addResourceSync(string $name, string $base, string $controller, array $options)
25+
protected function addResourceSync(string $name, string $base, string $controller, array $options): Route
2626
{
27-
$uri = $this->getResourceUri($name).'/sync';
27+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/sync';
2828

2929
$action = $this->getResourceAction($name, $controller, 'sync', $options);
3030

@@ -40,9 +40,9 @@ protected function addResourceSync(string $name, string $base, string $controlle
4040
* @param array $options
4141
* @return Route
4242
*/
43-
protected function addResourceToggle(string $name, string $base, string $controller, array $options)
43+
protected function addResourceToggle(string $name, string $base, string $controller, array $options): Route
4444
{
45-
$uri = $this->getResourceUri($name).'/toggle';
45+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/toggle';
4646

4747
$action = $this->getResourceAction($name, $controller, 'toggle', $options);
4848

@@ -58,9 +58,9 @@ protected function addResourceToggle(string $name, string $base, string $control
5858
* @param array $options
5959
* @return Route
6060
*/
61-
protected function addResourceUpdatePivot(string $name, string $base, string $controller, array $options)
61+
protected function addResourceUpdatePivot(string $name, string $base, string $controller, array $options): Route
6262
{
63-
$uri = $this->getResourceUri($name).'/{'.$base.'?}/pivot';
63+
$uri = $this->getNestedResourceUriWithNestedParameter($name, $base).'/pivot';
6464

6565
$action = $this->getResourceAction($name, $controller, 'updatePivot', $options);
6666

@@ -76,9 +76,9 @@ protected function addResourceUpdatePivot(string $name, string $base, string $co
7676
* @param array $options
7777
* @return Route
7878
*/
79-
protected function addResourceAttach(string $name, string $base, string $controller, array $options)
79+
protected function addResourceAttach(string $name, string $base, string $controller, array $options): Route
8080
{
81-
$uri = $this->getResourceUri($name).'/attach';
81+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/attach';
8282

8383
$action = $this->getResourceAction($name, $controller, 'attach', $options);
8484

@@ -94,12 +94,12 @@ protected function addResourceAttach(string $name, string $base, string $control
9494
* @param array $options
9595
* @return Route
9696
*/
97-
protected function addResourceDetach(string $name, string $base, string $controller, array $options)
97+
protected function addResourceDetach(string $name, string $base, string $controller, array $options): Route
9898
{
99-
$uri = $this->getResourceUri($name).'/detach';
99+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/detach';
100100

101101
$action = $this->getResourceAction($name, $controller, 'detach', $options);
102102

103103
return $this->router->delete($uri, $action);
104104
}
105-
}
105+
}

src/Http/Routing/HasManyRelationResourceRegistrar.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class HasManyRelationResourceRegistrar extends RelationResourceRegistrar
2222
* @param array $options
2323
* @return Route
2424
*/
25-
protected function addResourceAssociate(string $name, string $base, string $controller, array $options)
25+
protected function addResourceAssociate(string $name, string $base, string $controller, array $options): Route
2626
{
27-
$uri = $this->getResourceUri($name).'/associate';
27+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/associate';
2828

2929
$action = $this->getResourceAction($name, $controller, 'associate', $options);
3030

@@ -40,12 +40,12 @@ protected function addResourceAssociate(string $name, string $base, string $cont
4040
* @param array $options
4141
* @return Route
4242
*/
43-
protected function addResourceDissociate(string $name, string $base, string $controller, array $options)
43+
protected function addResourceDissociate(string $name, string $base, string $controller, array $options): Route
4444
{
45-
$uri = $this->getResourceUri($name).'/{'.$base.'?}/dissociate';
45+
$uri = $this->getNestedResourceUriWithNestedParameter($name, $base).'/dissociate';
4646

4747
$action = $this->getResourceAction($name, $controller, 'dissociate', $options);
4848

4949
return $this->router->delete($uri, $action);
5050
}
51-
}
51+
}

src/Http/Routing/RelationResourceRegistrar.php

+150-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,64 @@
66

77
class RelationResourceRegistrar extends ResourceRegistrar
88
{
9+
/**
10+
* Add the index method for a resourceful route.
11+
*
12+
* @param string $name
13+
* @param string $base
14+
* @param string $controller
15+
* @param array $options
16+
* @return Route
17+
*/
18+
protected function addResourceIndex($name, $base, $controller, $options): Route
19+
{
20+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base);
21+
22+
unset($options['missing']);
23+
24+
$action = $this->getResourceAction($name, $controller, 'index', $options);
25+
26+
return $this->router->get($uri, $action);
27+
}
28+
29+
/**
30+
* Add the search method for a resourceful route.
31+
*
32+
* @param string $name
33+
* @param string $base
34+
* @param string $controller
35+
* @param array $options
36+
* @return Route
37+
*/
38+
protected function addResourceSearch(string $name, string $base, string $controller, array $options): Route
39+
{
40+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/search';
41+
42+
$action = $this->getResourceAction($name, $controller, 'search', $options);
43+
44+
return $this->router->post($uri, $action);
45+
}
46+
47+
/**
48+
* Add the store method for a resourceful route.
49+
*
50+
* @param string $name
51+
* @param string $base
52+
* @param string $controller
53+
* @param array $options
54+
* @return Route
55+
*/
56+
protected function addResourceStore($name, $base, $controller, $options): Route
57+
{
58+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base);
59+
60+
unset($options['missing']);
61+
62+
$action = $this->getResourceAction($name, $controller, 'store', $options);
63+
64+
return $this->router->post($uri, $action);
65+
}
66+
967
/**
1068
* Add the update method for a resourceful route.
1169
*
@@ -17,7 +75,7 @@ class RelationResourceRegistrar extends ResourceRegistrar
1775
*/
1876
protected function addResourceUpdate($name, $base, $controller, $options): Route
1977
{
20-
$uri = $this->getResourceUri($name).'/{'.$base.'?}';
78+
$uri = $this->getNestedResourceUriWithNestedParameter($name, $base);
2179

2280
$action = $this->getResourceAction($name, $controller, 'update', $options);
2381

@@ -35,7 +93,7 @@ protected function addResourceUpdate($name, $base, $controller, $options): Route
3593
*/
3694
protected function addResourceShow($name, $base, $controller, $options): Route
3795
{
38-
$uri = $this->getResourceUri($name).'/{'.$base.'?}';
96+
$uri = $this->getNestedResourceUriWithNestedParameter($name, $base);
3997

4098
$action = $this->getResourceAction($name, $controller, 'show', $options);
4199

@@ -53,7 +111,7 @@ protected function addResourceShow($name, $base, $controller, $options): Route
53111
*/
54112
protected function addResourceDestroy($name, $base, $controller, $options): Route
55113
{
56-
$uri = $this->getResourceUri($name).'/{'.$base.'?}';
114+
$uri = $this->getNestedResourceUriWithNestedParameter($name, $base);
57115

58116
$action = $this->getResourceAction($name, $controller, 'destroy', $options);
59117

@@ -71,10 +129,97 @@ protected function addResourceDestroy($name, $base, $controller, $options): Rout
71129
*/
72130
protected function addResourceRestore(string $name, string $base, string $controller, array $options): Route
73131
{
74-
$uri = $this->getResourceUri($name).'/{'.$base.'?}/restore';
132+
$uri = $this->getNestedResourceUriWithNestedParameter($name, $base).'/restore';
75133

76134
$action = $this->getResourceAction($name, $controller, 'restore', $options);
77135

78136
return $this->router->post($uri, $action);
79137
}
80-
}
138+
139+
/**
140+
* Add the batch store method for a resourceful route.
141+
*
142+
* @param string $name
143+
* @param string $base
144+
* @param string $controller
145+
* @param array $options
146+
* @return Route
147+
*/
148+
protected function addResourceBatchStore(string $name, string $base, string $controller, array $options): Route
149+
{
150+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/batch';
151+
152+
$action = $this->getResourceAction($name, $controller, 'batchStore', $options);
153+
154+
return $this->router->post($uri, $action);
155+
}
156+
157+
/**
158+
* Add the batch update method for a resourceful route.
159+
*
160+
* @param string $name
161+
* @param string $base
162+
* @param string $controller
163+
* @param array $options
164+
* @return Route
165+
*/
166+
protected function addResourceBatchUpdate(string $name, string $base, string $controller, array $options): Route
167+
{
168+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/batch';
169+
170+
$action = $this->getResourceAction($name, $controller, 'batchUpdate', $options);
171+
172+
return $this->router->patch($uri, $action);
173+
}
174+
175+
/**
176+
* Add the batch destroy for a resourceful route.
177+
*
178+
* @param string $name
179+
* @param string $base
180+
* @param string $controller
181+
* @param array $options
182+
* @return Route
183+
*/
184+
protected function addResourceBatchDestroy(string $name, string $base, string $controller, array $options): Route
185+
{
186+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/batch';
187+
188+
$action = $this->getResourceAction($name, $controller, 'batchDestroy', $options);
189+
190+
return $this->router->delete($uri, $action);
191+
}
192+
193+
/**
194+
* Add the batch restore for a resourceful route.
195+
*
196+
* @param string $name
197+
* @param string $base
198+
* @param string $controller
199+
* @param array $options
200+
* @return Route
201+
*/
202+
protected function addResourceBatchRestore(string $name, string $base, string $controller, array $options): Route
203+
{
204+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base).'/batch/restore';
205+
206+
$action = $this->getResourceAction($name, $controller, 'batchRestore', $options);
207+
208+
return $this->router->post($uri, $action);
209+
}
210+
211+
protected function getNestedResourceUriWithNestedParameter(string $name, string $base): string
212+
{
213+
$uri = $this->getNestedResourceUriWithoutNestedParameter($name, $base);
214+
$uri .= "/{".$base."?}";
215+
216+
return $uri;
217+
}
218+
219+
protected function getNestedResourceUriWithoutNestedParameter(string $name, string $base): string
220+
{
221+
$uri = $this->getNestedResourceUri(explode('.', $name));
222+
223+
return rtrim(rtrim($uri, "\{$base\}"), '/');
224+
}
225+
}

tests/Unit/OrionTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ function () {
9191
$this->assertRouteRegistered('api.projects.batchRestore', ['POST'], 'api/projects/batch/restore', DummyController::class.'@batchRestore');
9292
}
9393

94+
/** @test */
95+
public function registering_relation_resource_with_the_same_name_as_parent(): void
96+
{
97+
Route::group(
98+
['as' => 'api.', 'prefix' => 'api'],
99+
function () {
100+
Orion::hasManyResource('projects', 'projects', DummyController::class);
101+
}
102+
);
103+
104+
$this->assertRouteRegistered('api.projects.projects.index', ['GET', 'HEAD'], 'api/projects/{project}/projects', DummyController::class.'@index');
105+
$this->assertRouteRegistered('api.projects.projects.search', ['POST'], 'api/projects/{project}/projects/search', DummyController::class.'@search');
106+
$this->assertRouteRegistered('api.projects.projects.store', ['POST'], 'api/projects/{project}/projects', DummyController::class.'@store');
107+
$this->assertRouteRegistered('api.projects.projects.show', ['GET', 'HEAD'], 'api/projects/{project}/projects/{project?}', DummyController::class.'@show');
108+
$this->assertRouteRegistered('api.projects.projects.update', ['PUT', 'PATCH'], 'api/projects/{project}/projects/{project?}', DummyController::class.'@update');
109+
$this->assertRouteRegistered('api.projects.projects.destroy', ['DELETE'], 'api/projects/{project}/projects/{project?}', DummyController::class.'@destroy');
110+
111+
$this->assertRouteRegistered('api.projects.projects.associate', ['POST'], 'api/projects/{project}/projects/associate', DummyController::class.'@associate');
112+
$this->assertRouteRegistered('api.projects.projects.dissociate', ['DELETE'], 'api/projects/{project}/projects/{project?}/dissociate', DummyController::class.'@dissociate');
113+
114+
$this->assertRouteRegistered('api.projects.projects.batchStore', ['POST'], 'api/projects/{project}/projects/batch', DummyController::class.'@batchStore');
115+
$this->assertRouteRegistered('api.projects.projects.batchUpdate', ['PATCH'], 'api/projects/{project}/projects/batch', DummyController::class.'@batchUpdate');
116+
$this->assertRouteRegistered('api.projects.projects.batchDestroy', ['DELETE'], 'api/projects/{project}/projects/batch', DummyController::class.'@batchDestroy');
117+
118+
$this->assertRouteNotRegistered('api.projects.projects.restore');
119+
$this->assertRouteNotRegistered('api.projects.projects.batchRestore');
120+
}
121+
94122
/** @test */
95123
public function registering_has_one_resource(): void
96124
{

0 commit comments

Comments
 (0)