Repository navigation
Expand file tree
/
Copy pathSandboxController.php
More file actions
287 lines (263 loc) · 7.82 KB
/
Copy pathSandboxController.php
File metadata and controls
287 lines (263 loc) · 7.82 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace SyncEngine\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use SyncEngine\Runtime\ExecutePreview;
use SyncEngine\Service\Sandbox\EntityManagerSandbox;
class SandboxController extends DefaultController
{
const VALID_TYPES = [ 'task', 'routine', 'step', 'flow', 'automation' ];
public static function getSubscribedServices(): array
{
return array_merge(
parent::getSubscribedServices(),
[
'entitymanager' => '?' . EntityManagerSandbox::class,
]
);
}
#[Route( '/admin/json/preview', name: 'json_preview', requirements: [], methods: [ 'POST' ] )]
public function preview( Request $request, ExecutePreview $executePreview ): JsonResponse
{
$this->denyAccessUnlessGranted( 'ROLE_EDITOR' );
return $this->handlePreview( $request, $executePreview );
}
#[Route( '/api/preview', name: 'api_preview', requirements: [], methods: [ 'POST' ] )]
public function api_preview( Request $request, ExecutePreview $executePreview ): JsonResponse
{
$this->denyAccessUnlessGranted( 'ROLE_API' );
return $this->handlePreview( $request, $executePreview );
}
#[Route( '/api/preview/schema/{type}', name: 'preview_schema', requirements: [ 'type' => '[a-z]+' ], methods: [ 'GET' ] )]
public function preview_schema( string $type ): JsonResponse
{
$this->denyAccessUnlessGranted( 'ROLE_API' );
return $this->json( $this->getPreviewSchema( $type ) );
}
protected function handlePreview( Request $request, ExecutePreview $executePreview ): JsonResponse
{
try {
$body = $request->toArray();
} catch ( \Throwable $e ) {
return $this->json(
[
'success' => false,
'message' => 'Invalid JSON request body.',
],
Response::HTTP_BAD_REQUEST
);
}
try {
$type = $body['type'] ?? '';
$mode = $body['mode'] ?? ExecutePreview::MODE_SAFE;
$config = $body['config'] ?? [];
$data = $body['data'] ?? [];
$variables = $body['variables'] ?? null;
$requestParams = $body['requestParams'] ?? null;
$requestQuery = $body['requestQuery'] ?? null;
$scope = $body['scope'] ?? null;
if ( ! in_array( $type, self::VALID_TYPES, true ) ) {
return $this->json(
[
'success' => false,
'message' => 'Invalid preview type. Valid types: ' . implode( ', ', self::VALID_TYPES ),
'valid_types' => self::VALID_TYPES,
],
Response::HTTP_BAD_REQUEST
);
}
if ( is_string( $data ) ) {
try {
$parsedData = json_decode( $data, true, flags: JSON_THROW_ON_ERROR );
$data = is_array( $parsedData ) ? $parsedData : $data;
} catch ( \Throwable $e ) {
// Nope.
}
}
$result = $executePreview->preview(
type : $type,
mode : $mode,
config : $config,
data : $data,
variables : $variables,
requestParams: $requestParams,
requestQuery : $requestQuery,
scope : $scope,
);
$count = isset( $result['Return'] ) ? count( is_countable( $result['Return'] ) ? $result['Return'] : [] ) : null;
if ( $count ) {
// @todo Implement tab/pane style DTO for return tabs.
$result['Return (' . $count . ')' ] = $result['Return'];
unset( $result['Return'] );
}
$params = $request->request->all();
foreach ( $params as &$param ) {
try {
$param = json_decode( $param, true ) ?: $param;
} catch ( \Throwable $e ) {
// Nope.
}
}
unset( $param ); // Remove reference to avoid accidental modifications.
$result['Params'] = $params;
$status = $result['success'] ? Response::HTTP_OK : Response::HTTP_PARTIAL_CONTENT;
return $this->json( $result, $status );
} catch ( \Throwable $e ) {
if ( $this->isDebug() ) {
return $this->json(
[
'success' => false,
'message' => $e->getMessage(),
'type' => get_class( $e ),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
$this->logException( $e, 'An error occurred during preview execution' );
return $this->json(
[
'success' => false,
'message' => 'An error occurred during preview execution. See logs for details.',
],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
protected function getPreviewSchema( string $type = '' ): array
{
$fields = [
'type' => [
'type' => 'string',
'description' => 'Preview type (task, routine, step, flow, automation).',
'enum' => self::VALID_TYPES,
'default' => '',
],
'mode' => [
'type' => 'string',
'description' => 'Preview mode: "safe" (dry-run, does not execute send-type webservice calls) or "live" (executes all webservice calls). Default: safe.',
'enum' => [ 'safe', 'live' ],
'default' => 'safe',
],
'data' => [
'type' => 'any',
'description' => 'Optional data to be used for preview as the source data.',
],
'config' => [
'type' => 'object',
'description' => 'Configuration object specific to the preview type.',
],
'variables' => [
'type' => 'object',
'description' => 'Key-value pairs of variables available during execution.',
'additionalProperties' => [ 'type' => 'string' ],
],
'requestParams' => [
'type' => 'object',
'description' => 'Simulated request body parameters.',
'additionalProperties' => true,
],
'requestQuery' => [
'type' => 'object',
'description' => 'Simulated request query string parameters.',
'additionalProperties' => true,
],
'scope' => [
'type' => 'array',
'description' => 'Optional queue of pre-configured entity models to simulate a full pipeline. Each item: { _entity, id, config }.',
],
];
return match ( $type ) {
default => [
'required' => [ 'config' ],
'fields' => $fields
],
'task' => [
'type' => 'task',
'description' => 'Preview a single task configuration.',
'required' => [ 'config' ],
'fields' => $fields,
'example' => [
'config' => [
'_class' => 'Http',
'url' => 'https://api.example.com/data',
'method' => 'GET',
],
'mode' => 'safe',
],
],
'routine' => [
'type' => 'routine',
'description' => 'Preview a routine (sequence of tasks).',
'required' => [ 'config' ],
'fields' => $fields,
'example' => [
'config' => [
'_ref' => 'my_routine',
'tasks' => [
[
'_class' => 'Http',
'url' => 'https://api.example.com/data',
],
],
],
'mode' => 'safe',
],
],
'step' => [
'type' => 'step',
'description' => 'Preview a single flow step.',
'required' => [ 'config' ],
'fields' => $fields,
'example' => [
'config' => [
'_ref' => 'my_step',
'routine' => [
'_ref' => 'my_routine',
'input' => [],
'variables' => [],
],
],
'mode' => 'safe',
],
],
'flow' => [
'type' => 'flow',
'description' => 'Preview a flow (sequence of steps).',
'required' => [ 'config' ],
'fields' => $fields,
'example' => [
'config' => [
'_ref' => 'my_flow',
'steps' => [
[
'_ref' => 'step_1',
'routine' => [ '_ref' => 'my_routine' ],
],
],
],
'mode' => 'safe',
],
],
'automation' => [
'type' => 'automation',
'description' => 'Preview a full automation (source + actions).',
'required' => [ 'config' ],
'fields' => $fields,
'example' => [
'config' => [
'source' => 'request',
'request' => [],
'actions' => [],
],
'mode' => 'safe',
],
],
};
}
}