@@ -29,13 +29,13 @@ public function statusAction()
29
29
* Includes several checks in order to ensure the validity of the data provided as well
30
30
* as authentication.
31
31
*
32
- * @param $auth_key
32
+ * @param $authKey
33
33
* @param $version
34
34
* @return Response
35
35
*/
36
- public function handleRequestAction ($ auth_key , $ version )
36
+ public function handleRequestAction ($ authKey , $ version )
37
37
{
38
- if ($ auth_key !== $ this ->container ->getParameter ('auth_key ' ))
38
+ if ($ authKey !== $ this ->container ->getParameter ('auth_key ' ))
39
39
{
40
40
return new Response (json_encode (array ("success " => false , "message " => "Invalid authorization key. " )));
41
41
}
@@ -83,39 +83,38 @@ public function handleRequestAction($auth_key, $version)
83
83
*/
84
84
protected function compile ($ contents )
85
85
{
86
- $ apihandler = $ this ->get ('codebender_builder.handler ' );
86
+ $ apiHandler = $ this ->get ('codebender_builder.handler ' );
87
87
88
88
$ files = $ contents ["files " ];
89
89
90
- $ this ->checkForUserProject ($ files );
90
+ $ this ->checkForUserIdProjectId ($ files );
91
91
92
- $ userLibs = array ();
92
+ $ userLibraries = array ();
93
93
94
- if (array_key_exists ('libraries ' , $ contents ))
95
- $ userLibs = $ contents ['libraries ' ];
94
+ if (array_key_exists ('libraries ' , $ contents )) {
95
+ $ userLibraries = $ contents ['libraries ' ];
96
+ }
96
97
97
- $ parsedLibs = $ this ->checkHeaders ($ files , $ userLibs );
98
+ $ userAndLibmanLibraries = $ this ->returnProvidedAndFetchedLibraries ($ files , $ userLibraries );
98
99
99
- $ contents ["libraries " ] = $ parsedLibs ['libraries ' ];
100
+ $ contents ["libraries " ] = $ userAndLibmanLibraries ['libraries ' ];
100
101
101
- $ request_content = json_encode ($ contents );
102
+ $ compilerRequestContent = json_encode ($ contents );
102
103
103
104
// perform the actual post to the compiler
104
- $ data = $ apihandler -> post_raw_data ($ this ->container ->getParameter ('compiler ' ), $ request_content );
105
+ $ data = $ apiHandler -> postRawData ($ this ->container ->getParameter ('compiler ' ), $ compilerRequestContent );
105
106
106
- $ decoded = json_decode ($ data , true );
107
- if ($ decoded === NULL )
108
- {
107
+ $ decodedResponse = json_decode ($ data , true );
108
+ if ($ decodedResponse === NULL ) {
109
109
return json_encode (array ("success " => false , "message " => "Failed to get compiler response. " ));
110
110
}
111
111
112
- if ($ decoded ["success " ] === false && !array_key_exists ("step " , $ decoded ))
113
- {
114
- $ decoded ["step " ] = "unknown " ;
112
+ if ($ decodedResponse ["success " ] === false && !array_key_exists ("step " , $ decodedResponse )) {
113
+ $ decodedResponse ["step " ] = "unknown " ;
115
114
}
116
115
117
- unset($ parsedLibs ['libraries ' ]);
118
- $ decoded ['additionalCode ' ] = $ parsedLibs ;
116
+ unset($ userAndLibmanLibraries ['libraries ' ]);
117
+ $ decoded ['additionalCode ' ] = $ userAndLibmanLibraries ;
119
118
120
119
return json_encode ($ decoded );
121
120
}
@@ -134,90 +133,98 @@ protected function getLibraryInfo($data)
134
133
135
134
$ libraryManager = $ this ->container ->getParameter ('library ' );
136
135
137
- return $ handler ->post_raw_data ($ libraryManager , $ data );
136
+ return $ handler ->postRawData ($ libraryManager , $ data );
138
137
}
139
138
140
139
/**
141
140
*
142
- * @param array $files
143
- * @param array $userLibs
141
+ * @param array $projectFiles
142
+ * @param array $userLibraries
144
143
*
145
144
* @return array
146
145
*/
147
- protected function checkHeaders ( $ files , $ userLibs )
146
+ protected function returnProvidedAndFetchedLibraries ( $ projectFiles , $ userLibraries )
148
147
{
149
148
$ apiHandler = $ this ->get ('codebender_builder.handler ' );
150
149
151
- $ headers = $ apiHandler ->read_libraries ( $ files );
150
+ $ detectedHeaders = $ apiHandler ->readLibraries ( $ projectFiles );
152
151
153
152
// declare arrays
154
- $ libraries = $ notFoundHeaders = $ foundHeaders = $ fetchedLibs = $ providedLibs = array ();
155
-
156
- $ providedLibs = array_keys ( $ userLibs );
157
-
158
- $ libraries = $ userLibs ;
159
-
160
- foreach ($ headers as $ header ) {
161
-
162
- $ exists_in_request = false ;
163
- foreach ($ userLibs as $ lib ) {
164
- foreach ($ lib as $ libcontent ) {
165
- if ($ libcontent ["filename " ] == $ header .".h " ) {
166
- $ exists_in_request = true ;
153
+ $ notFoundHeaders = array ();
154
+ $ foundHeaders = array ();
155
+ $ librariesFromLibman = array ( );
156
+ $ providedLibraries = array_keys ( $ userLibraries );
157
+ $ libraries = $ userLibraries ;
158
+
159
+ foreach ($ detectedHeaders as $ header ) {
160
+
161
+ $ existsInRequest = false ;
162
+ foreach ($ userLibraries as $ library ) {
163
+ foreach ($ library as $ libraryContent ) {
164
+ if ($ libraryContent ["filename " ] == $ header .".h " ) {
165
+ $ existsInRequest = true ;
167
166
$ foundHeaders [] = $ header . ".h " ;
168
167
}
169
168
}
170
169
}
171
- if ($ exists_in_request === false ) {
172
-
173
- $ requestContent = array ("type " => "fetch " , "library " => $ header );
174
- $ data = $ this ->getLibraryInfo (json_encode ($ requestContent ));
175
- $ data = json_decode ($ data , true );
176
-
177
- if ($ data ["success " ]) {
178
- $ fetchedLibs [] = $ header ;
179
- $ files_to_add = array ();
180
- foreach ($ data ["files " ] as $ file ){
181
- if (in_array (pathinfo ($ file ['filename ' ], PATHINFO_EXTENSION ), array ('cpp ' , 'h ' , 'c ' , 'S ' , 'inc ' )))
182
- $ files_to_add [] = $ file ;
183
- }
184
170
185
- $ libraries [$ header ] = $ files_to_add ;
186
- $ foundHeaders [] = $ header . ".h " ;
171
+ if ($ existsInRequest === true ) {
172
+ continue ;
173
+ }
174
+ $ requestContent = array ("type " => "fetch " , "library " => $ header );
175
+ $ data = $ this ->getLibraryInfo (json_encode ($ requestContent ));
176
+ $ data = json_decode ($ data , true );
177
+
178
+ if ($ data ['success ' ] === false ) {
179
+ $ notFoundHeaders [] = $ header . ".h " ;
180
+ continue ;
181
+ }
187
182
188
- } elseif (!$ data ['success ' ]){
189
- $ notFoundHeaders [] = $ header . ".h " ;
190
- }
183
+ $ foundHeaders [] = $ header . ".h " ;
184
+ $ librariesFromLibman [] = $ header ;
185
+ $ filesToBeAdded = array ();
186
+ foreach ($ data ["files " ] as $ file ) {
187
+ if (in_array (pathinfo ($ file ['filename ' ], PATHINFO_EXTENSION ), array ('cpp ' , 'h ' , 'c ' , 'S ' , 'inc ' )))
188
+ $ filesToBeAdded [] = $ file ;
191
189
}
190
+ $ libraries [$ header ] = $ filesToBeAdded ;
192
191
}
193
192
194
193
return array (
195
194
'libraries ' => $ libraries ,
196
- 'providedLibraries ' => $ providedLibs ,
197
- 'fetchedLibraries ' => $ fetchedLibs ,
198
- 'detectedHeaders ' => $ headers ,
195
+ 'providedLibraries ' => $ providedLibraries ,
196
+ 'fetchedLibraries ' => $ librariesFromLibman ,
197
+ 'detectedHeaders ' => $ detectedHeaders ,
199
198
'foundHeaders ' => $ foundHeaders ,
200
199
'notFoundHeaders ' => $ notFoundHeaders );
201
200
}
202
201
203
202
/**
204
- *
205
- * @param array $files
206
203
* Checks if project id and user id txt files exist in the request files.
207
204
* If not, creates these files with null id
208
- *
205
+ *
206
+ * @param array $projectFiles
209
207
*/
210
- protected function checkForUserProject (&$ files )
208
+ protected function checkForUserIdProjectId (&$ projectFiles )
211
209
{
212
- $ foundProj = $ foundUsr = false ;
210
+ $ foundProject = false ;
211
+ $ foundUser = false ;
213
212
214
- foreach ($ files as $ file ) {
215
- if (preg_match ('/(?<=user_)[\d]+/ ' , $ file ['filename ' ])) $ foundUsr = true ;
216
- if (preg_match ('/(?<=project_)[\d]+/ ' , $ file ['filename ' ])) $ foundProj = true ;
213
+ foreach ($ projectFiles as $ file ) {
214
+ if (preg_match ('/(?<=user_)[\d]+/ ' , $ file ['filename ' ])) {
215
+ $ foundUser = true ;
216
+ }
217
+ if (preg_match ('/(?<=project_)[\d]+/ ' , $ file ['filename ' ])) {
218
+ $ foundProject = true ;
219
+ }
217
220
}
218
221
219
- if (!$ foundUsr ) $ files [] = array ('filename ' => 'user_null.txt ' , 'content ' => '' );
220
- if (!$ foundProj ) $ files [] = array ('filename ' => 'project_null.txt ' , 'content ' => '' );
222
+ if (!$ foundUser ) {
223
+ $ projectFiles [] = array ('filename ' => 'user_null.txt ' , 'content ' => '' );
224
+ }
225
+ if (!$ foundProject ) {
226
+ $ projectFiles [] = array ('filename ' => 'project_null.txt ' , 'content ' => '' );
227
+ }
221
228
}
222
229
}
223
230
0 commit comments