Skip to content

Commit d840c4c

Browse files
authored
[Bref] Bug fixes and readme updates (#91)
1 parent f5a936b commit d840c4c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/bref/README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ The first thing we need is a file that returns a PSR-11 container. See below
217217
for an example for Symfony.
218218

219219
```php
220-
// lambda.php
220+
// bin/container.php
221221
222222
use App\Kernel;
223223
@@ -248,7 +248,7 @@ class HelloWorld implements Handler
248248
}
249249
```
250250

251-
Now we need to update serverless.yml to say "Use lambda.php to get the container
251+
Now we need to update serverless.yml to say "Use bin/container.php to get the container
252252
and then load service App\Lambda\HelloWorld".
253253

254254
```yaml
@@ -258,7 +258,10 @@ and then load service App\Lambda\HelloWorld".
258258
259259
functions:
260260
hello:
261-
handler: lambda.php:App\Lambda\HelloWorld
261+
handler: bin/container.php:App\Lambda\HelloWorld
262+
layers:
263+
- ${runtime-bref:php-80}
264+
262265
```
263266

264267
When this is deployed it can be invoked by
@@ -289,7 +292,7 @@ file containing JSON.
289292

290293
### Simplify serverless.yml
291294

292-
The syntax `handler: lambda.php:App\Lambda\HelloWorld` might be a bit weird to write,
295+
The syntax `handler: bin/container.php:App\Lambda\HelloWorld` might be a bit weird to write,
293296
but you may add an environment variable called `FALLBACK_CONTAINER_FILE` which
294297
includes the file to where we can get the PSR-11 container. This may help the
295298
serverless.yml file to read more natually.
@@ -304,13 +307,16 @@ serverless.yml file to read more natually.
304307
environment:
305308
APP_ENV: prod
306309
APP_RUNTIME: Runtime\Bref\Runtime
307-
+ FALLBACK_CONTAINER_FILE: lambda.php
310+
+ FALLBACK_CONTAINER_FILE: bin/container.php
308311
BREF_LOOP_MAX: 100 # Optional
309312

310313
functions:
311314
hello:
312-
- handler: lambda.php:App\Lambda\HelloWorld
313-
+ handler: App\Lambda\HelloWorld
315+
- handler: bin/container.php:App\Lambda\HelloWorld
316+
+ handler: App\Lambda\HelloWorld
317+
layers:
318+
- ${runtime-bref:php-80}
319+
314320
```
315321

316322
### Typed handlers
@@ -353,7 +359,7 @@ class S3FileCreated extends S3Handler
353359

354360
functions:
355361
s3_photos:
356-
handler: lambda.php:App\Lambda\S3FileCreated
362+
handler: bin/container.php:App\Lambda\S3FileCreated
357363
layers:
358364
- ${runtime-bref:php-80}
359365
events:
@@ -376,7 +382,7 @@ you may also want to define a worker function.
376382
377383
functions:
378384
worker:
379-
handler: lambda.php:Bref\Symfony\Messenger\Service\Sqs\SqsConsumer
385+
handler: bin/container.php:Bref\Symfony\Messenger\Service\Sqs\SqsConsumer
380386
timeout: 120
381387
layers:
382388
- ${runtime-bref:php-80}

src/bref/src/Runtime.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Container\ContainerInterface;
1010
use Psr\Http\Server\RequestHandlerInterface;
1111
use Symfony\Component\Console\Application;
12+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1213
use Symfony\Component\HttpKernel\HttpKernelInterface;
1314
use Symfony\Component\Runtime\RunnerInterface;
1415
use Symfony\Component\Runtime\SymfonyRuntime;
@@ -37,9 +38,15 @@ public function getRunner(?object $application): RunnerInterface
3738
if ($application instanceof ContainerInterface) {
3839
$handler = explode(':', $_SERVER['_HANDLER']);
3940
if (!isset($handler[1]) || '' === $handler[1]) {
40-
throw new \RuntimeException(sprintf('Application is instance of ContainerInterface but the handler does not contain a service. The handler must be on format "path/to/file.php:App\\Lambda\\MyHandler". You provided "%s".', $_SERVER['_HANDLER']));
41+
// We assume that $handler[0] is your service name, ie you are using FALLBACK_CONTAINER_FILE
42+
$handler[1] = $handler[0];
43+
}
44+
45+
try {
46+
$application = $application->get($handler[1]);
47+
} catch (ServiceNotFoundException $e) {
48+
throw new \RuntimeException(sprintf('Application is instance of ContainerInterface but the service is not found. The handler must be on format "path/to/file.php:App\\Lambda\\MyHandler". You provided "%s".', $_SERVER['_HANDLER']), 0, $e);
4149
}
42-
$application = $application->get($handler[1]);
4350
}
4451

4552
if ($application instanceof HttpKernelInterface) {

0 commit comments

Comments
 (0)