8000 Fix for methods in relationship routes. Add support of options "only"… · awolad/laravel-json-api@bd745d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd745d1

Browse files
committed
Fix for methods in relationship routes. Add support of options "only" and "except" in resource routes
1 parent c68eb35 commit bd745d1

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

src/Routing/ResourceRegistrar.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,30 @@ class ResourceRegistrar
3737
*/
3838
protected $router;
3939

40+
/**
41+
* @type array
42+
*/
43+
protected $resourceDefaults;
44+
45+
/**
46+
* @type array
47+
*/
48+
protected $rootUrlMethods = ['index', 'create'];
49+
4050
/**
4151
* @param Registrar $router
4252
*/
4353
public function __construct(Registrar $router)
4454
{
4555
$this->router = $router;
56+
57+
$this->resourceDefaults = [
58+
'index' => 'get',
59+
'create' => 'post',
60+
'read' => 'get',
61+
'update' => 'patch',
62+
'delete' => 'delete',
63+
];
4664
}
4765

4866
/**
@@ -58,7 +76,7 @@ public function resource($name, $controller, array $options = [])
5876
$hasOne = isset($options[static::HAS_ONE]) ? (array) $options[static::HAS_ONE] : [];
5977
$hasMany = isset($options[static::HAS_MANY]) ? (array) $options[static::HAS_MANY] : [];
6078

61-
$this->registerResource($rootUrl, $objectUrl, $controller)
79+
$this->registerResource($rootUrl, $objectUrl, $controller, $options)
6280
->registerHasOne($objectUrl, $controller, $hasOne)
6381
->registerHasMany($objectUrl, $controller, $hasMany);
6482
}
@@ -67,15 +85,19 @@ public function resource($name, $controller, array $options = [])
6785
* @param $rootUrl
6886
* @param $objectUrl
6987
* @param $controller
88+
* @param $options
7089
* @return $this
7190
*/
72-
private function registerResource($rootUrl, $objectUrl, $controller)
91+
private function registerResource($rootUrl, $objectUrl, $controller, $options)
7392
{
74-
$this->router->get($rootUrl, $controller . '@index');
75-
$this->router->post($rootUrl, $controller . '@create');
76-
$this->router->get($objectUrl, $controller . '@read');
77-
$this->router->patch($objectUrl, $controller . '@update');
78-
$this->router->delete($objectUrl, $controller . '@delete');
93+
foreach ($this->getResourceMethods($options) as $method) {
94+
$url = in_array($method,
95+
$this->rootUrlMethods) ? $rootUrl : $objectUrl;
96+
call_user_func_array(
97+
[$this->router, $this->resourceDefaults[$method],],
98+
[$url, $controller . '@' . $method,]
99+
);
100+
}
79101

80102
return $this;
81103
}
@@ -116,10 +138,27 @@ private function registerHasMany($objectUrl, $controller, array $relations)
116138

117139
$this->router->get($related, sprintf('%s@read%s', $controller, $name));
118140
$this->router->get($identifier, sprintf('%s@read%sRelationship', $controller, $name));
119-
$this->router->patch($identifier, sprintf('%s@read%sRelationship', $controller, $name));
120-
$this->router->delete($identifier, sprintf('%s@read%sRelationship', $controller, $name));
141+
$this->router->patch($identifier, sprintf('%s@update%sRelationship', $controller, $name));
142+
$this->router->delete($identifier, sprintf('%s@delete%sRelationship', $controller, $name));
121143
}
122144

123145
return $this;
124146
}
147+
148+
/**
149+
* Get the applicable resource methods.
150+
*
151+
* @param $options
152+
* @return array
153+
*/
154+
protected function getResourceMethods($options)
155+
{
156+
$defaults = array_keys($this->resourceDefaults);
157+
if (isset($options['only'])) {
158+
return array_intersect($defaults, (array) $options['only']);
159+
} elseif (isset($options['except'])) {
160+
return array_diff($defaults, (array) $options['except']);
161+
}
162+
return $defaults;
163+
}
125164
}

0 commit comments

Comments
 (0)
0