1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 06:06:45 +02:00

feat(directives): add '@calc' directive

This commit is contained in:
Awilum
2022-05-13 20:50:39 +03:00
parent 2dc20b9f54
commit 8e050eefbe
5 changed files with 73 additions and 13 deletions

View File

@@ -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",

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
use Glowy\Arrays\Arrays as Collection;
use ChrisKonnertz\StringCalc\StringCalc;
emitter()->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']);
});

View File

@@ -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:

View File

@@ -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:

View File

@@ -0,0 +1,16 @@
<?php
use Flextype\Component\Filesystem\Filesystem;
beforeEach(function() {
filesystem()->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']);
});