[TWIG] Add way to launch events from TWIG, capture service and add way to render from a string
This commit is contained in:
parent
1b7c48c732
commit
299bc5b551
@ -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
|
||||
|
@ -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']);
|
||||
|
@ -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]),
|
||||
];
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
// {{{ License
|
||||
|
||||
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||
//
|
||||
// GNU social is free software: you can redistribute it and/or modify
|
||||
@ -15,6 +16,7 @@
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// }}}
|
||||
|
||||
/**
|
||||
@ -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 '';
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
|
@ -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 /
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user