diff --git a/ChangeLog.md b/ChangeLog.md index c136b7a..95f2668 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,12 @@ AWS Lambda Webservices change log ## ?.?.? / ????-??-?? +## 2.3.0 / 2024-05-20 + +* Added support for dispatching on application level, introduced in + https://github.com/xp-forge/web/releases/tag/v4.2.0 + (@thekid) + ## 2.2.0 / 2024-03-24 * Made compatible with XP 12 - @thekid diff --git a/src/main/php/com/amazon/aws/lambda/HttpApi.class.php b/src/main/php/com/amazon/aws/lambda/HttpApi.class.php index 982e34c..89bef3b 100755 --- a/src/main/php/com/amazon/aws/lambda/HttpApi.class.php +++ b/src/main/php/com/amazon/aws/lambda/HttpApi.class.php @@ -14,16 +14,16 @@ abstract class HttpApi extends HttpIntegration { /** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */ public function target() { - $routing= $this->routing(); + $app= $this->application(); // Return event handler - return function($event, $context) use($routing) { + return function($event, $context) use($app) { $in= new FromApiGateway($event); $req= new Request($in); $res= new Response(new ResponseDocument()); try { - foreach ($routing->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { } + foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { } $this->tracing->log($req, $res); $res->end(); diff --git a/src/main/php/com/amazon/aws/lambda/HttpIntegration.class.php b/src/main/php/com/amazon/aws/lambda/HttpIntegration.class.php index bb9a718..54a7fe5 100755 --- a/src/main/php/com/amazon/aws/lambda/HttpIntegration.class.php +++ b/src/main/php/com/amazon/aws/lambda/HttpIntegration.class.php @@ -27,21 +27,34 @@ public function __construct(Environment $environment) { */ public abstract function routes($environment); - /** @return web.Routing */ - protected final function routing() { - $routes= $this->routes(new WebEnv( + /** @return web.Application */ + protected final function application() { + $env= new WebEnv( $this->environment->variable('PROFILE') ?? 'prod', $this->environment->root, $this->environment->path('static'), [$this->environment->properties], [], $this->tracing - )); + ); + $routes= $this->routes($env); // Check for `xp-forge/web ^4.0` applications, which provide an initializer - if ($routes instanceof Application && method_exists($routes, 'initialize')) { - $routes->initialize(); + if ($routes instanceof Application) { + method_exists($routes, 'initialize') && $routes->initialize(); + return $routes; } - return Routing::cast($routes); + + // Wrap routes inside an application to ensure application-level functionality + return new class($env, $routes) extends Application { + private $routes; + + public function __construct($env, $routes) { + parent::__construct($env); + $this->routes= $routes; + } + + public function routes() { return $this->routes; } + }; } } \ No newline at end of file diff --git a/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php b/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php index e948700..8cb5895 100755 --- a/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php +++ b/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php @@ -14,16 +14,16 @@ abstract class HttpStreaming extends HttpIntegration { /** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */ public function target() { - $routing= $this->routing(); + $app= $this->application(); // Return event handler - return function($event, $stream, $context) use($routing) { + return function($event, $stream, $context) use($app) { $in= new FromApiGateway($event); $req= new Request($in); $res= new Response(new StreamingTo($stream)); try { - foreach ($routing->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { } + foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { } $this->tracing->log($req, $res); $res->end(); diff --git a/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php b/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php index 6c68487..0a1c3fe 100755 --- a/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php +++ b/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php @@ -138,6 +138,30 @@ public function sending_redirect() { ); } + #[Test] + public function sending_dispatch() { + $fixture= [ + '/target' => function($req, $res) { + $res->answer(200); + $res->send('Hello '.$req->param('name'), 'text/plain'); + }, + '/' => function($req, $res) { + return $req->dispatch('/target', ['name' => 'Test']); + }, + ]; + + Assert::equals( + [ + 'statusCode' => 200, + 'statusDescription' => 'OK', + 'isBase64Encoded' => false, + 'headers' => ['Content-Type' => 'text/plain', 'Content-Length' => 10], + 'body' => 'Hello Test', + ], + $this->transform($this->invoke($fixture, 'GET')) + ); + } + #[Test] public function throwing_error() { $fixture= ['/' => function($req, $res) {