-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDefaultController.php
180 lines (144 loc) · 5.9 KB
/
DefaultController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* \file
* \brief Functions used by the compiler backend.
*
* \author Dimitrios Christidis
* \author Vasilis Georgitzikis
*
* \copyright (c) 2012-2013, The Codebender Development Team
* \copyright Licensed under the Simplified BSD License
*/
namespace Codebender\CompilerBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Codebender\CompilerBundle\Handler\CompilerHandler;
use Codebender\CompilerBundle\Handler\DeletionHandler;
use Codebender\CompilerBundle\Handler\BuilderCompilerHandler;
class DefaultController extends Controller
{
public function statusAction()
{
return new Response(json_encode(array("success" => true, "status" => "OK")));
}
public function testAction($authorizationKey)
{
$params = $this->generateParameters();
if ($authorizationKey !== $params["authorizationKey"])
{
return new Response(json_encode(array("success" => false, "step" => 0, "message" => "Invalid authorization key.")));
}
set_time_limit(0); // make the script execution time unlimited (otherwise the request may time out)
// change the current Symfony root dir
chdir($this->get('kernel')->getRootDir()."/../");
//TODO: replace this with a less horrible way to handle phpunit
exec("phpunit -c app --stderr 2>&1", $output, $return_val);
return new Response(json_encode(array("success" => (bool) !$return_val, "message" => implode("\n", $output))));
}
public function indexAction($authorizationKey, $version)
{
$params = $this->generateParameters();
if ($authorizationKey !== $params["authorizationKey"])
{
return new Response(json_encode(array("success" => false, "step" => 0, "message" => "Invalid authorization key.")));
}
if ($version == "v1")
{
$request = $this->getRequest()->getContent();
//Get the compiler service
/** @var CompilerHandler $compiler */
$compiler = $this->get('builder_compiler_handler');
$reply = $compiler->main($request, $params);
return new Response(json_encode($reply));
}
else
{
return new Response(json_encode(array("success" => false, "step" => 0, "message" => "Invalid API version.")));
}
}
public function deleteAllObjectsAction($authorizationKey, $version)
{
if ($this->container->getParameter('authorizationKey') != $authorizationKey) {
return new Response(json_encode(
array('success' => false, 'step' => 0, 'message' => 'Invalid authorization key.')
));
}
if ($version != 'v1') {
return new Response(json_encode(
array('success' => false, 'step' => 0, 'message' => 'Invalid API version.')
));
}
//Get the compiler service
/** @var DeletionHandler $deleter */
$deleter = $this->get('deletion_handler');
$response = $deleter->deleteAllObjects();
if ($response['success'] !== true) {
return new Response(json_encode(
array('success' => false, 'step' => 0, 'message' => 'Failed to access object files directory.')
));
}
return new Response(json_encode(
array_merge(
array(
'success' => true,
'message' => 'Object files deletion complete. Found ' . $response['fileCount'] . ' files.'
),
$response['deletionStats'],
array("Files not deleted" => $response['notDeletedFiles'])
)));
}
public function deleteSpecificObjectsAction($authorizationKey, $version, $option, $cachedObjectToDelete)
{
if ($this->container->getParameter('authorizationKey') != $authorizationKey) {
return new Response(json_encode(
array('success' => false, 'step' => 0, 'message' => 'Invalid authorization key.')
));
}
if ($version != 'v1') {
return new Response(json_encode(
array('success' => false, 'step' => 0, 'message' => 'Invalid API version.')
));
}
//Get the compiler service
/** @var DeletionHandler $deleter */
$deleter = $this->get('deletion_handler');
$response = $deleter->deleteSpecificObjects($option, $cachedObjectToDelete);
if ($response['success'] !== true) {
return new Response(json_encode(
array('success' => false, 'step' => 0, 'message' => 'Failed to access object files directory.')
));
}
if (!empty($response["notDeletedFiles"])) {
$message = 'Failed to delete one or more of the specified core object files.';
if ($option == 'library') {
$message = 'Failed to delete one or more of the specified library object files.';
}
return new Response(json_encode(
array_merge(array('success' => false, 'step' => 0, 'message' => $message), $response)
));
}
$message = 'Core object files deleted successfully.';
if ($option == 'library'){
$message = 'Library deleted successfully.';
}
return new Response(json_encode(array_merge(array('success' => true, 'message' => $message), $response)));
}
/**
* \brief Creates a list of the configuration parameters to be used in the compilation process.
*
* \return An array of the parameters.
*
* This function accesses the Symfony global configuration parameters, and creates an array that our handlers (which
* don't have access to them) can use them.
*/
private function generateParameters()
{
$parameters = array("binutils", "python", "clang", "logdir", "temp_dir", "archive_dir", "autocompletion_dir", "autocompleter", "cflags", "cppflags", "asflags", "arflags", "ldflags", "ldflags_tail", "clang_flags", "objcopy_flags", "size_flags", "output", "arduino_cores_dir", "external_core_files", "authorizationKey");
$compiler_config = array();
foreach ($parameters as $parameter)
{
$compiler_config[$parameter] = $this->container->getParameter($parameter);
}
return $compiler_config;
}
}