From c1c9bcde7538c2dcf2122c0ca9fbc38f2a40e979 Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 13 May 2022 15:27:17 +0300 Subject: [PATCH] feat(directives): add `vars` directive --- .../core/Entries/Directives/VarsDirective.php | 36 +++++++++++++++++++ src/flextype/settings.yaml | 8 +++++ tests/fixtures/settings/settings.yaml | 8 +++++ .../Entries/Directives/VarsDirectiveTest.php | 17 +++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/flextype/core/Entries/Directives/VarsDirective.php create mode 100644 tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php diff --git a/src/flextype/core/Entries/Directives/VarsDirective.php b/src/flextype/core/Entries/Directives/VarsDirective.php new file mode 100644 index 00000000..e9fb6dcc --- /dev/null +++ b/src/flextype/core/Entries/Directives/VarsDirective.php @@ -0,0 +1,36 @@ +addListener('onEntriesFetchSingleField', static function (): void { + + if (! registry()->get('flextype.settings.entries.directives.vars.enabled')) { + return; + } + + $field = entries()->registry()->get('methods.fetch.field'); + $result = entries()->registry()->get('methods.fetch.result'); + + if (is_string($field['value'])) { + $field['value'] = preg_replace_callback('/@var\((.*?)\)/', function($matches) use ($result) { + return collection($result['vars'])->get($matches[1]); + }, $field['value']); + } + + entries()->registry()->set('methods.fetch.field.key', $field['key']); + entries()->registry()->set('methods.fetch.field.value', $field['value']); +}); \ No newline at end of file diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index f31736c4..3b598daf 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -77,6 +77,8 @@ entries: parsers: shortcodes: enabled: true + vars: + debug: false directives: markdown: enabled: true @@ -93,8 +95,14 @@ entries: fields: enabled: true path: "/src/flextype/core/Entries/Directives/FieldsDirective.php" + vars: + enabled: true + path: "/src/flextype/core/Entries/Directives/VarsDirective.php" macros: debug: false + vars: + enabled: true + path: "/src/flextype/core/Entries/Macros/VarsMacros.php" php: enabled: true path: "/src/flextype/core/Entries/Macros/PhpMacros.php" diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 4805188c..0be4cc09 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -73,6 +73,8 @@ entries: parsers: shortcodes: enabled: true + vars: + debug: false directives: markdown: enabled: true @@ -89,8 +91,14 @@ entries: fields: enabled: true path: "/src/flextype/core/Entries/Directives/FieldsDirective.php" + vars: + enabled: true + path: "/src/flextype/core/Entries/Directives/VarsDirective.php" macros: debug: false + vars: + enabled: true + path: "/src/flextype/core/Entries/Macros/VarsMacros.php" php: enabled: true path: "/src/flextype/core/Entries/Macros/PhpMacros.php" diff --git a/tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php new file mode 100644 index 00000000..8716b1f3 --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/VarsDirectiveTest.php @@ -0,0 +1,17 @@ +directory(PATH['project'] . '/entries')->create(); +}); + +afterEach(function (): void { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('vars directive', function () { + entries()->create('type-vars', ['vars' => ['foo' => 'Foo'], 'title' => '@var(foo)']); + + $this->assertEquals('Foo', entries()->fetch('type-vars')['title']); +}); \ No newline at end of file