Skip to content

Commit ca2a9c8

Browse files
authored
Add bref-local-handler.php to invoke bref handlers locally (#71)
* Add bref-local-handler.php to invoke bref handlers locally * Dont use Invoker
1 parent 89ae52e commit ca2a9c8

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

src/bref/bin/bref-local-handler.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if ($argc < 2) {
5+
error_log("bref-local-handler.php must be invoked with at least one argument: \n./vendor/bin/bref-local-handler.php ./bin/container.php:App\\Service\\MyService [data or file path]");
6+
exit(1);
7+
}
8+
9+
/*
10+
* Set the correct runtime
11+
*/
12+
if (!isset($_SERVER['APP_RUNTIME'])) {
13+
$_SERVER['APP_RUNTIME'] = \Runtime\Bref\Runtime::class;
14+
}
15+
16+
$config = [
17+
'bref_runner_type' => 'local',
18+
];
19+
20+
/*
21+
* Handle first argument and prepare "handler"
22+
*/
23+
$handler = $argv[1];
24+
if (!isset($_SERVER['_HANDLER'])) {
25+
$_SERVER['_HANDLER'] = $handler;
26+
}
27+
28+
if (false === $pos = strpos($handler, ':')) {
29+
$file = $handler;
30+
} else {
31+
$file = substr($handler, 0, $pos);
32+
}
33+
$_SERVER['SCRIPT_FILENAME'] = $file;
34+
35+
/*
36+
* Handle second argument which is that data or a file with the data
37+
*/
38+
if (isset($argv[2])) {
39+
$json = $argv[2];
40+
if (is_file($json)) {
41+
$json = file_get_contents($json);
42+
}
43+
44+
try {
45+
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
46+
} catch (JsonException $e) {
47+
throw new Exception('The JSON provided for the event data is invalid JSON.');
48+
}
49+
50+
$config['bref_local_runner_data'] = $data;
51+
}
52+
53+
/*
54+
* Dump all options to APP_RUNTIME_OPTIONS
55+
*/
56+
$_SERVER['APP_RUNTIME_OPTIONS'] = array_merge($config, $_SERVER['APP_RUNTIME_OPTIONS'] ?? []);
57+
58+
include $file;

src/bref/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
}
3333
},
3434
"minimum-stability": "dev",
35-
"prefer-stable": true
35+
"prefer-stable": true,
36+
"bin": [
37+
"bin/bref-local-handler.php"
38+
]
3639
}

src/bref/src/LocalRunner.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Runtime\Bref;
4+
5+
use Bref\Context\Context;
6+
use Bref\Event\Handler;
7+
use Symfony\Component\Runtime\RunnerInterface;
8+
9+
/**
10+
* This will run BrefHandlers for local development.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class LocalRunner implements RunnerInterface
15+
{
16+
private $handler;
17+
private $data;
18+
19+
public function __construct(Handler $handler, $data)
20+
{
21+
$this->handler = $handler;
22+
$this->data = $data;
23+
}
24+
25+
public function run(): int
26+
{
27+
$this->handler->handle($this->data, new Context('', 0, '', ''));
28+
29+
return 0;
30+
}
31+
}

src/bref/src/Runtime.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414

1515
class Runtime extends SymfonyRuntime
1616
{
17+
private $handlerRunnerClass;
18+
1719
/**
1820
* @param array{
1921
* bref_loop_max?: int,
22+
* bref_runner_type?: string,
23+
* bref_local_runner_data?: mixed,
2024
* } $options
2125
*/
2226
public function __construct(array $options = [])
2327
{
2428
$options['bref_loop_max'] = $options['bref_loop_max'] ?? $_SERVER['BREF_LOOP_MAX'] ?? $_ENV['BREF_LOOP_MAX'] ?? 1;
29+
$options['bref_runner_type'] = $options['bref_runner_type'] ?? $_SERVER['BREF_RUNNER_TYPE'] ?? $_ENV['BREF_RUNNER_TYPE'] ?? 'aws';
30+
$options['bref_local_runner_data'] = $options['bref_local_runner_data'] ?? $_SERVER['BREF_LOCAL_RUNNER_DATA'] ?? $_ENV['BREF_LOCAL_RUNNER_DATA'] ?? [];
2531
parent::__construct($options);
2632
}
2733

@@ -48,7 +54,13 @@ public function getRunner(?object $application): RunnerInterface
4854
}
4955

5056
if ($application instanceof Handler) {
51-
return new BrefRunner($application, $this->options['bref_loop_max']);
57+
if ('aws' === $this->options['bref_runner_type']) {
58+
return new BrefRunner($application, $this->options['bref_loop_max']);
59+
} elseif ('local' === $this->options['bref_runner_type']) {
60+
return new LocalRunner($application, $this->options['bref_local_runner_data']);
61+
} else {
62+
throw new \InvalidArgumentException(sprintf('Value "%s" of "bref_runner_type" is not supported.', $this->options['bref_runner_type']));
63+
}
5264
}
5365

5466
if ($application instanceof Application) {

0 commit comments

Comments
 (0)