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

feat(expressions): add const expression

This commit is contained in:
Awilum
2022-05-29 11:38:47 +03:00
parent cd55223e0c
commit 89325800e9
4 changed files with 53 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?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.
*/
namespace Flextype\Entries\Expressions;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
class ConstExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [
new ExpressionFunction('const', fn(string $const) => "defined($const) ? constant($const) : ''", fn($arguments, string $const) => defined($const) ? constant($const) : '')
];
}
}

View File

@@ -116,6 +116,9 @@ entries:
field:
enabled: true
class: "Flextype\\Entries\\Expressions\\FieldExpression"
const:
enabled: true
class: "Flextype\\Entries\\Expressions\\ConstExpression"
directives:
expressions:
enabled: true

View File

@@ -112,6 +112,9 @@ entries:
field:
enabled: true
class: "Flextype\\Entries\\Expressions\\FieldExpression"
const:
enabled: true
class: "Flextype\\Entries\\Expressions\\ConstExpression"
directives:
expressions:
enabled: true

View File

@@ -0,0 +1,17 @@
<?php
use Flextype\Component\Filesystem\Filesystem;
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/entries')->create();
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('const expression', function () {
define('test-expression-const', 'Foo');
entries()->create('const', ['test' => '[[ const("test-expression-const") ]]']);
expect(entries()->fetch('const')['test'])->toBe('Foo');
});