Skip to content

Commit 47dee3c

Browse files
committed
Support for dispatching on application level
1 parent be5a7ff commit 47dee3c

5 files changed

Lines changed: 56 additions & 13 deletions

File tree

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ AWS Lambda Webservices change log
33

44
## ?.?.? / ????-??-??
55

6+
## 2.3.0 / 2024-05-20
7+
8+
* Added support for dispatching on application level, introduced in
9+
https://github.com/xp-forge/web/releases/tag/v4.2.0
10+
(@thekid)
11+
612
## 2.2.0 / 2024-03-24
713

814
* Made compatible with XP 12 - @thekid

src/main/php/com/amazon/aws/lambda/HttpApi.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ abstract class HttpApi extends HttpIntegration {
1414

1515
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
1616
public function target() {
17-
$routing= $this->routing();
17+
$app= $this->application();
1818

1919
// Return event handler
20-
return function($event, $context) use($routing) {
20+
return function($event, $context) use($app) {
2121
$in= new FromApiGateway($event);
2222
$req= new Request($in);
2323
$res= new Response(new ResponseDocument());
2424

2525
try {
26-
foreach ($routing->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { }
26+
foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { }
2727
$this->tracing->log($req, $res);
2828
$res->end();
2929

src/main/php/com/amazon/aws/lambda/HttpIntegration.class.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,34 @@ public function __construct(Environment $environment) {
2727
*/
2828
public abstract function routes($environment);
2929

30-
/** @return web.Routing */
31-
protected final function routing() {
32-
$routes= $this->routes(new WebEnv(
30+
/** @return web.Application */
31+
protected final function application() {
32+
$env= new WebEnv(
3333
$this->environment->variable('PROFILE') ?? 'prod',
3434
$this->environment->root,
3535
$this->environment->path('static'),
3636
[$this->environment->properties],
3737
[],
3838
$this->tracing
39-
));
39+
);
40+
$routes= $this->routes($env);
4041

4142
// Check for `xp-forge/web ^4.0` applications, which provide an initializer
42-
if ($routes instanceof Application && method_exists($routes, 'initialize')) {
43-
$routes->initialize();
43+
if ($routes instanceof Application) {
44+
method_exists($routes, 'initialize') && $routes->initialize();
45+
return $routes;
4446
}
45-
return Routing::cast($routes);
47+
48+
// Wrap routes inside an application to ensure application-level functionality
49+
return new class($env, $routes) extends Application {
50+
private $routes;
51+
52+
public function __construct($env, $routes) {
53+
parent::__construct($env);
54+
$this->routes= $routes;
55+
}
56+
57+
public function routes() { return $this->routes; }
58+
};
4659
}
4760
}

src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ abstract class HttpStreaming extends HttpIntegration {
1414

1515
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
1616
public function target() {
17-
$routing= $this->routing();
17+
$app= $this->application();
1818

1919
// Return event handler
20-
return function($event, $stream, $context) use($routing) {
20+
return function($event, $stream, $context) use($app) {
2121
$in= new FromApiGateway($event);
2222
$req= new Request($in);
2323
$res= new Response(new StreamingTo($stream));
2424

2525
try {
26-
foreach ($routing->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { }
26+
foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { }
2727
$this->tracing->log($req, $res);
2828

2929
$res->end();

src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ public function sending_redirect() {
138138
);
139139
}
140140

141+
#[Test]
142+
public function sending_dispatch() {
143+
$fixture= [
144+
'/target' => function($req, $res) {
145+
$res->answer(200);
146+
$res->send('Hello '.$req->param('name'), 'text/plain');
147+
},
148+
'/' => function($req, $res) {
149+
return $req->dispatch('/target', ['name' => 'Test']);
150+
},
151+
];
152+
153+
Assert::equals(
154+
[
155+
'statusCode' => 200,
156+
'statusDescription' => 'OK',
157+
'isBase64Encoded' => false,
158+
'headers' => ['Content-Type' => 'text/plain', 'Content-Length' => 10],
159+
'body' => 'Hello Test',
160+
],
161+
$this->transform($this->invoke($fixture, 'GET'))
162+
);
163+
}
164+
141165
#[Test]
142166
public function throwing_error() {
143167
$fixture= ['/' => function($req, $res) {

0 commit comments

Comments
 (0)