1313class RunLambda {
1414 const TRACE_ID = 'Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419;Sampled=1 ' ;
1515
16- private $ impl , $ length , $ event ;
16+ private $ impl , $ events ;
1717
1818 /**
1919 * Creates a new `run` subcommand
2020 *
2121 * @param string $handler
22- * @param ? string $event
22+ * @param string... $events
2323 * @throws lang.ClassLoadingException
2424 */
25- public function __construct ($ handler, $ event = null ) {
25+ public function __construct ($ handler= ' Handler ' , ... $ events ) {
2626 $ this ->impl = XPClass::forName ($ handler );
27- if (null === $ event ) {
28- $ this ->length = 0 ;
29- $ this ->event = null ;
30- } else {
31- $ this ->length = strlen ($ event );
32- $ this ->event = json_decode ($ event , true );
33- }
27+ $ this ->events = $ events ?: ['{} ' ];
3428 }
3529
3630 /** Runs this command */
3731 public function run (): int {
3832 $ name = $ this ->impl ->getSimpleName ();
3933 $ region = getenv ('AWS_REGION ' ) ?: 'test-local-1 ' ;
40- $ context = new Context (
41- [
42- 'Lambda-Runtime-Aws-Request-Id ' => [UUID ::randomUUID ()->hashCode ()],
43- 'Lambda-Runtime-Invoked-Function-Arn ' => ["arn:aws:lambda: {$ region }:123456789012:function: {$ name }" ],
44- 'Lambda-Runtime-Trace-Id ' => [self ::TRACE_ID ],
45- 'Lambda-Runtime-Deadline-Ms ' => [(time () + 900 ) * 1000 ],
46- 'Content-Length ' => [$ this ->length ],
47- ],
48- $ _ENV + ['AWS_LAMBDA_FUNCTION_NAME ' => $ name , 'AWS_REGION ' => $ region ]
49- );
50-
34+ $ functionArn = "arn:aws:lambda: {$ region }:123456789012:function: {$ name }" ;
35+ $ deadlineMs = (time () + 900 ) * 1000 ;
36+ $ environment = $ _ENV + ['AWS_LAMBDA_FUNCTION_NAME ' => $ name , 'AWS_REGION ' => $ region ];
37+
5138 try {
52- $ lambda = $ this ->impl ->newInstance (new Environment (getcwd (), Console::$ out , []))->target ();
53- $ result = $ lambda instanceof Lambda ? $ lambda ->process ($ this ->event , $ context ) : $ lambda ($ this ->event , $ context );
54- Console::$ out ->writeLine ($ result );
55- return 0 ;
39+ $ target = $ this ->impl ->newInstance (new Environment (getcwd (), Console::$ out , []))->target ();
40+ $ lambda = $ target instanceof Lambda ? [$ target , 'process ' ] : $ target ;
5641 } catch (Throwable $ e ) {
5742 Console::$ err ->writeLine ($ e );
5843 return 1 ;
5944 }
45+
46+ $ status = 0 ;
47+ foreach ($ this ->events as $ event ) {
48+ $ headers = [
49+ 'Lambda-Runtime-Aws-Request-Id ' => [UUID ::randomUUID ()->hashCode ()],
50+ 'Lambda-Runtime-Invoked-Function-Arn ' => [$ functionArn ],
51+ 'Lambda-Runtime-Trace-Id ' => [self ::TRACE_ID ],
52+ 'Lambda-Runtime-Deadline-Ms ' => [$ deadlineMs ],
53+ 'Content-Length ' => [strlen ($ event )],
54+ ];
55+
56+ try {
57+ $ result = $ lambda (json_decode ($ event , true ), new Context ($ headers , $ environment ));
58+ Console::$ out ->writeLine ($ result );
59+ } catch (Throwable $ e ) {
60+ Console::$ err ->writeLine ($ e );
61+ $ status = 1 ;
62+ }
63+ }
64+ return $ status ;
6065 }
6166}
0 commit comments