mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 06:06:45 +02:00
feat(expressions): add new fetch
expression
This commit is contained in:
57
src/flextype/core/Entries/Expressions/FetchExpression.php
Normal file
57
src/flextype/core/Entries/Expressions/FetchExpression.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?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 Glowy\Macroable\Macroable;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
use function Flextype\fetch;
|
||||
use function Flextype\entries;
|
||||
|
||||
class FetchExpression implements ExpressionFunctionProviderInterface
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return [new ExpressionFunction('fetch', static fn (string $resource, array $options = []) => '(new \Flextype\Entries\Expressions\FetchExpressionsMethods())->fetch($resource, $options);', static fn ($arguments, string $resource, array $options = []) => (new \Flextype\Entries\Expressions\FetchExpressionsMethods())->fetch($resource, $options))];
|
||||
}
|
||||
}
|
||||
|
||||
class FetchExpressionsMethods
|
||||
{
|
||||
use Macroable;
|
||||
|
||||
/**
|
||||
* Fetch data from entries, local files and urls.
|
||||
*
|
||||
* @param string $resource A resource that you wish to fetch.
|
||||
* @param array $options Options.
|
||||
* @return Glowy\Arrays\Arrays|GuzzleHttp\Psr7\Response Returns the data from the resource or empty collection on failure.
|
||||
*/
|
||||
function fetch(string $resource, array $options = [])
|
||||
{
|
||||
// Backup current entry data
|
||||
$original = entries()->registry()->get('methods.fetch');
|
||||
|
||||
// Do fetch the data from the resource.
|
||||
$result = fetch($resource, $options);
|
||||
|
||||
// Restore original entry data
|
||||
entries()->registry()->set('methods.fetch', $original);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use function Glowy\Filesystem\filesystem;
|
||||
use function Flextype\entries;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('fetch expression', function () {
|
||||
|
||||
// 1.
|
||||
$this->assertTrue(entries()->create('data', ['title' => 'Foo!', 'category' => 'foo']));
|
||||
$this->assertTrue(entries()->create('foo-test', ['test1' => "[[ fetch('data').get('title') ]]",
|
||||
'test2' => "[[ fetch('data').toJson() ]]",
|
||||
'test3' => "[[ fetch('data').get('foo', 'FooDefault') ]]"]));
|
||||
|
||||
$foo = entries()->fetch('foo-test');
|
||||
|
||||
expect($foo['test1'])->toBe('Foo!');
|
||||
expect($foo['test2'])->toBeJson();
|
||||
expect($foo['test3'])->toBe('FooDefault');
|
||||
|
||||
// 2.
|
||||
$this->assertTrue(entries()->create('bar-test', ['test1' => "[[ fetch(const('FLEXTYPE_ROOT_DIR') ~ '/src/flextype/flextype.yaml').get('name') ]]",
|
||||
'test2' => "[[ fetch(const('FLEXTYPE_ROOT_DIR') ~ '/src/flextype/flextype.yaml').toJson() ]]",
|
||||
'test3' => "[[ fetch(const('FLEXTYPE_ROOT_DIR') ~ '/src/flextype/flextype.yaml').get('boo', 'Boo') ]]"]));
|
||||
|
||||
$bar = entries()->fetch('bar-test');
|
||||
|
||||
expect($bar['test1'])->toBe('Flextype');
|
||||
expect($bar['test2'])->toBeJson();
|
||||
expect($bar['test3'])->toBe('Boo');
|
||||
|
||||
// 3. @TODO test for fetch data from the urls...
|
||||
});
|
Reference in New Issue
Block a user