2525 ->param ('timeout ' , '600 ' , new Text (16 ), 'Maximum logs timeout. ' , true )
2626 ->inject ('response ' )
2727 ->inject ('runner ' )
28- ->action (function (string $ runtimeId , string $ timeoutStr , Response $ response , Runner $ runner ) {
28+ ->action (function (string $ runtimeId , string $ timeoutStr , Response $ response , Runner $ runner ): void {
2929 $ timeout = \intval ($ timeoutStr );
3030
3131 $ response ->sendHeader ('Content-Type ' , 'text/event-stream ' );
4343 ->param ('timeout ' , 600 , new Integer (), 'Commands execution time in seconds. ' , true )
4444 ->inject ('response ' )
4545 ->inject ('runner ' )
46- ->action (function (string $ runtimeId , string $ command , int $ timeout , Response $ response , Runner $ runner ) {
46+ ->action (function (string $ runtimeId , string $ command , int $ timeout , Response $ response , Runner $ runner ): void {
4747 $ output = $ runner ->executeCommand ($ runtimeId , $ command , $ timeout );
4848 $ response ->setStatusCode (Response::STATUS_CODE_OK )->json ([ 'output ' => $ output ]);
4949 });
6868 ->param ('restartPolicy ' , DockerAPI::RESTART_NO , new WhiteList ([DockerAPI::RESTART_NO , DockerAPI::RESTART_ALWAYS , DockerAPI::RESTART_ON_FAILURE , DockerAPI::RESTART_UNLESS_STOPPED ], true ), 'Define restart policy for the runtime once an exit code is returned. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped". ' , true )
6969 ->inject ('response ' )
7070 ->inject ('runner ' )
71- ->action (function (string $ runtimeId , string $ image , string $ entrypoint , string $ source , string $ destination , string $ outputDirectory , array $ variables , string $ runtimeEntrypoint , string $ command , int $ timeout , bool $ remove , float $ cpus , int $ memory , string $ version , string $ restartPolicy , Response $ response , Runner $ runner ) {
71+ ->action (function (string $ runtimeId , string $ image , string $ entrypoint , string $ source , string $ destination , string $ outputDirectory , array $ variables , string $ runtimeEntrypoint , string $ command , int $ timeout , bool $ remove , float $ cpus , int $ memory , string $ version , string $ restartPolicy , Response $ response , Runner $ runner ): void {
7272 $ secret = \bin2hex (\random_bytes (16 ));
7373
7474 /**
9090 default => [],
9191 });
9292
93- if (! empty ( $ outputDirectory) ) {
93+ if ($ outputDirectory !== '' && $ outputDirectory !== ' 0 ' ) {
9494 $ variables = \array_merge ($ variables , [
9595 'OPEN_RUNTIMES_OUTPUT_DIRECTORY ' => $ outputDirectory
9696 ]);
100100 'CI ' => 'true '
101101 ]);
102102
103- $ variables = array_map (fn ( $ v ) => strval ($ v ), $ variables );
103+ $ variables = array_map (strval (... ), $ variables );
104104
105105 $ container = $ runner ->createRuntime ($ runtimeId , $ secret , $ image , $ entrypoint , $ source , $ destination , $ variables , $ runtimeEntrypoint , $ command , $ timeout , $ remove , $ cpus , $ memory , $ version , $ restartPolicy );
106106 $ response ->setStatusCode (Response::STATUS_CODE_CREATED )->json ($ container );
111111 ->desc ("List currently active runtimes " )
112112 ->inject ('runner ' )
113113 ->inject ('response ' )
114- ->action (function (Runner $ runner , Response $ response ) {
114+ ->action (function (Runner $ runner , Response $ response ): void {
115115 $ response ->setStatusCode (Response::STATUS_CODE_OK )->json ($ runner ->getRuntimes ());
116116 });
117117
121121 ->param ('runtimeId ' , '' , new Text (64 ), 'Runtime unique ID. ' )
122122 ->inject ('runner ' )
123123 ->inject ('response ' )
124- ->action (function (string $ runtimeId , Runner $ runner , Response $ response ) {
124+ ->action (function (string $ runtimeId , Runner $ runner , Response $ response ): void {
125125 $ runtimeName = System::getHostname () . '- ' . $ runtimeId ;
126126 $ response ->setStatusCode (Response::STATUS_CODE_OK )->json ($ runner ->getRuntime ($ runtimeName ));
127127 });
132132 ->param ('runtimeId ' , '' , new Text (64 ), 'Runtime unique ID. ' )
133133 ->inject ('response ' )
134134 ->inject ('runner ' )
135- ->action (function (string $ runtimeId , Response $ response , Runner $ runner ) {
135+ ->action (function (string $ runtimeId , Response $ response , Runner $ runner ): void {
136136 $ runner ->deleteRuntime ($ runtimeId );
137137 $ response ->setStatusCode (Response::STATUS_CODE_OK )->send ();
138138 });
@@ -183,39 +183,18 @@ function (
183183 Response $ response ,
184184 Request $ request ,
185185 Runner $ runner
186- ) {
187- // Extra parsers and validators to support both JSON and multipart
188- $ intParams = ['timeout ' , 'memory ' ];
189- foreach ($ intParams as $ intParam ) {
190- if (!empty ($ $ intParam ) && !is_numeric ($ $ intParam )) {
191- $ $ intParam = \intval ($ $ intParam );
192- }
186+ ): void {
187+ // Parse JSON strings for assoc params when coming from multipart
188+ if (\is_string ($ headers )) {
189+ $ headers = \json_decode ($ headers , true ) ?? [];
193190 }
194191
195- $ floatParams = ['cpus ' ];
196- foreach ($ floatParams as $ floatPram ) {
197- if (!empty ($ $ floatPram ) && !is_numeric ($ $ floatPram )) {
198- $ $ floatPram = \floatval ($ $ floatPram );
199- }
192+ if (\is_string ($ variables )) {
193+ $ variables = \json_decode ($ variables , true ) ?? [];
200194 }
201195
202- /**
203- * @var array<string, mixed> $headers
204- * @var array<string, mixed> $variables
205- */
206- $ assocParams = ['headers ' , 'variables ' ];
207- foreach ($ assocParams as $ assocParam ) {
208- if (!empty ($ $ assocParam ) && !is_array ($ $ assocParam )) {
209- $ $ assocParam = \json_decode ($ $ assocParam , true );
210- }
211- }
212-
213- $ booleanParams = ['logging ' ];
214- foreach ($ booleanParams as $ booleamParam ) {
215- if (!empty ($ $ booleamParam ) && !is_bool ($ $ booleamParam )) {
216- $ $ booleamParam = $ $ booleamParam === "true " ? true : false ;
217- }
218- }
196+ /** @var array<string, mixed> $headers */
197+ /** @var array<string, mixed> $variables */
219198
220199 // 'headers' validator
221200 $ validator = new Assoc ();
@@ -229,11 +208,11 @@ function (
229208 throw new Exception (Exception::EXECUTION_BAD_REQUEST , $ validator ->getDescription ());
230209 }
231210
232- if (empty ($ payload )) {
211+ if (in_array ($ payload, [ null , '' , ' 0 ' ], true )) {
233212 $ payload = '' ;
234213 }
235214
236- $ variables = array_map (fn ( $ v ) => strval ($ v ), $ variables );
215+ $ variables = array_map (strval (... ), $ variables );
237216
238217 $ execution = $ runner ->createExecution (
239218 $ runtimeId ,
@@ -303,16 +282,16 @@ function (
303282Http::get ('/v1/health ' )
304283 ->desc ("Get health status " )
305284 ->inject ('response ' )
306- ->action (function (Response $ response ) {
285+ ->action (function (Response $ response ): void {
307286 $ response ->setStatusCode (Response::STATUS_CODE_OK )->text ("OK " );
308287 });
309288
310289Http::init ()
311290 ->groups (['api ' ])
312291 ->inject ('request ' )
313- ->action (function (Request $ request ) {
292+ ->action (function (Request $ request ): void {
314293 $ secretKey = \explode (' ' , $ request ->getHeader ('authorization ' , '' ))[1 ] ?? '' ;
315- if (empty ( $ secretKey) || $ secretKey !== System::getEnv ('OPR_EXECUTOR_SECRET ' , '' )) {
294+ if ($ secretKey === '' || $ secretKey === ' 0 ' || $ secretKey !== System::getEnv ('OPR_EXECUTOR_SECRET ' , '' )) {
316295 throw new Exception (Exception::GENERAL_UNAUTHORIZED , 'Missing executor key ' );
317296 }
318297 });
0 commit comments