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

feat(url): update url shortcodes and helpers

This commit is contained in:
Awilum
2022-04-15 16:57:03 +03:00
parent 0e4e8bb8be
commit 0cb4068139
2 changed files with 41 additions and 6 deletions

View File

@@ -16,19 +16,54 @@ declare(strict_types=1);
namespace Flextype\Parsers\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use function app;
use function parsers;
use function registry;
// Shortcode: [url]
parsers()->shortcodes()->addHandler('url', static function () {
// Shortcode: [getBaseUrl]
parsers()->shortcodes()->addHandler('getBaseUrl', static function () {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) {
return '';
}
if (registry()->has('flextype.settings.url') && registry()->get('flextype.settings.url') !== '') {
return registry()->get('flextype.settings.url');
return getBaseUrl();
});
// Shortcode: [getBasePath]
parsers()->shortcodes()->addHandler('getBasePath', static function () {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) {
return '';
}
return app()->getBasePath();
return getBasePath();
});
// Shortcode: [getAbsoluteUrl]
parsers()->shortcodes()->addHandler('getAbsoluteUrl', static function () {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) {
return '';
}
return getAbsoluteUrl();
});
// Shortcode: [getUriString]
parsers()->shortcodes()->addHandler('getUriString', static function () {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) {
return '';
}
return getUriString();
});
// Shortcode: [urlFor routeName="route-name" data='{"foo": "Foo"}' queryParams='{"foo": "Foo"}']
parsers()->shortcodes()->addHandler('urlFor', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.url.enabled')) {
return '';
}
return urlFor($s->getParameter('routeName'),
$s->getParameter('data') != null ? serializers()->json()->decode($s->getParameter('data')) : [],
$s->getParameter('queryParams') != null ? serializers()->json()->decode($s->getParameter('queryParams')) : [],);
});

View File

@@ -186,7 +186,7 @@ if (! function_exists('getAbsoluteUrl')) {
function getAbsoluteUrl(): string
{
$url = getBaseUrl();
$url .= $_SERVER['REQUEST_URI'];
$url .= $_SERVER['REQUEST_URI'] ?? '';
return $url;
}