@@ -23,14 +23,18 @@ public function statusAction()
23
23
}
24
24
25
25
/**
26
- * compile action
26
+ * Gets a request for compilation or library fetching (depends on the 'type' field of the request)
27
+ * and passes the request to either the compiler or the library manager.
27
28
*
28
- * @return Response Response intance.
29
+ * Includes several checks in order to ensure the validity of the data provided as well
30
+ * as authentication.
29
31
*
32
+ * @param $auth_key
33
+ * @param $version
34
+ * @return Response
30
35
*/
31
- public function compileAction ($ auth_key , $ version )
36
+ public function handleRequestAction ($ auth_key , $ version )
32
37
{
33
-
34
38
if ($ auth_key !== $ this ->container ->getParameter ('auth_key ' ))
35
39
{
36
40
return new Response (json_encode (array ("success " => false , "message " => "Invalid authorization key. " )));
@@ -49,18 +53,48 @@ public function compileAction($auth_key, $version)
49
53
50
54
$ contents = json_decode ($ request , true );
51
55
56
+ if ($ contents === NULL )
57
+ {
58
+ return new Response (json_encode (array ("success " => false , "message " => "Wrong data. " )));
59
+ }
60
+
61
+ if (!array_key_exists ("data " , $ contents ))
62
+ {
63
+ return new Response (json_encode (array ("success " => false , "message " => "Insufficient data provided. " )));
64
+ }
65
+
66
+ if ($ contents ["type " ] == "compiler " ) {
67
+ return new Response ($ this ->compile ($ contents ["data " ]));
68
+ }
69
+
70
+ if ($ contents ["type " ] == "library " )
71
+ {
72
+ return new Response ($ this ->getLibraryInfo (json_encode ($ contents ["data " ])));
73
+ }
74
+
75
+ return new Response (json_encode (array ("success " => false , "message " => "Invalid request type (can handle only 'compiler' or 'library' requests) " )));
76
+ }
77
+
78
+ /**
79
+ * Gets the data from the handleRequestAction and proceeds with the compilation
80
+ *
81
+ * @param $contents
82
+ * @return Response
83
+ */
84
+ protected function compile ($ contents )
85
+ {
52
86
$ apihandler = $ this ->get ('codebender_api.handler ' );
53
87
54
88
$ files = $ contents ["files " ];
55
89
56
90
$ this ->checkForUserProject ($ files );
57
91
58
- $ userlibs = array ();
92
+ $ userLibs = array ();
59
93
60
94
if (array_key_exists ('libraries ' , $ contents ))
61
- $ userlibs = $ contents ['libraries ' ];
95
+ $ userLibs = $ contents ['libraries ' ];
62
96
63
- $ parsedLibs = $ this ->checkHeaders ($ files , $ userlibs );
97
+ $ parsedLibs = $ this ->checkHeaders ($ files , $ userLibs );
64
98
65
99
$ contents ["libraries " ] = $ parsedLibs ['libraries ' ];
66
100
@@ -70,18 +104,37 @@ public function compileAction($auth_key, $version)
70
104
$ data = $ apihandler ->post_raw_data ($ this ->container ->getParameter ('compiler ' ), $ request_content );
71
105
72
106
$ decoded = json_decode ($ data , true );
73
- if ($ decoded == null )
107
+ if ($ decoded === NULL )
74
108
{
75
- return new Response ( json_encode (array ("success " => false , "message " => "Failed to get compiler response. " ) ));
109
+ return json_encode (array ("success " => false , "message " => "Failed to get compiler response. " ));
76
110
}
77
111
78
112
if ($ decoded ["success " ] === false && !array_key_exists ("step " , $ decoded ))
113
+ {
79
114
$ decoded ["step " ] = "unknown " ;
115
+ }
80
116
81
117
unset($ parsedLibs ['libraries ' ]);
82
118
$ decoded ['additionalCode ' ] = $ parsedLibs ;
83
119
84
- return new Response (json_encode ($ decoded ));
120
+ return json_encode ($ decoded );
121
+ }
122
+
123
+ /**
124
+ * Gets a request for library information from the handleRequestAction and makes the
125
+ * actual call to the library manager.
126
+ * The data must be already json encoded before passing them to this function.
127
+ *
128
+ * @param $data
129
+ * @return mixed
130
+ */
131
+ protected function getLibraryInfo ($ data )
132
+ {
133
+ $ handler = $ this ->get ('codebender_api.handler ' );
134
+
135
+ $ libraryManager = $ this ->container ->getParameter ('library ' );
136
+
137
+ return $ handler ->post_raw_data ($ libraryManager , $ data );
85
138
}
86
139
87
140
/**
@@ -91,7 +144,7 @@ public function compileAction($auth_key, $version)
91
144
*
92
145
* @return array
93
146
*/
94
- protected function checkHeaders ($ files , array $ userlibs )
147
+ protected function checkHeaders ($ files , $ userLibs )
95
148
{
96
149
$ apiHandler = $ this ->get ('codebender_api.handler ' );
97
150
@@ -100,17 +153,14 @@ protected function checkHeaders($files, array $userlibs)
100
153
// declare arrays
101
154
$ libraries = $ notFoundHeaders = $ foundHeaders = $ fetchedLibs = $ providedLibs = array ();
102
155
103
- $ providedLibs = array_keys ($ userlibs );
104
-
105
- // get library manager url
106
- $ libmanager_url = $ this ->container ->getParameter ('library ' );
156
+ $ providedLibs = array_keys ($ userLibs );
107
157
108
- $ libraries = $ userlibs ;
158
+ $ libraries = $ userLibs ;
109
159
110
160
foreach ($ headers as $ header ) {
111
161
112
162
$ exists_in_request = false ;
113
- foreach ($ userlibs as $ lib ){
163
+ foreach ($ userLibs as $ lib ){
114
164
foreach ($ lib as $ libcontent ){
115
165
if ($ libcontent ["filename " ] == $ header .".h " ) {
116
166
$ exists_in_request = true ;
@@ -120,7 +170,8 @@ protected function checkHeaders($files, array $userlibs)
120
170
}
121
171
if ($ exists_in_request === false ) {
122
172
123
- $ data = $ apiHandler ->get ($ libmanager_url . "/fetch?library= " . urlencode ($ header ));
173
+ $ requestContent = array ("type " => "fetch " , "library " => $ header );
174
+ $ data = $ this ->getLibraryInfo (json_encode ($ requestContent ));
124
175
$ data = json_decode ($ data , true );
125
176
126
177
if ($ data ["success " ]) {
0 commit comments