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

feat(expressions): add new field and var expressions

This commit is contained in:
Awilum
2022-05-29 11:11:53 +03:00
parent da3b93b940
commit c3619c65b4
6 changed files with 104 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 FieldExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [
new ExpressionFunction('field', fn(string $field) => "entries()->registry()->get('methods.fetch.result.' . $field . ')'", fn($arguments, string $field) => entries()->registry()->get('methods.fetch.result.' . $field))
];
}
}

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 VarExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [
new ExpressionFunction('var', fn(string $var) => "entries()->registry()->get('methods.fetch.result.vars.' . $var . ')'", fn($arguments, string $var) => entries()->registry()->get('methods.fetch.result.vars.' . $var))
];
}
}

View File

@@ -110,6 +110,12 @@ entries:
csrf:
enabled: true
class: "Flextype\\Entries\\Expressions\\CsrfExpression"
var:
enabled: true
class: "Flextype\\Entries\\Expressions\\VarExpression"
field:
enabled: true
class: "Flextype\\Entries\\Expressions\\FieldExpression"
directives:
expressions:
enabled: true

View File

@@ -106,6 +106,12 @@ entries:
csrf:
enabled: true
class: "Flextype\\Entries\\Expressions\\CsrfExpression"
var:
enabled: true
class: "Flextype\\Entries\\Expressions\\VarExpression"
field:
enabled: true
class: "Flextype\\Entries\\Expressions\\FieldExpression"
directives:
expressions:
enabled: true

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('field expression', function () {
entries()->create('field', ['test' => '[[ field("id") ]]']);
expect(entries()->fetch('field')['test'])->toBe('field');
});

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('var expression', function () {
entries()->create('var', ['vars' => ['foo' => 'Foo'], 'test' => '[[ var("foo") ]]']);
expect(entries()->fetch('var')['test'])->toBe('Foo');
});