@@ -30,7 +30,12 @@ public function analyze(array $tokens): array
30
30
$ controller = new Controller ($ name );
31
31
32
32
if ($ this ->isResource ($ definition )) {
33
+ $ original = $ definition ;
33
34
$ definition = $ this ->generateResourceTokens ($ controller , $ this ->methodsForResource ($ definition ['resource ' ]));
35
+ // unset shorthand
36
+ unset($ original ['resource ' ]);
37
+ // this gives the ability to both use a shorthand and override some methods
38
+ $ definition = array_merge ($ definition , $ original );
34
39
}
35
40
36
41
foreach ($ definition as $ method => $ body ) {
@@ -54,17 +59,18 @@ private function generateResourceTokens(Controller $controller, array $methods)
54
59
->filter (function ($ statements , $ method ) use ($ methods ) {
55
60
return in_array ($ method , $ methods );
56
61
})
57
- ->map (function ($ statements ) use ($ controller ) {
58
- return collect ( $ statements )
59
- ->map (function ($ statement ) use ($ controller ) {
62
+ ->mapWithKeys (function ($ statements, $ method ) use ($ controller ) {
63
+ return [
64
+ str_replace ( ' api. ' , '' , $ method ) => collect ( $ statements ) ->map (function ($ statement ) use ($ controller ) {
60
65
$ model = Str::singular ($ controller ->prefix ());
61
66
62
67
return str_replace (
63
68
['[singular] ' , '[plural] ' ],
64
69
[Str::lower ($ model ), Str::lower (Str::plural ($ model ))],
65
70
$ statement
66
71
);
67
- });
72
+ }),
73
+ ];
68
74
})
69
75
->toArray ();
70
76
}
@@ -74,40 +80,62 @@ private function resourceTokens()
74
80
return [
75
81
'index ' => [
76
82
'query ' => 'all:[plural] ' ,
77
- 'render ' => '[singular].index with [plural] '
83
+ 'render ' => '[singular].index with [plural] ' ,
78
84
],
79
85
'create ' => [
80
- 'render ' => '[singular].create '
86
+ 'render ' => '[singular].create ' ,
81
87
],
82
88
'store ' => [
83
89
'validate ' => '[singular] ' ,
84
90
'save ' => '[singular] ' ,
85
91
'flash ' => '[singular].id ' ,
86
- 'redirect ' => '[singular].index '
92
+ 'redirect ' => '[singular].index ' ,
87
93
],
88
94
'show ' => [
89
- 'render ' => '[singular].show with:[singular] '
95
+ 'render ' => '[singular].show with:[singular] ' ,
90
96
],
91
97
'edit ' => [
92
- 'render ' => '[singular].edit with:[singular] '
98
+ 'render ' => '[singular].edit with:[singular] ' ,
93
99
],
94
100
'update ' => [
95
101
'validate ' => '[singular] ' ,
96
102
'update ' => '[singular] ' ,
97
103
'flash ' => '[singular].id ' ,
98
- 'redirect ' => '[singular].index '
104
+ 'redirect ' => '[singular].index ' ,
99
105
],
100
106
'destroy ' => [
101
107
'delete ' => '[singular] ' ,
102
- 'redirect ' => '[singular].index '
103
- ]
108
+ 'redirect ' => '[singular].index ' ,
109
+ ],
110
+
111
+ 'api.index ' => [
112
+ 'query ' => 'all:[plural] ' ,
113
+ 'resource ' => 'collection:[plural] ' ,
114
+ ],
115
+ 'api.store ' => [
116
+ 'validate ' => '[singular] ' ,
117
+ 'save ' => '[singular] ' ,
118
+ 'resource ' => '[singular] ' ,
119
+ ],
120
+ 'api.show ' => [
121
+ 'resource ' => '[singular] ' ,
122
+ ],
123
+ 'api.update ' => [
124
+ 'validate ' => '[singular] ' ,
125
+ 'update ' => '[singular] ' ,
126
+ 'resource ' => '[singular] ' ,
127
+ ],
128
+ 'api.destroy ' => [
129
+ 'delete ' => '[singular] ' ,
130
+ 'respond ' => 200 ,
131
+ ],
104
132
];
105
133
}
106
134
107
135
private function methodsForResource (string $ type )
108
136
{
109
137
if ($ type === 'api ' ) {
110
- return ['index ' , 'store ' , 'show ' , 'update ' , 'destroy ' ];
138
+ return ['api. index ' , 'api. store ' , 'api. show ' , 'api. update ' , 'api. destroy ' ];
111
139
}
112
140
113
141
if ($ type === 'all ' ) {
0 commit comments