From 3bcb87d4b3d62776c5710f7a32b16ca022c6735f Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 00:07:16 +0100 Subject: [PATCH 01/16] Adds short flag for handler --- src/bref-local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bref-local b/src/bref-local index 63e77d5ac..c03ed66e5 100755 --- a/src/bref-local +++ b/src/bref-local @@ -18,7 +18,7 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { $handler = $argv[1] ?? ''; $data = $argv[2] ?? null; -$opts = getopt('', ['path:']); +$opts = getopt('f:', ['path:']); if (isset($opts['path'])) { if (! file_exists($opts['path'])) { From 06d4780649b85ff15f8df96e9ab27b2818ec811f Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 00:13:08 +0100 Subject: [PATCH 02/16] Set handler to f flag value if passed --- src/bref-local | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bref-local b/src/bref-local index c03ed66e5..1b4fc0d96 100755 --- a/src/bref-local +++ b/src/bref-local @@ -29,6 +29,10 @@ if (isset($opts['path'])) { $data = file_get_contents($opts['path']); } +if (isset($opts['f'])) { + $handler = $opts['f']; +} + try { $handler = Bref::getContainer()->get($handler); } catch (NotFoundExceptionInterface $e) { From 22a902e2435e765e3ed855b4e5a31ccebae31eb7 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 00:13:42 +0100 Subject: [PATCH 03/16] Handles non existant handler files --- src/bref-local | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bref-local b/src/bref-local index 1b4fc0d96..9c4af89c5 100755 --- a/src/bref-local +++ b/src/bref-local @@ -30,6 +30,10 @@ if (isset($opts['path'])) { } if (isset($opts['f'])) { + if (! file_exists($opts['f'])) { + throw new Exception('Handler file ' . $opts['f'] . ' could not be found.'); + } + $handler = $opts['f']; } From 5cb031ff9640d916981f11043cf5ca5acea46588 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 00:17:52 +0100 Subject: [PATCH 04/16] Sometimes only -f will be passed --- src/bref-local | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bref-local b/src/bref-local index 9c4af89c5..e1946dcc5 100755 --- a/src/bref-local +++ b/src/bref-local @@ -34,6 +34,10 @@ if (isset($opts['f'])) { throw new Exception('Handler file ' . $opts['f'] . ' could not be found.'); } + if (! isset($opts['path'])) { + $data = $argv[3] ?? null; + } + $handler = $opts['f']; } From c1caed8ecb4914eb51edb7d4c6ca926a02a16be6 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 14:39:22 +0100 Subject: [PATCH 05/16] Documents -f flag --- docs/function/local-development.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/docs/function/local-development.md b/docs/function/local-development.md index 6fe40633e..938a60c5f 100644 --- a/docs/function/local-development.md +++ b/docs/function/local-development.md @@ -71,14 +71,11 @@ 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 - -# 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 '{"name": "Jane"}' Hello Jane # With a path to a file containing a JSON event. From 7d288a3fe37b98cedbb1c7365adec21d4b210506 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 14:41:21 +0100 Subject: [PATCH 06/16] Swaps arg order --- docs/function/local-development.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/function/local-development.md b/docs/function/local-development.md index 938a60c5f..1dad979e0 100644 --- a/docs/function/local-development.md +++ b/docs/function/local-development.md @@ -83,7 +83,7 @@ $ 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 ``` From b157565004496037865f00e6ba5770307f83eaf0 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 15:10:54 +0100 Subject: [PATCH 07/16] Removes indexing into argv --- src/bref-local | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/bref-local b/src/bref-local index e1946dcc5..bbcec7986 100755 --- a/src/bref-local +++ b/src/bref-local @@ -16,16 +16,15 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../../../autoload.php'; } -$handler = $argv[1] ?? ''; -$data = $argv[2] ?? null; -$opts = getopt('f:', ['path:']); +$opts = getopt('f:', ['path:', 'data:']); +$data = $opts['data'] ?? null; +$handler = $opts['f'] ?? 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']); } @@ -34,8 +33,8 @@ if (isset($opts['f'])) { throw new Exception('Handler file ' . $opts['f'] . ' could not be found.'); } - if (! isset($opts['path'])) { - $data = $argv[3] ?? null; + if (! isset($opts['path']) && isset($opts['data'])) { + $data = $opts['data']; } $handler = $opts['f']; From fb081290a62037d6b2b5562ae0ca2ed5b4ed0e40 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 15:22:01 +0100 Subject: [PATCH 08/16] Provides backward compatability --- src/bref-local | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bref-local b/src/bref-local index bbcec7986..57f23f085 100755 --- a/src/bref-local +++ b/src/bref-local @@ -20,6 +20,12 @@ $opts = getopt('f:', ['path:', 'data:']); $data = $opts['data'] ?? null; $handler = $opts['f'] ?? null; +// Retains backward compatibilty with `./vendor/bin/bref-local ` +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.'); From 2a528e8c6f53b1c93a8fff4a909447ee6cd83533 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 15:24:14 +0100 Subject: [PATCH 09/16] Handler should defualt to empty string not null --- src/bref-local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bref-local b/src/bref-local index 57f23f085..3c9212f9c 100755 --- a/src/bref-local +++ b/src/bref-local @@ -17,8 +17,8 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { } $opts = getopt('f:', ['path:', 'data:']); +$handler = $opts['f'] ?? ''; $data = $opts['data'] ?? null; -$handler = $opts['f'] ?? null; // Retains backward compatibilty with `./vendor/bin/bref-local ` if (empty($opts)) { From 6b4bf66daf2195d8af693a6acdd837aff1764557 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 15:25:35 +0100 Subject: [PATCH 10/16] Handler deafult to null to avoid extra call --- src/bref-local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bref-local b/src/bref-local index 3c9212f9c..878e0b163 100755 --- a/src/bref-local +++ b/src/bref-local @@ -17,7 +17,7 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php')) { } $opts = getopt('f:', ['path:', 'data:']); -$handler = $opts['f'] ?? ''; +$handler = $opts['f'] ?? null; $data = $opts['data'] ?? null; // Retains backward compatibilty with `./vendor/bin/bref-local ` From fa50b1e95e1ce3f11bea209d8f41e3b8a0b1e349 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Sat, 2 Sep 2023 15:33:59 +0100 Subject: [PATCH 11/16] Updates docs for local invocation without sls --- docs/function/local-development.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/function/local-development.md b/docs/function/local-development.md index 1dad979e0..028b13033 100644 --- a/docs/function/local-development.md +++ b/docs/function/local-development.md @@ -75,15 +75,15 @@ $ vendor/bin/bref-local -f my-function.php Hello world # With JSON event data -$ vendor/bin/bref-local -f 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 -f my-function.php --path event.json +$ vendor/bin/bref-local -f my-function.php --path event.json Hello Alex ``` From 31a7c18d5d8bb789e42b5c29718bfc2f86fdc909 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Thu, 14 Sep 2023 21:50:01 +0100 Subject: [PATCH 12/16] Reverts to old style as last resort --- src/bref-local | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bref-local b/src/bref-local index 878e0b163..6cd1b7ce3 100755 --- a/src/bref-local +++ b/src/bref-local @@ -20,12 +20,6 @@ $opts = getopt('f:', ['path:', 'data:']); $handler = $opts['f'] ?? null; $data = $opts['data'] ?? null; -// Retains backward compatibilty with `./vendor/bin/bref-local ` -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.'); @@ -46,6 +40,12 @@ if (isset($opts['f'])) { $handler = $opts['f']; } +// Retains backward compatibilty with `./vendor/bin/bref-local ` +if (empty($opts)) { + $handler = $argv[1] ?? ''; + $data = $argv[2] ?? null; +} + try { $handler = Bref::getContainer()->get($handler); } catch (NotFoundExceptionInterface $e) { From 7e28c640ff770c2a83739b2e25887074eda7064d Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Thu, 14 Sep 2023 21:51:00 +0100 Subject: [PATCH 13/16] Deals with handler first --- src/bref-local | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/bref-local b/src/bref-local index 6cd1b7ce3..21563e2c5 100755 --- a/src/bref-local +++ b/src/bref-local @@ -20,14 +20,6 @@ $opts = getopt('f:', ['path:', 'data:']); $handler = $opts['f'] ?? null; $data = $opts['data'] ?? null; -if (isset($opts['path'])) { - if (! file_exists($opts['path'])) { - throw new Exception('The file ' . $opts['path'] . ' could not be found.'); - } - - $data = file_get_contents($opts['path']); -} - if (isset($opts['f'])) { if (! file_exists($opts['f'])) { throw new Exception('Handler file ' . $opts['f'] . ' could not be found.'); @@ -40,6 +32,14 @@ if (isset($opts['f'])) { $handler = $opts['f']; } +if (isset($opts['path'])) { + if (! file_exists($opts['path'])) { + throw new Exception('The file ' . $opts['path'] . ' could not be found.'); + } + + $data = file_get_contents($opts['path']); +} + // Retains backward compatibilty with `./vendor/bin/bref-local ` if (empty($opts)) { $handler = $argv[1] ?? ''; From e7592ae8bb8abf64083ee5a8b0109a52b5330ac1 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Thu, 14 Sep 2023 21:55:04 +0100 Subject: [PATCH 14/16] Makes path and data flags exclusive --- src/bref-local | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/bref-local b/src/bref-local index 21563e2c5..d5d99990a 100755 --- a/src/bref-local +++ b/src/bref-local @@ -20,24 +20,18 @@ $opts = getopt('f:', ['path:', 'data:']); $handler = $opts['f'] ?? null; $data = $opts['data'] ?? null; -if (isset($opts['f'])) { - if (! file_exists($opts['f'])) { - throw new Exception('Handler file ' . $opts['f'] . ' could not be found.'); - } - - if (! isset($opts['path']) && isset($opts['data'])) { - $data = $opts['data']; - } - - $handler = $opts['f']; +if ($handler !== null && ! is_file($opts['f'])) { + throw new Exception('Handler file ' . $handler . ' could not be found.'); } if (isset($opts['path'])) { - if (! file_exists($opts['path'])) { + if (! is_file($opts['path'])) { throw new Exception('The file ' . $opts['path'] . ' could not be found.'); } $data = file_get_contents($opts['path']); +} elseif (isset($opts['data'])) { + $data = $opts['data']; } // Retains backward compatibilty with `./vendor/bin/bref-local ` From 6516f66383cab7d3db1ceee077c2d98f8f49cb13 Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Thu, 14 Sep 2023 21:56:36 +0100 Subject: [PATCH 15/16] Use handler var --- src/bref-local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bref-local b/src/bref-local index d5d99990a..4b5f07dc2 100755 --- a/src/bref-local +++ b/src/bref-local @@ -20,7 +20,7 @@ $opts = getopt('f:', ['path:', 'data:']); $handler = $opts['f'] ?? null; $data = $opts['data'] ?? null; -if ($handler !== null && ! is_file($opts['f'])) { +if ($handler !== null && ! is_file($handler)) { throw new Exception('Handler file ' . $handler . ' could not be found.'); } From 45d0750d6c9596929eb189b99bb8473d4ce261be Mon Sep 17 00:00:00 2001 From: theoboldalex Date: Thu, 14 Sep 2023 22:06:49 +0100 Subject: [PATCH 16/16] Elseif check is not required --- src/bref-local | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bref-local b/src/bref-local index 4b5f07dc2..9dcc9c516 100755 --- a/src/bref-local +++ b/src/bref-local @@ -30,8 +30,6 @@ if (isset($opts['path'])) { } $data = file_get_contents($opts['path']); -} elseif (isset($opts['data'])) { - $data = $opts['data']; } // Retains backward compatibilty with `./vendor/bin/bref-local `