@@ -22,49 +22,6 @@ public function statusAction()
22
22
return new Response (json_encode (array ("success " => true , "status " => "OK " )));
23
23
}
24
24
25
- /**
26
- * compileWebsite action
27
- *
28
- * @return Response Response intance.
29
- *
30
- */
31
- public function compileWebsiteAction ($ auth_key , $ version )
32
- {
33
-
34
- if ($ auth_key !== $ this ->container ->getParameter ('auth_key ' ))
35
- {
36
- return new Response (json_encode (array ("success " => false , "step " => 0 , "message " => "Invalid authorization key. " )));
37
- }
38
-
39
- if ($ version !== $ this ->container ->getParameter ('version ' ))
40
- {
41
- return new Response (json_encode (array ("success " => false , "step " => 0 , "message " => "Invalid api version. " )));
42
- }
43
-
44
- $ request = $ this ->getRequest ()->getContent ();
45
-
46
- $ contents = json_decode ($ request , true );
47
-
48
- $ apihandler = $ this ->get ('codebender_api.handler ' );
49
-
50
- $ personalMatchedLibs = $ contents ['libraries ' ];
51
-
52
- $ files = $ contents ["files " ];
53
-
54
- $ headersArr = $ this ->checkHeaders ($ files , $ personalMatchedLibs );
55
-
56
- $ contents ["libraries " ] = $ headersArr ['libraries ' ];
57
- $ request_content = json_encode ($ contents );
58
-
59
- // perform the actual post to the compiler
60
- $ data = $ apihandler ->post_raw_data ($ this ->container ->getParameter ('compiler ' ), $ request_content );
61
- $ decoded_compiler_data = json_decode ($ data , true );
62
-
63
- $ responsedata = array ('success ' => true , 'personal ' => array_keys ($ personalMatchedLibs ), 'library ' => $ headersArr ['libraries ' ], 'foundFiles ' => $ headersArr ['foundFiles ' ], 'notFoundHeaders ' => $ headersArr ['notFoundHeaders ' ], 'compileResponse ' => $ decoded_compiler_data );
64
-
65
- return new Response (json_encode ($ responsedata ));
66
- }
67
-
68
25
/**
69
26
* compile action
70
27
*
@@ -76,85 +33,135 @@ public function compileAction($auth_key, $version)
76
33
77
34
if ($ auth_key !== $ this ->container ->getParameter ('auth_key ' ))
78
35
{
79
- return new Response (json_encode (array ("success " => false , "step " => 0 , " message " => "Invalid authorization key. " )));
36
+ return new Response (json_encode (array ("success " => false , "message " => "Invalid authorization key. " )));
80
37
}
81
38
82
39
if ($ version !== $ this ->container ->getParameter ('version ' ))
83
40
{
84
- return new Response (json_encode (array ("success " => false , "step " => 0 , " message " => "Invalid api version. " )));
41
+ return new Response (json_encode (array ("success " => false , "message " => "Invalid api version. " )));
85
42
}
86
43
87
44
$ request = $ this ->getRequest ()->getContent ();
45
+ if (empty ($ request ))
46
+ {
47
+ return new Response (json_encode (array ("success " => false , "message " => "Invalid input. " )));
48
+ }
88
49
89
50
$ contents = json_decode ($ request , true );
90
51
91
52
$ apihandler = $ this ->get ('codebender_api.handler ' );
92
53
93
54
$ files = $ contents ["files " ];
94
55
56
+ $ this ->checkForUserProject ($ files );
57
+
95
58
$ userlibs = array ();
59
+
96
60
if (array_key_exists ('libraries ' , $ contents ))
97
61
$ userlibs = $ contents ['libraries ' ];
98
62
99
- $ headersArr = $ this ->checkHeaders ($ files , $ userlibs );
63
+ $ parsedLibs = $ this ->checkHeaders ($ files , $ userlibs );
100
64
101
- $ contents ['files ' ][] = array ('filename ' => 'user_null.txt ' , 'content ' => '' );
102
- $ contents ['files ' ][] = array ('filename ' => 'null.txt ' , 'content ' => '' );
65
+ $ contents ["libraries " ] = $ parsedLibs ['libraries ' ];
103
66
104
- $ contents ["libraries " ] = $ headersArr ['libraries ' ];
105
67
$ request_content = json_encode ($ contents );
106
68
107
69
// perform the actual post to the compiler
108
70
$ data = $ apihandler ->post_raw_data ($ this ->container ->getParameter ('compiler ' ), $ request_content );
109
71
110
- return new Response ($ data );
72
+ $ decoded = json_decode ($ data , true );
73
+ if ($ decoded == null )
74
+ {
75
+ return new Response (json_encode (array ("success " => false , "message " => "Failed to get compiler response. " )));
76
+ }
77
+
78
+ if ($ decoded ["success " ] === false && !array_key_exists ("step " , $ decoded ))
79
+ $ decoded ["step " ] = "unknown " ;
80
+
81
+ unset($ parsedLibs ['libraries ' ]);
82
+ $ decoded ['additionalCode ' ] = $ parsedLibs ;
83
+
84
+ return new Response (json_encode ($ decoded ));
111
85
}
112
86
113
87
/**
114
88
*
115
- * @param type $files
116
- * @param array $personalLibs
89
+ * @param array $files
90
+ * @param array $userLibs
117
91
*
118
- * @return type
92
+ * @return array
119
93
*/
120
- protected function checkHeaders ($ files , array $ personalLibs )
94
+ protected function checkHeaders ($ files , array $ userlibs )
121
95
{
122
96
$ apiHandler = $ this ->get ('codebender_api.handler ' );
123
97
124
98
$ headers = $ apiHandler ->read_libraries ($ files );
125
99
126
100
// declare arrays
127
- $ libraries = $ notFoundHeaders = $ foundFiles = array ();
101
+ $ libraries = $ notFoundHeaders = $ foundHeaders = $ fetchedLibs = $ providedLibs = array ();
102
+
103
+ $ providedLibs = array_keys ($ userlibs );
128
104
129
105
// get library manager url
130
106
$ libmanager_url = $ this ->container ->getParameter ('library ' );
131
107
132
- $ libraries = $ personalLibs ;
108
+ $ libraries = $ userlibs ;
133
109
134
110
foreach ($ headers as $ header ) {
135
- $ foundPersonal = false ;
136
- foreach ($ personalLibs as $ plibrary ){
137
- foreach ($ plibrary as $ libcontent ){
138
- if ($ libcontent ["filename " ] == $ header .".h " )
139
- $ foundPersonal = true ;
111
+
112
+ $ exists_in_request = false ;
113
+ foreach ($ userlibs as $ lib ){
114
+ foreach ($ lib as $ libcontent ){
115
+ if ($ libcontent ["filename " ] == $ header .".h " ) {
116
+ $ exists_in_request = true ;
117
+ $ foundHeaders [] = $ header . ".h " ;
118
+ }
140
119
}
141
120
}
142
- if ($ foundPersonal === false ) {
121
+ if ($ exists_in_request === false ) {
143
122
144
123
$ data = $ apiHandler ->get ($ libmanager_url . "/fetch?library= " . urlencode ($ header ));
145
124
$ data = json_decode ($ data , true );
146
125
147
126
if ($ data ["success " ]) {
127
+ $ fetchedLibs [] = $ header ;
128
+
148
129
$ libraries [$ header ] = $ data ["files " ];
149
- foreach ($ data ['files ' ] as $ file ) {
150
- $ foundFiles [] = $ file ['filename ' ];
151
- }
130
+ $ foundHeaders [] = $ header . ".h " ;
131
+
152
132
} elseif (!$ data ['success ' ]){
153
133
$ notFoundHeaders [] = $ header . ".h " ;
154
134
}
155
135
}
156
136
}
157
- return array ('libraries ' => $ libraries , 'foundFiles ' => $ foundFiles , 'notFoundHeaders ' => $ notFoundHeaders );
137
+
138
+ return array (
139
+ 'libraries ' => $ libraries ,
140
+ 'providedLibraries ' => $ providedLibs ,
141
+ 'fetchedLibraries ' => $ fetchedLibs ,
142
+ 'detectedHeaders ' => $ headers ,
143
+ 'foundHeaders ' => $ foundHeaders ,
144
+ 'notFoundHeaders ' => $ notFoundHeaders );
145
+ }
146
+
147
+ /**
148
+ *
149
+ * @param array $files
150
+ * Checks if project id and user id txt files exist in the request files.
151
+ * If not, creates these files with null id
152
+ *
153
+ */
154
+ protected function checkForUserProject (&$ files )
155
+ {
156
+ $ foundProj = $ foundUsr = false ;
157
+
158
+ foreach ($ files as $ file ) {
159
+ if (preg_match ('/(?<=user_)[\d]+/ ' , $ file ['filename ' ])) $ foundUsr = true ;
160
+ if (preg_match ('/(?<=project_)[\d]+/ ' , $ file ['filename ' ])) $ foundProj = true ;
161
+ }
162
+
163
+ if (!$ foundUsr ) $ files [] = array ('filename ' => 'user_null.txt ' , 'content ' => '' );
164
+ if (!$ foundProj ) $ files [] = array ('filename ' => 'project_null.txt ' , 'content ' => '' );
158
165
}
159
166
}
160
167
0 commit comments