. // }}} /** * Common utility functions * * @package GNUsocial * @category Util * * @author Hugo Sales * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ namespace App\Twig; use App\Util\Formatting; use Functional as F; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; use Twig\Extension\RuntimeExtensionInterface; class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface { private Request $request; public function __constructor() { } public function isCurrentRouteActive(string ...$routes): string { return $this->isCurrentRoute('active', ...$routes); } public function isCurrentRoute(string $class, string ...$routes): string { $current_route = $this->request->attributes->get('_route'); return F\some($routes, F\partial_left([Formatting::class, 'startsWith'], $current_route)) ? $class : ''; } // ---------------------------------------------------------- // Request is not a service, can't find a better way to get it public function onKernelRequest(RequestEvent $event) { $this->request = $event->getRequest(); } public static function getSubscribedEvents() { return [KernelEvents::REQUEST => 'onKernelRequest']; } }