Skip to content

Commit cb88ef2

Browse files
committed
Improved checkHeaders function and implemented a function that detects user/project ids from the request files (if possible)
1 parent 15e9896 commit cb88ef2

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

Symfony/src/Codebender/ApiBundle/Controller/DefaultController.php

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,49 +86,82 @@ public function compileAction($auth_key, $version)
8686

8787
/**
8888
*
89-
* @param type $files
90-
* @param array $personalLibs
89+
* @param array $files
90+
* @param array $userLibs
9191
*
92-
* @return type
92+
* @return array
9393
*/
94-
protected function checkHeaders($files, array $personalLibs)
94+
protected function checkHeaders($files, array $userlibs)
9595
{
9696
$apiHandler = $this->get('codebender_api.handler');
9797

9898
$headers = $apiHandler->read_libraries($files);
9999

100100
// declare arrays
101-
$libraries = $notFoundHeaders = $foundFiles = array();
101+
$libraries = $notFoundHeaders = $foundHeaders = $fetchedLibs = $providedLibs = array();
102+
103+
$providedLibs = array_keys($userlibs);
102104

103105
// get library manager url
104106
$libmanager_url = $this->container->getParameter('library');
105107

106-
$libraries = $personalLibs;
108+
$libraries = $userlibs;
107109

108110
foreach ($headers as $header) {
109-
$foundPersonal = false;
110-
foreach ($personalLibs as $plibrary){
111-
foreach ($plibrary as $libcontent){
112-
if ($libcontent["filename"] == $header.".h")
113-
$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+
}
114119
}
115120
}
116-
if ($foundPersonal === false) {
121+
if ($exists_in_request === false) {
117122

118123
$data = $apiHandler->get($libmanager_url . "/fetch?library=" . urlencode($header));
119124
$data = json_decode($data, true);
120125

121126
if ($data["success"]) {
127+
$fetchedLibs[] = $header;
128+
122129
$libraries[$header] = $data["files"];
123-
foreach ($data['files'] as $file) {
124-
$foundFiles[] = $file['filename'];
125-
}
130+
$foundHeaders[] = $header . ".h";
131+
126132
} elseif (!$data['success']){
127133
$notFoundHeaders[] = $header . ".h";
128134
}
129135
}
130136
}
131-
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' => '');
132165
}
133166
}
134167

0 commit comments

Comments
 (0)