From 299bc5b551acb61c65c4e49c688c0fb56310c649 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 15 Apr 2021 00:59:30 +0000 Subject: [PATCH] [TWIG] Add way to launch events from TWIG, capture service and add way to render from a string --- src/Core/GNUsocial.php | 7 ++++++- src/Routes/Main.php | 2 -- src/Twig/Extension.php | 1 + src/Twig/Runtime.php | 26 +++++++++++--------------- src/Util/Formatting.php | 11 +++++++++++ 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Core/GNUsocial.php b/src/Core/GNUsocial.php index d62ace512a..a6879acdbf 100644 --- a/src/Core/GNUsocial.php +++ b/src/Core/GNUsocial.php @@ -47,6 +47,7 @@ use App\Core\I18n\I18n; use App\Core\Queue\Queue; use App\Core\Router\Router; use App\Util\Common; +use App\Util\Formatting; use Doctrine\ORM\EntityManagerInterface; use HtmlSanitizer\SanitizerInterface; use Psr\Log\LoggerInterface; @@ -85,6 +86,7 @@ class GNUsocial implements EventSubscriberInterface protected HttpClientInterface $client; protected SanitizerInterface $sanitizer; protected ContainerBagInterface $config; + protected \Twig\Environment $twig; /** * Symfony dependency injection gives us access to these services @@ -102,7 +104,8 @@ class GNUsocial implements EventSubscriberInterface ModuleManager $mm, HttpClientInterface $cl, SanitizerInterface $san, - ContainerBagInterface $conf) + ContainerBagInterface $conf, + \Twig\Environment $twig) { $this->logger = $logger; $this->translator = $trans; @@ -118,6 +121,7 @@ class GNUsocial implements EventSubscriberInterface $this->client = $cl; $this->saniter = $san; $this->config = $conf; + $this->twig = $twig; $this->initialize(); } @@ -140,6 +144,7 @@ class GNUsocial implements EventSubscriberInterface Security::setHelper($this->security, $this->saniter); Router::setRouter($this->router, $this->url_generator); HTTPClient::setClient($this->client); + Formatting::setTwig($this->twig); Cache::setupCache(); // Events are proloaded on compilation, but set at runtime diff --git a/src/Routes/Main.php b/src/Routes/Main.php index 6f37a35759..04c370502a 100644 --- a/src/Routes/Main.php +++ b/src/Routes/Main.php @@ -51,8 +51,6 @@ abstract class Main $r->connect('main_all', '/main/all', [C\Network::class, 'network']); $r->connect('home_all', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/all', [C\Network::class, 'home']); $r->connect('replies', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/replies', [C\Network::class, 'replies']); - $r->connect('favourites', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', [C\Network::class, 'favourites']); - $r->connect('reversefavs', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/reversefavs', [C\Network::class, 'reversefavs']); $r->connect('panel', '/panel', [C\AdminPanel::class, 'site']); $r->connect('panel_site', '/panel/site', [C\AdminPanel::class, 'site']); diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php index cdde0cc119..811a67f057 100644 --- a/src/Twig/Extension.php +++ b/src/Twig/Extension.php @@ -56,6 +56,7 @@ class Extension extends AbstractExtension new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']), new TwigFunction('get_note_other_content', [Runtime::class, 'getNoteOtherContent']), new TwigFunction('get_show_styles', [Runtime::class, 'getShowStyles']), + new TwigFunction('handle_event', [Runtime::class, 'handleEvent']), new TwigFunction('config', [Runtime::class, 'getConfig']), new TwigFunction('icon', [Runtime::class, 'embedSvgIcon'], ['needs_environment' => true]), ]; diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php index 3f72fff617..83235ac856 100644 --- a/src/Twig/Runtime.php +++ b/src/Twig/Runtime.php @@ -1,6 +1,7 @@ . + // }}} /** @@ -40,9 +42,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; use Twig\Environment; -use Twig\Error\LoaderError; -use Twig\Error\RuntimeError; -use Twig\Error\SyntaxError; use Twig\Extension\RuntimeExtensionInterface; class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface @@ -103,6 +102,14 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface return $styles; } + public function handleEvent(string $event, ...$args) + { + $res = ''; + $args[] = &$res; + Event::handle($event, $args); + return $res; + } + /** * Renders the Svg Icon template and returns it. * @@ -116,18 +123,7 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface */ public function embedSvgIcon(Environment $twig, string $icon_name = '', string $icon_css_class = '') { - try { - return $twig->render('@public_path/assets/icons/' . $icon_name . '.svg.twig', ['iconClass' => $icon_css_class]); - } catch (LoaderError $e) { - //return an empty string (a missing icon is not that important of an error) - return ''; - } catch (RuntimeError $e) { - //return an empty string (a missing icon is not that important of an error) - return ''; - } catch (SyntaxError $e) { - //return an empty string (a missing icon is not that important of an error) - return ''; - } + return $twig->render('@public_path/assets/icons/' . $icon_name . '.svg.twig', ['iconClass' => $icon_css_class]); } // ---------------------------------------------------------- diff --git a/src/Util/Formatting.php b/src/Util/Formatting.php index d3e4a49735..b001009733 100644 --- a/src/Util/Formatting.php +++ b/src/Util/Formatting.php @@ -37,6 +37,17 @@ use InvalidArgumentException; abstract class Formatting { + private static ?\Twig\Environment $twig; + public static function setTwig(\Twig\Environment $twig) + { + self::$twig = $twig; + } + + public static function twigRender(string $template, array $context): string + { + return self::$twig->createTemplate($template, null)->render($context); + } + /** * Normalize path by converting \ to / *