From 8e050eefbee19b283f2aaf01757b6899a540912c Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 13 May 2022 20:50:39 +0300 Subject: [PATCH] feat(directives): add '@calc' directive --- composer.json | 3 +- .../core/Entries/Directives/CalcDirective.php | 37 +++++++++++++++++++ src/flextype/settings.yaml | 15 +++++--- tests/fixtures/settings/settings.yaml | 15 +++++--- .../Entries/Directives/CalcDirectiveTest.php | 16 ++++++++ 5 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 src/flextype/core/Entries/Directives/CalcDirective.php create mode 100644 tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php diff --git a/composer.json b/composer.json index 242d7d9d..1017fc95 100755 --- a/composer.json +++ b/composer.json @@ -62,7 +62,8 @@ "symfony/var-exporter": "^5.4.3", "thermage/thermage": "^0.19.0", "colinodell/json5": "^2.2", - "netcarver/textile": "^3.7" + "netcarver/textile": "^3.7", + "chriskonnertz/string-calc": "^1.0" }, "suggest": { "ext-zend-opcache": "Recommended for better performance", diff --git a/src/flextype/core/Entries/Directives/CalcDirective.php b/src/flextype/core/Entries/Directives/CalcDirective.php new file mode 100644 index 00000000..04e9417a --- /dev/null +++ b/src/flextype/core/Entries/Directives/CalcDirective.php @@ -0,0 +1,37 @@ +addListener('onEntriesFetchSingleField', static function (): void { + + if (! registry()->get('flextype.settings.entries.directives.calc.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('/@calc\((.*?)\)/', function($matches) use ($result) { + return (new StringCalc())->calculate($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 4c933895..a37b98cd 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -80,12 +80,6 @@ entries: vars: debug: false directives: - markdown: - enabled: true - path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" - textile: - enabled: true - path: "/src/flextype/core/Entries/Directives/TextileDirective.php" shortcodes: enabled: true path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php" @@ -101,6 +95,15 @@ entries: vars: enabled: true path: "/src/flextype/core/Entries/Directives/VarsDirective.php" + calc: + enabled: true + path: "/src/flextype/core/Entries/Directives/CalcDirective.php" + markdown: + enabled: true + path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" + textile: + enabled: true + path: "/src/flextype/core/Entries/Directives/TextileDirective.php" macros: debug: false vars: diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 4707ecda..eee2830a 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -76,12 +76,6 @@ entries: vars: debug: false directives: - markdown: - enabled: true - path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" - textile: - enabled: true - path: "/src/flextype/core/Entries/Directives/TextileDirective.php" shortcodes: enabled: true path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php" @@ -97,6 +91,15 @@ entries: vars: enabled: true path: "/src/flextype/core/Entries/Directives/VarsDirective.php" + calc: + enabled: true + path: "/src/flextype/core/Entries/Directives/CalcDirective.php" + markdown: + enabled: true + path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" + textile: + enabled: true + path: "/src/flextype/core/Entries/Directives/TextileDirective.php" macros: debug: false vars: diff --git a/tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php new file mode 100644 index 00000000..d1fa3257 --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/CalcDirectiveTest.php @@ -0,0 +1,16 @@ +directory(PATH['project'] . '/entries')->create(); +}); + +afterEach(function (): void { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('calc directive', function () { + entries()->create('field', ['foo' => '@calc(2+2)']); + $this->assertEquals(4, entries()->fetch('field')['foo']); +}); \ No newline at end of file