From b794e64386801f3275b245c42203c5ace21d86b6 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 30 Aug 2022 21:04:53 +0300 Subject: [PATCH] feat(expressions): add new `fetch` expression --- .../Entries/Expressions/FetchExpression.php | 57 +++++++++++++++++++ .../Expressions/FetchExpressionTest.php | 41 +++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/flextype/core/Entries/Expressions/FetchExpression.php create mode 100644 tests/src/flextype/core/Entries/Expressions/FetchExpressionTest.php diff --git a/src/flextype/core/Entries/Expressions/FetchExpression.php b/src/flextype/core/Entries/Expressions/FetchExpression.php new file mode 100644 index 00000000..576540b5 --- /dev/null +++ b/src/flextype/core/Entries/Expressions/FetchExpression.php @@ -0,0 +1,57 @@ + '(new \Flextype\Entries\Expressions\FetchExpressionsMethods())->fetch($resource, $options);', static fn ($arguments, string $resource, array $options = []) => (new \Flextype\Entries\Expressions\FetchExpressionsMethods())->fetch($resource, $options))]; + } +} + +class FetchExpressionsMethods +{ + use Macroable; + + /** + * Fetch data from entries, local files and urls. + * + * @param string $resource A resource that you wish to fetch. + * @param array $options Options. + * @return Glowy\Arrays\Arrays|GuzzleHttp\Psr7\Response Returns the data from the resource or empty collection on failure. + */ + function fetch(string $resource, array $options = []) + { + // Backup current entry data + $original = entries()->registry()->get('methods.fetch'); + + // Do fetch the data from the resource. + $result = fetch($resource, $options); + + // Restore original entry data + entries()->registry()->set('methods.fetch', $original); + + return $result; + } +} \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Expressions/FetchExpressionTest.php b/tests/src/flextype/core/Entries/Expressions/FetchExpressionTest.php new file mode 100644 index 00000000..5f8dcd7a --- /dev/null +++ b/tests/src/flextype/core/Entries/Expressions/FetchExpressionTest.php @@ -0,0 +1,41 @@ +directory(FLEXTYPE_PATH_PROJECT . '/entries')->create(); +}); + +afterEach(function (): void { + filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->delete(); +}); + +test('fetch expression', function () { + + // 1. + $this->assertTrue(entries()->create('data', ['title' => 'Foo!', 'category' => 'foo'])); + $this->assertTrue(entries()->create('foo-test', ['test1' => "[[ fetch('data').get('title') ]]", + 'test2' => "[[ fetch('data').toJson() ]]", + 'test3' => "[[ fetch('data').get('foo', 'FooDefault') ]]"])); + + $foo = entries()->fetch('foo-test'); + + expect($foo['test1'])->toBe('Foo!'); + expect($foo['test2'])->toBeJson(); + expect($foo['test3'])->toBe('FooDefault'); + + // 2. + $this->assertTrue(entries()->create('bar-test', ['test1' => "[[ fetch(const('FLEXTYPE_ROOT_DIR') ~ '/src/flextype/flextype.yaml').get('name') ]]", + 'test2' => "[[ fetch(const('FLEXTYPE_ROOT_DIR') ~ '/src/flextype/flextype.yaml').toJson() ]]", + 'test3' => "[[ fetch(const('FLEXTYPE_ROOT_DIR') ~ '/src/flextype/flextype.yaml').get('boo', 'Boo') ]]"])); + + $bar = entries()->fetch('bar-test'); + + expect($bar['test1'])->toBe('Flextype'); + expect($bar['test2'])->toBeJson(); + expect($bar['test3'])->toBe('Boo'); + + // 3. @TODO test for fetch data from the urls... +}); \ No newline at end of file