Skip to content

Commit 9a2b5d1

Browse files
authored
[Bref] Add possibility to use without bref/bref (#89)
* [Bref] Add possibility to use without bref/bref * CS * cs
1 parent 46318de commit 9a2b5d1

File tree

5 files changed

+134
-10
lines changed

5 files changed

+134
-10
lines changed

src/bref/bref/Context.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bref\Context;
6+
7+
/**
8+
* The execution context of a Lambda.
9+
*
10+
* @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
11+
*/
12+
final class Context implements \JsonSerializable
13+
{
14+
/** @var string */
15+
private $awsRequestId;
16+
17+
/** @var int Holds the deadline Unix timestamp in millis */
18+
private $deadlineMs;
19+
20+
/** @var string */
21+
private $invokedFunctionArn;
22+
23+
/** @var string */
24+
private $traceId;
25+
26+
public function __construct(string $awsRequestId, int $deadlineMs, string $invokedFunctionArn, string $traceId)
27+
{
28+
$this->awsRequestId = $awsRequestId;
29+
$this->deadlineMs = $deadlineMs;
30+
$this->invokedFunctionArn = $invokedFunctionArn;
31+
$this->traceId = $traceId;
32+
}
33+
34+
/**
35+
* Returns the identifier of the invocation request.
36+
*/
37+
public function getAwsRequestId(): string
38+
{
39+
return $this->awsRequestId;
40+
}
41+
42+
/**
43+
* Returns the number of milliseconds left before the execution times out.
44+
*/
45+
public function getRemainingTimeInMillis(): int
46+
{
47+
return $this->deadlineMs - intval(microtime(true) * 1000);
48+
}
49+
50+
/**
51+
* Returns the Amazon Resource Name (ARN) used to invoke the function.
52+
* Indicates if the invoker specified a version number or alias.
53+
*/
54+
public function getInvokedFunctionArn(): string
55+
{
56+
return $this->invokedFunctionArn;
57+
}
58+
59+
/**
60+
* Returns content of the AWS X-Ray trace information header.
61+
*
62+
* @see https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
63+
*/
64+
public function getTraceId(): string
65+
{
66+
return $this->traceId;
67+
}
68+
69+
public function jsonSerialize(): array
70+
{
71+
return [
72+
'awsRequestId' => $this->awsRequestId,
73+
'deadlineMs' => $this->deadlineMs,
74+
'invokedFunctionArn' => $this->invokedFunctionArn,
75+
'traceId' => $this->traceId,
76+
];
77+
}
78+
}

src/bref/bref/Handler.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bref\Event;
6+
7+
use Bref\Context\Context;
8+
9+
/**
10+
* Handles any kind of Lambda events.
11+
*/
12+
interface Handler
13+
{
14+
/**
15+
* @param mixed $event the raw event data
16+
*
17+
* @return mixed|void
18+
*/
19+
public function handle($event, Context $context);
20+
}

src/bref/bref/autoload.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* To support use of runtime/bref without bref/bref we include two small
5+
* classes here.
6+
*/
7+
if (!class_exists(\Bref\Context\Context::class)) {
8+
require_once __DIR__.'/Context.php';
9+
}
10+
11+
if (!interface_exists(\Bref\Event\Handler::class)) {
12+
require_once __DIR__.'/Handler.php';
13+
}

src/bref/composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@
1212
"require": {
1313
"ext-json": "*",
1414
"ext-sockets": "*",
15-
"bref/bref": "^1.3",
1615
"clue/arguments": "^2.1",
1716
"psr/http-server-handler": "^1.0",
1817
"riverline/multipart-parser": "^2.0",
1918
"symfony/runtime": "^5.3 || ^6.0"
2019
},
2120
"require-dev": {
21+
"bref/bref": "^1.3",
2222
"symfony/http-foundation": "^5.3 || ^6.0",
2323
"symfony/http-kernel": "^5.4 || ^6.0",
2424
"symfony/phpunit-bridge": "^5.3"
2525
},
2626
"autoload": {
2727
"psr-4": {
2828
"Runtime\\Bref\\": "src/"
29-
}
29+
},
30+
"files": [
31+
"bref/autoload.php"
32+
]
3033
},
3134
"autoload-dev": {
3235
"psr-4": {

src/bref/src/Runtime.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Runtime\Bref;
44

55
use Bref\Event\Handler;
6+
use Bref\Event\Http\HttpHandler;
67
use Bref\Event\Http\Psr15Handler;
78
use Illuminate\Contracts\Http\Kernel;
89
use Psr\Container\ContainerInterface;
@@ -33,24 +34,33 @@ public function __construct(array $options = [])
3334

3435
public function getRunner(?object $application): RunnerInterface
3536
{
37+
if ($application instanceof ContainerInterface) {
38+
$handler = explode(':', $_SERVER['_HANDLER']);
39+
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+
}
42+
$application = $application->get($handler[1]);
43+
}
44+
3645
if ($application instanceof HttpKernelInterface) {
46+
if (!class_exists(HttpHandler::class)) {
47+
throw new \RuntimeException(sprintf('The Bref Runtime needs package bref/bref to support %s applications. Try running "composer require bref/bref".', HttpKernelInterface::class));
48+
}
3749
$application = new SymfonyHttpHandler($application);
3850
}
3951

4052
if ($application instanceof Kernel) {
53+
if (!class_exists(HttpHandler::class)) {
54+
throw new \RuntimeException(sprintf('The Bref Runtime needs package bref/bref to support %s applications. Try running "composer require bref/bref".', Kernel::class));
55+
}
4156
$application = new LaravelHttpHandler($application);
4257
}
4358

4459
if ($application instanceof RequestHandlerInterface) {
45-
$application = new Psr15Handler($application);
46-
}
47-
48-
if ($application instanceof ContainerInterface) {
49-
$handler = explode(':', $_SERVER['_HANDLER']);
50-
if (!isset($handler[1]) || '' === $handler[1]) {
51-
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']));
60+
if (!class_exists(Psr15Handler::class)) {
61+
throw new \RuntimeException(sprintf('The Bref Runtime needs package bref/bref to support %s applications. Try running "composer require bref/bref".', RequestHandlerInterface::class));
5262
}
53-
$application = $application->get($handler[1]);
63+
$application = new Psr15Handler($application);
5464
}
5565

5666
if ($application instanceof Handler) {

0 commit comments

Comments
 (0)