11<?php namespace com \amazon \aws \lambda ;
22
3- use Throwable as Any ;
3+ use ReflectionFunction , Throwable as Any ;
44use io \Channel ;
55use io \streams \InputStream ;
66use lang \{Throwable , IllegalStateException , IllegalArgumentException };
@@ -33,7 +33,7 @@ public function __construct($endpoint, $version= '2018-06-01') {
3333
3434 /** Returns the buffered invoke mode */
3535 public function buffered (): InvokeMode {
36- return new class ($ this ) extends InvokeMode {
36+ return new class ($ this , ' BUFFERED ' ) extends InvokeMode {
3737 public function invoke ($ lambda , $ event , $ context ) {
3838 try {
3939 $ result = $ lambda ($ event , $ context );
@@ -47,7 +47,7 @@ public function invoke($lambda, $event, $context) {
4747
4848 /** Returns the streaming invoke mode */
4949 public function streaming (): InvokeMode {
50- return new class ($ this ) extends InvokeMode implements Stream {
50+ return new class ($ this , ' RESPONSE_STREAM ' ) extends InvokeMode implements Stream {
5151 private $ request = null ;
5252 private $ response = null ;
5353 private $ stream = null ;
@@ -128,6 +128,26 @@ public function invoke($lambda, $event, $context) {
128128 };
129129 }
130130
131+ /**
132+ * Returns an invokeable
133+ *
134+ * @param callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming $target
135+ * @return com.amazon.aws.lambda.Invokeable
136+ * @throws lang.IllegalArgumentException
137+ */
138+ public final function invokeable ($ target ) {
139+ if ($ target instanceof Lambda) {
140+ return new Invokeable ([$ target , 'process ' ], $ this ->buffered ());
141+ } else if ($ target instanceof Streaming) {
142+ return new Invokeable ([$ target , 'handle ' ], $ this ->streaming ());
143+ } else if (is_callable ($ target )) {
144+ $ n = (new ReflectionFunction ($ target ))->getNumberOfParameters ();
145+ return new Invokeable ($ target , $ n < 3 ? $ this ->buffered () : $ this ->streaming ());
146+ } else {
147+ throw new IllegalArgumentException ('Expected callable|Lambda|Streaming, have ' .typeof ($ target ));
148+ }
149+ }
150+
131151 /**
132152 * Marshals an exception according to the AWS specification.
133153 *
0 commit comments