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

feat(expressions): improve entries expression #582

This commit is contained in:
Awilum
2022-09-02 12:41:01 +03:00
parent 99806c2acb
commit ffa27d63ef
4 changed files with 189 additions and 6 deletions

View File

@@ -21,7 +21,9 @@ use Glowy\Macroable\Macroable;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use function Flextype\registry;
use function Flextype\entries;
use function Flextype\collection;
class EntriesExpression implements ExpressionFunctionProviderInterface
{
@@ -47,6 +49,10 @@ class EntriesExpressionsMethods
*/
public function fetch(string $id, array $options = []): \Glowy\Arrays\Arrays
{
if (! registry()->get('flextype.settings.entries.expressions.entries.fetch.enabled')) {
return collection();
}
// Backup current entry data
$original = entries()->registry()->get('methods.fetch');
@@ -68,6 +74,10 @@ class EntriesExpressionsMethods
*/
public function registry(): \Glowy\Arrays\Arrays
{
if (! registry()->get('flextype.settings.entries.expressions.entries.registry.enabled')) {
return collection();
}
return entries()->registry();
}
@@ -82,6 +92,104 @@ class EntriesExpressionsMethods
*/
public function has(string $id): bool
{
if (! registry()->get('flextype.settings.entries.expressions.entries.has.enabled')) {
return false;
}
return entries()->has($id);
}
/**
* Move entry.
*
* @param string $id Unique identifier of the entry.
* @param string $newID New Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function move(string $id, string $newID): bool
{
if (! registry()->get('flextype.settings.entries.expressions.entries.move.enabled')) {
return false;
}
return entries()->move($id, $newID);
}
/**
* Update entry.
*
* @param string $id Unique identifier of the entry.
* @param array $data Data to update for the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function update(string $id, array $data): bool
{
if (! registry()->get('flextype.settings.entries.expressions.entries.update.enabled')) {
return false;
}
return entries()->update($id, $data);
}
/**
* Create entry.
*
* @param string $id Unique identifier of the entry.
* @param array $data Data to create for the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function create(string $id, array $data = []): bool
{
if (! registry()->get('flextype.settings.entries.expressions.entries.create.enabled')) {
return false;
}
return entries()->create($id, $data);
}
/**
* Delete entry.
*
* @param string $id Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function delete(string $id): bool
{
if (! registry()->get('flextype.settings.entries.expressions.entries.delete.enabled')) {
return false;
}
return entries()->delete($id);
}
/**
* Copy entry.
*
* @param string $id Unique identifier of the entry.
* @param string $newID New Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function copy(string $id, string $newID): bool
{
if (! registry()->get('flextype.settings.entries.expressions.entries.copy.enabled')) {
return false;
}
return entries()->copy($id, $newID);
}
}

View File

@@ -94,6 +94,20 @@ entries:
entries:
enabled: true
class: "Flextype\\Entries\\Expressions\\EntriesExpression"
fetch:
enabled: true
has:
enabled: true
registry:
enabled: true
create:
enabled: false
move:
enabled: false
update:
enabled: false
delete:
enabled: false
fetch:
enabled: true
class: "Flextype\\Entries\\Expressions\\FetchExpression"

View File

@@ -94,6 +94,20 @@ entries:
entries:
enabled: true
class: "Flextype\\Entries\\Expressions\\EntriesExpression"
fetch:
enabled: true
has:
enabled: true
registry:
enabled: true
create:
enabled: false
move:
enabled: false
update:
enabled: false
delete:
enabled: false
fetch:
enabled: true
class: "Flextype\\Entries\\Expressions\\FetchExpression"

View File

@@ -3,6 +3,7 @@
use Flextype\Component\Filesystem\Filesystem;
use function Glowy\Filesystem\filesystem;
use function Flextype\entries;
use function Flextype\registry;
beforeEach(function() {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->create();
@@ -13,11 +14,57 @@ afterEach(function (): void {
});
test('entries expression', function () {
// fetch
entries()->create('foo', ['title' => 'Foo']);
entries()->create('entries', ['test' => '[[ entries().fetch("foo").get("title") ]]',
'test2' => '(type:bool) [[ entries().has("foo") ]]',
'test3' => '(type:bool) [[ entries().has("bar") ]]']);
expect(entries()->fetch('entries')['test'])->toBe('Foo');
expect(entries()->fetch('entries')['test2'])->toBe(true);
expect(entries()->fetch('entries')['test3'])->toBe(false);
entries()->create('fetch', ['test' => '[[ entries().fetch("foo").get("title") ]]']);
expect(entries()->fetch('fetch')['test'])->toBe('Foo');
// has
entries()->create('has', [
'test1' => '(type:bool) [[ entries().has("foo") ]]',
'test2' => '(type:bool) [[ entries().has("bar") ]]',
]);
expect(entries()->fetch('has')['test1'])->toBe(true);
expect(entries()->fetch('has')['test2'])->toBe(false);
// delete
entries()->create('delete', [
'test' => '(type:bool) [[ entries().delete("foo") ]]',
]);
registry()->set('flextype.settings.entries.expressions.entries.delete.enabled', true);
expect(entries()->fetch('delete')['test'])->toBeTrue();
registry()->set('flextype.settings.entries.expressions.entries.delete.enabled', false);
// copy
entries()->create('copy-foo');
entries()->create('copy', [
'test' => '(type:bool) [[ entries().copy("copy-foo", "copy-foo-2") ]]',
]);
registry()->set('flextype.settings.entries.expressions.entries.copy.enabled', true);
expect(entries()->fetch('copy')['test'])->toBeTrue();
registry()->set('flextype.settings.entries.expressions.entries.copy.enabled', false);
// move
entries()->create('move-foo');
entries()->create('move', [
'test' => '(type:bool) [[ entries().move("move-foo", "move-foo-2") ]]',
]);
registry()->set('flextype.settings.entries.expressions.entries.move.enabled', true);
expect(entries()->fetch('move')['test'])->toBeTrue();
registry()->set('flextype.settings.entries.expressions.entries.move.enabled', false);
// update
entries()->create('update-foo');
entries()->create('update', [
'test' => '(type:bool) [[ entries().update("update-foo", {"title": "Foo"}) ]]',
]);
registry()->set('flextype.settings.entries.expressions.entries.update.enabled', true);
expect(entries()->fetch('update')['test'])->toBeTrue();
expect(entries()->fetch('update-foo')['title'])->toBe('Foo');
registry()->set('flextype.settings.entries.expressions.entries.update.enabled', false);
});