Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flags should have consitent api #1634

Closed
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions docs/function/local-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,19 @@ Instead, you can use the [`bref/dev-server`](https://github.com/brefphp/dev-serv
If you do not use `serverless.yml` but something else, like SAM/AWS CDK/Terraform, use the `vendor/bin/bref-local` command instead:

```bash
$ vendor/bin/bref-local <handler> <event-data>

# For example
$ vendor/bin/bref-local my-function.php
$ vendor/bin/bref-local -f my-function.php
Hello world

# With JSON event data
$ vendor/bin/bref-local my-function.php '{"name": "Jane"}'
$ vendor/bin/bref-local -f my-function.php --data '{"name": "Fred"}'
Hello Jane

# With a path to a file containing a JSON event.
# With JSON in a file
$ cat event.json
{
"name": "Alex"
}
$ vendor/bin/bref-local --path event.json my-function.php
$ vendor/bin/bref-local -f my-function.php --path event.json
Hello Alex
```

Expand Down
25 changes: 21 additions & 4 deletions src/bref-local
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,36 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../../../autoload.php';
}

$handler = $argv[1] ?? '';
$data = $argv[2] ?? null;
$opts = getopt('', ['path:']);
$opts = getopt('f:', ['path:', 'data:']);
$handler = $opts['f'] ?? null;
$data = $opts['data'] ?? null;

// Retains backward compatibilty with `./vendor/bin/bref-local <handler> <event-data>`
if (empty($opts)) {
$handler = $argv[1] ?? '';
$data = $argv[2] ?? null;
}

if (isset($opts['path'])) {
if (! file_exists($opts['path'])) {
throw new Exception('The file ' . $opts['path'] . ' could not be found.');
}

$handler = $argv[array_key_last($argv)];
$data = file_get_contents($opts['path']);
}
theoboldalex marked this conversation as resolved.
Show resolved Hide resolved

if (isset($opts['f'])) {
if (! file_exists($opts['f'])) {
theoboldalex marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception('Handler file ' . $opts['f'] . ' could not be found.');
}

if (! isset($opts['path']) && isset($opts['data'])) {
$data = $opts['data'];
}
theoboldalex marked this conversation as resolved.
Show resolved Hide resolved

$handler = $opts['f'];
}

try {
$handler = Bref::getContainer()->get($handler);
} catch (NotFoundExceptionInterface $e) {
Expand Down
Loading