66class TestLambda {
77 use Docker;
88
9- private $ version , $ path , $ handler , $ payload ;
9+ private $ version , $ path ;
10+ private $ environment = [];
11+ private $ handler = 'Handler ' ;
12+ private $ payload = '{} ' ;
1013
11- public function __construct (string $ version , Path $ path , string $ handler , string $ payload ) {
14+ public function __construct (string $ version , Path $ path , array $ args ) {
1215 $ this ->version = $ version ;
1316 $ this ->path = $ path ->asRealpath ();
14- $ this ->handler = $ handler ;
15- $ this ->payload = $ payload ;
17+
18+ // Separate `-e NAME=VALUE` from handler and payload
19+ for ($ i = 0 , $ s = sizeof ($ args ); $ i < $ s ; $ i ++) {
20+ if ('-e ' === $ args [$ i ]) {
21+ $ this ->environment []= $ args [++$ i ];
22+ } else {
23+ $ this ->handler = $ args [$ i ];
24+ $ this ->payload = $ args [++$ i ] ?? '{} ' ;
25+ break ;
26+ }
27+ }
28+ }
29+
30+ /** Passes multiple arguments via command line */
31+ private function pass ($ flag , $ list ) {
32+ $ r = [];
33+ foreach ($ list as $ element ) {
34+ $ r []= $ flag ;
35+ $ r []= $ element ;
36+ }
37+ return $ r ;
1638 }
1739
40+ /** Runs this command */
1841 public function run (): int {
19- $ test = $ this ->image ('test ' , $ this ->version , ['runtime ' => []])['test ' ];
20- if (null === $ test ) return 1 ;
42+ $ image = $ this ->image ('test ' , $ this ->version , ['runtime ' => []])['test ' ];
43+ if (null === $ image ) return 1 ;
2144
22- return $ this ->passthru (['run ' , '--rm ' , '-v ' , "{$ this ->path }:/var/task:ro " , $ test , $ this ->handler , $ this ->payload ]);
45+ return $ this ->passthru ([
46+ 'run ' ,
47+ '--rm ' ,
48+ '-v ' , "{$ this ->path }:/var/task:ro " ,
49+ ...$ this ->pass ('-e ' , $ this ->environment ),
50+ $ image ,
51+ $ this ->handler ,
52+ $ this ->payload
53+ ]);
2354 }
2455}
0 commit comments