Skip to content

Commit

Permalink
Support for dispatching on application level
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed May 20, 2024
1 parent be5a7ff commit 47dee3c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/php/com/amazon/aws/lambda/HttpApi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
27 changes: 20 additions & 7 deletions src/main/php/com/amazon/aws/lambda/HttpIntegration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};
}
}
6 changes: 3 additions & 3 deletions src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 47dee3c

Please sign in to comment.