@@ -37,12 +37,30 @@ class ResourceRegistrar
37
37
*/
38
38
protected $ router ;
39
39
40
+ /**
41
+ * @type array
42
+ */
43
+ protected $ resourceDefaults ;
44
+
45
+ /**
46
+ * @type array
47
+ */
48
+ protected $ rootUrlMethods = ['index ' , 'create ' ];
49
+
40
50
/**
41
51
* @param Registrar $router
42
52
*/
43
53
public function __construct (Registrar $ router )
44
54
{
45
55
$ this ->router = $ router ;
56
+
57
+ $ this ->resourceDefaults = [
58
+ 'index ' => 'get ' ,
59
+ 'create ' => 'post ' ,
60
+ 'read ' => 'get ' ,
61
+ 'update ' => 'patch ' ,
62
+ 'delete ' => 'delete ' ,
63
+ ];
46
64
}
47
65
48
66
/**
@@ -58,7 +76,7 @@ public function resource($name, $controller, array $options = [])
58
76
$ hasOne = isset ($ options [static ::HAS_ONE ]) ? (array ) $ options [static ::HAS_ONE ] : [];
59
77
$ hasMany = isset ($ options [static ::HAS_MANY ]) ? (array ) $ options [static ::HAS_MANY ] : [];
60
78
61
- $ this ->registerResource ($ rootUrl , $ objectUrl , $ controller )
79
+ $ this ->registerResource ($ rootUrl , $ objectUrl , $ controller, $ options )
62
80
->registerHasOne ($ objectUrl , $ controller , $ hasOne )
63
81
->registerHasMany ($ objectUrl , $ controller , $ hasMany );
64
82
}
@@ -67,15 +85,19 @@ public function resource($name, $controller, array $options = [])
67
85
* @param $rootUrl
68
86
* @param $objectUrl
69
87
* @param $controller
88
+ * @param $options
70
89
* @return $this
71
90
*/
72
- private function registerResource ($ rootUrl , $ objectUrl , $ controller )
91
+ private function registerResource ($ rootUrl , $ objectUrl , $ controller, $ options )
73
92
{
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
+ }
79
101
80
102
return $ this ;
81
103
}
@@ -116,10 +138,27 @@ private function registerHasMany($objectUrl, $controller, array $relations)
116
138
117
139
$ this ->router ->get ($ related , sprintf ('%s@read%s ' , $ controller , $ name ));
118
140
$ 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 ));
121
143
}
122
144
123
145
return $ this ;
124
146
}
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
+ }
125
164
}
0 commit comments