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

feat(helpers): add shortcode fetch for fetch helper #581

This commit is contained in:
Awilum
2022-08-28 21:20:11 +03:00
parent 691b529e3c
commit 2e99d68d4b
2 changed files with 158 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
<?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\Parsers\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use GuzzleHttp\Psr7\Response;
use function array_keys;
use function Flextype\fetch;
use function Flextype\collection;
use function Flextype\collectionFromJson;
use function Flextype\entries;
use function Flextype\parsers;
use function Flextype\registry;
use function Glowy\Strings\strings;
use function Glowy\Filesystem\filesystem;
use function parse_str;
// Shortcode: fetch
// Usage: (fetch resource:'blog/post-1' options:'' field:'title')
// (fetch:'blog/post-1')
parsers()->shortcodes()->addHandler('fetch', static function (ShortcodeInterface $s) {
$params = $s->getParameters();
$fetchHelper = function ($params) use ($s) {
// Get the resource parameter.
$resource = parsers()->shortcodes()->parse($params['resource']);
// Set options
if (isset($params['options'])) {
parse_str(parsers()->shortcodes()->parse($params['options']), $options);
} else {
$options = [];
}
// Prepare options
$options = collection($options)->dot()->map(static function ($value) {
if (strings($value)->isInteger()) {
$value = strings($value)->toInteger();
} elseif (strings($value)->isFloat()) {
$value = strings($value)->toFloat();
} elseif (strings($value)->isBoolean()) {
$value = strings($value)->toBoolean();
} elseif (strings($value)->isNull()) {
$value = strings($value)->toNull();
} else {
$value = (string) $value;
}
return $value;
})->undot()->toArray();
// Backup current entry data
$original = entries()->registry()['methods.fetch'];
// Do fetch the data from the resource.
$result = fetch($resource, $options);
// Restore original entry data
entries()->registry()->set('methods.fetch', $original);
// Return empty result if result is instance of Response (for e.g. if async = true).
if ($result instanceof Response) {
return '';
}
if ($result->count() > 0) {
$result = $result->toJson();
// Get specific field value
if ($s->getParameter('field') !== null) {
$result = collectionFromJson($result)->get(parsers()->shortcodes()->parse($s->getParameter('field')), $s->getParameter('default') !== null ? parsers()->shortcodes()->parse($s->getParameter('default')) : '');
}
return $result;
}
return '';
};
if (isset($params['resource'])) {
return $fetchHelper($params);
}
if ($s->getBBCode() !== null) {
$params = ['resource' => $s->getBBCode(), 'options' => isset($params['options']) ? $params['options'] : ''];
return $fetchHelper($params);
}
return '';
});

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
use function Glowy\Filesystem\filesystem;
use function Flextype\fetch;
use function Flextype\entries;
use function Flextype\registry;
beforeEach(function() {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->ensureExists(0755, true);
});
afterEach(function () {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->delete();
});
test('fetch shortcode', function () {
// 1.
$this->assertTrue(entries()->create('data', ['title' => 'Foo', 'category' => 'foo']));
$this->assertTrue(entries()->create('foo', ['test1' => "(fetch:data field:'title')",
'test2' => '(fetch:data)',
'test3' => "(fetch:data field:'foo' default:'FooDefault')"]));
$foo = entries()->fetch('foo');
expect($foo['test1'])->toBe('Foo');
expect($foo['test2'])->toBeJson();
expect($foo['test3'])->toBe('FooDefault');
// 2.
$this->assertTrue(entries()->create('bar', ['test1' => "(fetch:'(const:FLEXTYPE_ROOT_DIR)/src/flextype/flextype.yaml' field:'name')",
'test2' => "(fetch:'(const:FLEXTYPE_ROOT_DIR)/src/flextype/flextype.yaml')",
'test3' => "(fetch:'(const:FLEXTYPE_ROOT_DIR)/src/flextype/flextype.yaml' field:boo default:Boo)"]));
$bar = entries()->fetch('bar');
expect($bar['test1'])->toBe('Flextype');
expect($bar['test2'])->toBeJson();
expect($bar['test3'])->toBe('Boo');
// 3. @TODO test for fetch data from the urls...
});
test('entries shortcode disabled', function () {
registry()->set('flextype.settings.parsers.shortcodes.shortcodes.fetch.enabled', false);
expect(entries()->create('foo', ['test' => ""]))->toBeTrue();
expect(entries()->fetch('foo')['test'])->toBe('');
registry()->set('flextype.settings.parsers.shortcodes.shortcodes.fetch.enabled', true);
});