4
4
5
5
use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
6
6
use Symfony \Component \HttpFoundation \JsonResponse ;
7
- use Symfony \Component \HttpFoundation \Response ;
8
7
9
8
/**
10
9
* default controller of api bundle
11
10
*/
12
11
class DefaultController extends Controller
13
12
{
13
+ /* @var array $additionalCode */
14
+ protected $ additionalCode = [];
15
+
14
16
/**
15
17
* status action
16
18
*
@@ -26,36 +28,15 @@ public function statusAction()
26
28
* Gets a request for compilation or library fetching (depends on the 'type' field of the request)
27
29
* and passes the request to either the compiler or the library manager.
28
30
*
29
- * Includes several checks in order to ensure the validity of the data provided as well
30
- * as authentication.
31
- *
32
- * @param $authKey
33
- * @param $version
34
31
* @return JsonResponse
35
32
*/
36
- public function handleRequestAction ($ authKey , $ version )
33
+ public function handleRequestAction ()
37
34
{
38
- if ($ authKey !== $ this ->container ->getParameter ('authorizationKey ' )) {
39
- return new JsonResponse (['success ' => false , 'message ' => 'Invalid authorization key. ' ]);
40
- }
35
+ $ contents = json_decode ($ this ->getRequest ()->getContent (), true );
41
36
42
- if ($ version !== $ this ->container ->getParameter ('version ' )) {
43
- return new JsonResponse (['success ' => false , 'message ' => 'Invalid api version. ' ]);
44
- }
45
-
46
- $ request = $ this ->getRequest ()->getContent ();
47
- if (empty ($ request )) {
48
- return new JsonResponse (['success ' => false , 'message ' => 'Invalid input. ' ]);
49
- }
50
-
51
- $ contents = json_decode ($ request , true );
52
-
53
- if (json_last_error () !== JSON_ERROR_NONE ) {
54
- return new JsonResponse (['success ' => false , 'message ' => 'Wrong data. ' ]);
55
- }
56
-
57
- if (!array_key_exists ('data ' , $ contents )) {
58
- return new JsonResponse (['success ' => false , 'message ' => 'Insufficient data provided. ' ]);
37
+ $ isContentValid = $ this ->isContentValid ($ contents );
38
+ if ($ isContentValid ['success ' ] !== true ) {
39
+ return new JsonResponse (['success ' => false , 'message ' => $ isContentValid ['error ' ]]);
59
40
}
60
41
61
42
if ($ contents ['type ' ] == 'compiler ' ) {
@@ -72,6 +53,15 @@ public function handleRequestAction($authKey, $version)
72
53
]);
73
54
}
74
55
56
+ protected function isContentValid ($ requestContent )
57
+ {
58
+ if (!array_key_exists ('data ' , $ requestContent )) {
59
+ return ['success ' => false , 'error ' => 'Insufficient data provided. ' ];
60
+ }
61
+
62
+ return ['success ' => true ];
63
+ }
64
+
75
65
/**
76
66
* Gets the data from the handleRequestAction and proceeds with the compilation
77
67
*
@@ -84,23 +74,11 @@ protected function compile($contents)
84
74
{
85
75
$ apiHandler = $ this ->get ('codebender_builder.handler ' );
86
76
87
- $ contents = $ this ->addUserIdProjectIdIfNotInRequest ($ contents );
88
-
89
- $ files = $ contents ['files ' ];
90
-
91
- $ userLibraries = [];
92
-
93
- if (array_key_exists ('libraries ' , $ contents )) {
94
- $ userLibraries = $ contents ['libraries ' ];
95
- }
96
-
97
- $ userAndLibmanLibraries = $ this ->returnProvidedAndFetchedLibraries ($ files , $ userLibraries );
98
-
99
- $ contents ['libraries ' ] = $ userAndLibmanLibraries ['libraries ' ];
77
+ $ contents = $ this ->generateCompilerPayload ($ contents );
100
78
101
79
$ compilerRequestContent = json_encode ($ contents );
102
80
103
- // perform the actual post to the compiler
81
+ // perform the actual request to the compiler
104
82
$ data = $ apiHandler ->postRawData ($ this ->container ->getParameter ('compiler ' ), $ compilerRequestContent );
105
83
106
84
$ decodedResponse = json_decode ($ data , true );
@@ -112,8 +90,8 @@ protected function compile($contents)
112
90
$ decodedResponse ['step ' ] = 'unknown ' ;
113
91
}
114
92
115
- unset( $ userAndLibmanLibraries [ ' libraries ' ]) ;
116
- $ decodedResponse [ ' additionalCode ' ] = $ userAndLibmanLibraries ;
93
+ $ decodedResponse [ ' additionalCode ' ] = $ this -> additionalCode ;
94
+ $ this -> additionalCode = [] ;
117
95
118
96
return $ decodedResponse ;
119
97
}
@@ -195,14 +173,66 @@ protected function returnProvidedAndFetchedLibraries($projectFiles, $userLibrari
195
173
$ libraries [$ header ] = $ filesToBeAdded ;
196
174
}
197
175
198
- return [
199
- ' libraries ' => $ libraries ,
176
+ // store info about libraries and headers in the `additionalCode` class property;
177
+ $ this -> additionalCode = [
200
178
'providedLibraries ' => $ providedLibraries ,
201
179
'fetchedLibraries ' => $ librariesFromLibman ,
202
180
'detectedHeaders ' => $ detectedHeaders ,
203
181
'foundHeaders ' => $ foundHeaders ,
204
182
'notFoundHeaders ' => $ notFoundHeaders
205
183
];
184
+
185
+ return $ libraries ;
186
+ }
187
+
188
+ /**
189
+ * Returns the payload used for compilation. Parses projects files and libraries
190
+ * already existing in request and fetches any necessary libraries from eratosthenes.
191
+ *
192
+ * @return JsonResponse
193
+ */
194
+ public function generatePayloadAction ()
195
+ {
196
+ $ providedPayload = json_decode ($ this ->getRequest ()->getContent (), true );
197
+
198
+ $ payload = $ this ->generateCompilerPayload ($ providedPayload );
199
+ if (empty ($ payload )) {
200
+ return new JsonResponse (['success ' => false , 'message ' => 'Invalid compilation payload provided. ' ]);
201
+ }
202
+
203
+ $ payload ['success ' ] = true ;
204
+ $ payload ['additionalCode ' ] = $ this ->additionalCode ;
205
+ $ this ->additionalCode = [];
206
+
207
+ return new JsonResponse ($ payload );
208
+ }
209
+
210
+ /**
211
+ * Returns the payload of a compilation request. Sketch files and board-related
212
+ * data (build, core, variant) must be provided in the initial payload.
213
+ *
214
+ * @param array $providedData
215
+ * @return array
216
+ */
217
+ protected function generateCompilerPayload (array $ providedData )
218
+ {
219
+ $ payload = $ this ->addUserIdProjectIdIfNotInRequest ($ providedData );
220
+
221
+ if (!array_key_exists ('files ' , $ payload )) {
222
+ return [];
223
+ }
224
+
225
+ $ files = $ payload ['files ' ];
226
+
227
+ $ userLibraries = [];
228
+
229
+ if (array_key_exists ('libraries ' , $ payload )) {
230
+ $ userLibraries = $ payload ['libraries ' ];
231
+ }
232
+
233
+ $ payload ['libraries ' ] = $ this ->returnProvidedAndFetchedLibraries ($ files , $ userLibraries );
234
+
235
+ return $ payload ;
206
236
}
207
237
208
238
/**
0 commit comments