Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
9 / 9
Runtime
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
11
100.00% covered (success)
100.00%
9 / 9
 setRequest
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 isCurrentRouteActive
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 isCurrentRoute
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
2 / 2
 getNoteActions
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getConfig
n/a
0 / 0
1
n/a
0 / 0
 getShowStylesheets
n/a
0 / 0
1
n/a
0 / 0
 handleEvent
n/a
0 / 0
1
n/a
0 / 0
 embedSvgIcon
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 onKernelRequest
n/a
0 / 0
1
n/a
0 / 0
 getSubscribedEvents
n/a
0 / 0
1
n/a
0 / 0
1<?php
2
3// {{{ License
4// This file is part of GNU social - https://www.gnu.org/software/social
5//
6// GNU social is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Affero General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU social is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU Affero General Public License for more details.
15//
16// You should have received a copy of the GNU Affero General Public License
17// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
18// }}}
19
20/**
21 * Common utility functions
22 *
23 * @package   GNUsocial
24 * @category  Util
25 *
26 * @author    Hugo Sales <hugo@hsal.es>
27 * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
28 * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
29 */
30
31namespace App\Twig;
32
33use App\Core\Event;
34use App\Entity\Note;
35use App\Util\Common;
36use App\Util\Formatting;
37use Functional as F;
38use Symfony\Component\EventDispatcher\EventSubscriberInterface;
39use Symfony\Component\HttpFoundation\Request;
40use Symfony\Component\HttpKernel\Event\RequestEvent;
41use Symfony\Component\HttpKernel\KernelEvents;
42use Twig\Environment;
43use Twig\Extension\RuntimeExtensionInterface;
44
45class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
46{
47    private Request $request;
48    public function setRequest(Request $req)
49    {
50        $this->request = $req;
51    }
52
53    public function isCurrentRouteActive(string ...$routes): string
54    {
55        return $this->isCurrentRoute('active', ...$routes);
56    }
57
58    public function isCurrentRoute(string $class, string ...$routes): string
59    {
60        $current_route = $this->request->attributes->get('_route');
61        return F\some($routes, F\partial_left([Formatting::class, 'startsWith'], $current_route)) ? $class : '';
62    }
63
64    public function getNoteActions(Note $note)
65    {
66        $actions = [];
67        Event::handle('AddNoteActions', [$this->request, $note, &$actions]);
68        return $actions;
69    }
70
71    /**
72     * @codeCoverageIgnore
73     */
74    public function getConfig(...$args)
75    {
76        return Common::config(...$args);
77    }
78
79    /**
80     * get stylesheets
81     *
82     * @return array|mixed
83     * @codeCoverageIgnore
84     */
85    public function getShowStylesheets()
86    {
87        $styles = [];
88        Event::handle('ShowStyles', [&$styles]);
89        return implode("\n", $styles);
90    }
91
92    /**
93     * @codeCoverageIgnore
94     */
95    public function handleEvent(string $event, ...$args)
96    {
97        $res    = [];
98        $args[] = &$res;
99        Event::handle($event, $args);
100        return $res;
101    }
102
103    /**
104     * Renders the Svg Icon template and returns it.
105     *
106     * @param Environment $twig
107     * @param string      $icon_name
108     * @param string      $icon_css_class
109     *
110     * @return string
111     *
112     * @author Ângelo D. Moura <up201303828@fe.up.pt>
113     */
114    public function embedSvgIcon(Environment $twig, string $icon_name = '', string $icon_css_class = '')
115    {
116        return $twig->render('@public_path/assets/icons/' . $icon_name . '.svg.twig', ['iconClass' => $icon_css_class]);
117    }
118
119    // ----------------------------------------------------------
120
121    /**
122     * @codeCoverageIgnore
123     */
124    public function onKernelRequest(RequestEvent $event)
125    {
126        // Request is not a service, can't find a better way to get it
127        $this->request = $event->getRequest();
128    }
129
130    /**
131     * @codeCoverageIgnore
132     */
133    public static function getSubscribedEvents()
134    {
135        return [KernelEvents::REQUEST => 'onKernelRequest'];
136    }
137}