Skip to content

Commit 07dacf0

Browse files
committed
Add example with initialization code
1 parent ffa8bff commit 07dacf0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,27 @@ class Greet extends Handler {
3434

3535
The two parameters passed are *$event* (a value [depending on where the lambda was invoked from](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html)) and *$context* (a Context instance, see [below](https://github.com/xp-forge/lambda#context)).
3636

37-
If you need to run any initialization code, you can do before returning the lambda from *target()*. This code is only run once during the initialization phase. It has access to the lambda's environment via *$this->environment* (an Environment instance, see [below](https://github.com/xp-forge/lambda#environment)).
37+
If you need to run any initialization code, you can do before returning the lambda from *target()*. This code is only run once during the initialization phase. It has access to the lambda's environment via *$this->environment* (an Environment instance, see [below](https://github.com/xp-forge/lambda#environment)):
38+
39+
```php
40+
use com\amazon\aws\lambda\Handler;
41+
42+
class Greet extends Handler {
43+
44+
/** @return com.amazon.aws.lambda.Lambda|callable */
45+
public function target() {
46+
$default= $this->environment->properties('task')->readString('greet', 'default');
47+
48+
return fn($event, $context) => sprintf(
49+
'Hello %s from PHP %s via %s @ %s',
50+
$event['name'] ?? $default,
51+
PHP_VERSION,
52+
$context->functionName,
53+
$context->region
54+
);
55+
}
56+
}
57+
```
3858

3959
Development
4060
-----------

0 commit comments

Comments
 (0)