Skip to content

Commit 46318de

Browse files
authored
[Bref] Dont use internal classes from bref/bref (#88)
* [Bref] Dont use internal classes from bref/bref * cs fixes
1 parent ef72ce1 commit 46318de

File tree

6 files changed

+494
-31
lines changed

6 files changed

+494
-31
lines changed

src/bref/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
],
1212
"require": {
1313
"ext-json": "*",
14+
"ext-sockets": "*",
1415
"bref/bref": "^1.3",
1516
"clue/arguments": "^2.1",
1617
"psr/http-server-handler": "^1.0",

src/bref/src/BrefRunner.php

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

55
use Bref\Event\Handler;
6-
use Bref\Runtime\LambdaRuntime;
6+
use Runtime\Bref\Lambda\LambdaClient;
77
use Symfony\Component\Runtime\RunnerInterface;
88

99
/**
@@ -24,7 +24,7 @@ public function __construct(Handler $handler, int $loopMax)
2424

2525
public function run(): int
2626
{
27-
$lambda = LambdaRuntime::fromEnvironmentVariable('symfony-runtime');
27+
$lambda = LambdaClient::fromEnvironmentVariable('symfony-runtime');
2828

2929
$loops = 0;
3030
while (true) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Runtime\Bref;
4+
5+
use Bref\Context\Context;
6+
use Bref\Event\Handler;
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Input\ArgvInput;
9+
use Symfony\Component\Console\Output\BufferedOutput;
10+
11+
/**
12+
* @author Tobias Nyholm <[email protected]>
13+
*
14+
* @internal
15+
*/
16+
class ConsoleApplicationHandler implements Handler
17+
{
18+
private $application;
19+
20+
public function __construct(Application $application)
21+
{
22+
$this->application = $application;
23+
$this->application->setAutoExit(false);
24+
}
25+
26+
public function handle($event, Context $context)
27+
{
28+
$args = \Clue\Arguments\split((string) $event);
29+
array_unshift($args, 'command');
30+
31+
$input = new ArgvInput($args);
32+
$output = new BufferedOutput();
33+
$exitCode = $this->application->run($input, $output);
34+
35+
$content = $output->fetch();
36+
// Echo the output so that it is written to CloudWatch logs
37+
echo $content;
38+
39+
if ($exitCode > 0) {
40+
throw new \Exception('The command exited with a non-zero status code: '.$exitCode);
41+
}
42+
43+
return [
44+
'exitCode' => $exitCode, // will always be 0
45+
'output' => $content,
46+
];
47+
}
48+
}

src/bref/src/ConsoleApplicationRunner.php

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace Runtime\Bref;
44

5-
use Bref\Context\Context;
6-
use Bref\Runtime\LambdaRuntime;
5+
use Runtime\Bref\Lambda\LambdaClient;
76
use Symfony\Component\Console\Application;
8-
use Symfony\Component\Console\Input\ArgvInput;
9-
use Symfony\Component\Console\Output\BufferedOutput;
107
use Symfony\Component\Runtime\RunnerInterface;
118

129
/**
@@ -16,40 +13,19 @@
1613
*/
1714
class ConsoleApplicationRunner implements RunnerInterface
1815
{
19-
private $application;
16+
private $handler;
2017

2118
public function __construct(Application $application)
2219
{
23-
$this->application = $application;
24-
$this->application->setAutoExit(false);
20+
$this->handler = new ConsoleApplicationHandler($application);
2521
}
2622

2723
public function run(): int
2824
{
29-
$lambda = LambdaRuntime::fromEnvironmentVariable('symfony-runtime-console');
25+
$lambda = LambdaClient::fromEnvironmentVariable('symfony-runtime-console');
3026

3127
while (true) {
32-
$lambda->processNextEvent(function ($event, Context $context): array {
33-
$args = \Clue\Arguments\split((string) $event);
34-
array_unshift($args, 'command');
35-
36-
$input = new ArgvInput($args);
37-
$output = new BufferedOutput();
38-
$exitCode = $this->application->run($input, $output);
39-
40-
$content = $output->fetch();
41-
// Echo the output so that it is written to CloudWatch logs
42-
echo $content;
43-
44-
if ($exitCode > 0) {
45-
throw new \Exception('The command exited with a non-zero status code: '.$exitCode);
46-
}
47-
48-
return [
49-
'exitCode' => $exitCode, // will always be 0
50-
'output' => $content,
51-
];
52-
});
28+
$lambda->processNextEvent($this->handler);
5329
}
5430
}
5531
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Runtime\Bref\Lambda;
4+
5+
use Bref\Context\Context;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class ContextBuilder
11+
{
12+
/** @var string */
13+
private $awsRequestId;
14+
15+
/** @var int */
16+
private $deadlineMs;
17+
18+
/** @var string */
19+
private $invokedFunctionArn;
20+
21+
/** @var string */
22+
private $traceId;
23+
24+
public function __construct()
25+
{
26+
$this->awsRequestId = '';
27+
$this->deadlineMs = 0;
28+
$this->invokedFunctionArn = '';
29+
$this->traceId = '';
30+
}
31+
32+
public function setAwsRequestId(string $awsRequestId): void
33+
{
34+
$this->awsRequestId = $awsRequestId;
35+
}
36+
37+
public function setDeadlineMs(int $deadlineMs): void
38+
{
39+
$this->deadlineMs = $deadlineMs;
40+
}
41+
42+
public function setInvokedFunctionArn(string $invokedFunctionArn): void
43+
{
44+
$this->invokedFunctionArn = $invokedFunctionArn;
45+
}
46+
47+
public function setTraceId(string $traceId): void
48+
{
49+
$this->traceId = $traceId;
50+
}
51+
52+
public function buildContext(): Context
53+
{
54+
return new Context(
55+
$this->awsRequestId,
56+
$this->deadlineMs,
57+
$this->invokedFunctionArn,
58+
$this->traceId
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)