RoutesPanel::processData()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of Biurad opensource projects.
5
 *
6
 * @copyright 2019 Biurad Group (https://biurad.com/)
7
 * @license   https://opensource.org/licenses/BSD-3-Clause License
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Flange\Debug\Tracy;
14
15
use Biurad\Http\Uri;
16
use Flight\Routing\Router;
17
use Nette;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Tracy;
21
22
/**
23
 * Routing debugger for Debug Bar.
24
 */
25
final class RoutesPanel implements Tracy\IBarPanel
26
{
27
    use Nette\SmartObject;
28
29
    private int $routeCount = 0;
30
    private ?Request $request;
31
32
    /** @var array<int,mixed> */
33
    private array $routes = [];
34
35
    /** @var \ReflectionClass|\ReflectionFunction|\ReflectionMethod|string */
36
    private ?\Reflector $source = null;
0 ignored issues
show
introduced by
The private property $source is not used, and could be removed.
Loading history...
37
38
    public function __construct(private Router $profiler, RequestStack $request)
39
    {
40
        $this->request = $request->getMainRequest();
41
    }
42
43
    /**
44
     * Renders tab.
45
     */
46
    public function getTab(): string
47
    {
48
        foreach ($this->profiler->getCollection()->getRoutes() as $route) {
49
            $this->processData($route);
50
        }
51
52
        return Nette\Utils\Helpers::capture(function (): void {
53
            require __DIR__.'/templates/RoutingPanel.tab.phtml';
54
        });
55
    }
56
57
    /**
58
     * Renders panel.
59
     */
60
    public function getPanel(): string
61
    {
62
        return Nette\Utils\Helpers::capture(function (): void {
63
            require __DIR__.'/templates/RoutingPanel.panel.phtml';
64
        });
65
    }
66
67
    private function processData($profile): void
68
    {
69
        ++$this->routeCount;
70
        $data = ['matched' => false, 'route' => $profile, 'name' => \is_array($profile) ? $profile['name'] ?? 'unnamed' : $profile->getName() ?? 'unnamed'];
71
72
        if (null !== ($r = $this->request) && $profile === $this->profiler->match($r->getMethod(), new Uri($r->getUri()))) {
73
            $data['matched'] = true;
74
        }
75
76
        $this->routes[] = $data;
77
    }
78
}
79