Issues (40)

php-src/Diagnostics/ResourceRouterPanel.php (5 issues)

Severity
1
<?php
2
3
namespace kalanis\Restful\Diagnostics;
4
5
6
use kalanis\Restful\Application\IResourceRouter;
7
use Nette;
8
use Nette\Routing\Router;
9
use Nette\Utils\FileSystem;
10
use Nette\Utils\Html;
11
use Tracy\IBarPanel;
12
use Traversable;
13
14
15
/**
16
 * ResourceRouterPanel to see REST API resource routes
17
 * @package kalanis\Restful\Diagnostics
18
 */
19
class ResourceRouterPanel implements IBarPanel
20
{
21
22
    public function __construct(
23
        #[\SensitiveParameter] private readonly string $secretKey,
24
        private readonly string                        $requestTimeKey,
25
        private readonly Router                        $router,
26
    )
27
    {
28
    }
29
30
    /**
31
     * Renders HTML code for custom tab.
32
     * @return string
33
     */
34
    public function getTab(): string
35
    {
36
        $icon = Html::el('img')
37
            ->src('data:image/png;base64,' . base64_encode(FileSystem::read(__DIR__ . '/icon.png')))
38
            ->height(16);
39
        return '<span class="REST API resource routes">' . $icon . 'API resources</span>';
40
    }
41
42
    /**
43
     * Renders HTML code for custom panel.
44
     * @return string
45
     */
46
    public function getPanel(): string
47
    {
48
        ob_start();
49
        $esc = ['Tracy\Helpers', 'escapeHtml'];
0 ignored issues
show
The assignment to $esc is dead and can be removed.
Loading history...
50
        $router = ($this->router instanceof Nette\Routing\RouteList)
51
            ? $this->router
52
            : (new Nette\Routing\RouteList())->add($this->router);
53
        $routes = $this->getResourceRoutes($router);
0 ignored issues
show
The assignment to $routes is dead and can be removed.
Loading history...
54
        $methods = [
0 ignored issues
show
The assignment to $methods is dead and can be removed.
Loading history...
55
            IResourceRouter::GET => 'GET',
56
            IResourceRouter::POST => 'POST',
57
            IResourceRouter::PUT => 'PUT',
58
            IResourceRouter::DELETE => 'DELETE',
59
            IResourceRouter::HEAD => 'HEAD',
60
            IResourceRouter::PATCH => 'PATCH',
61
            IResourceRouter::OPTIONS => 'OPTIONS',
62
        ];
63
        $privateKey = $this->secretKey;
0 ignored issues
show
The assignment to $privateKey is dead and can be removed.
Loading history...
64
        $requestTimeKey = $this->requestTimeKey;
0 ignored issues
show
The assignment to $requestTimeKey is dead and can be removed.
Loading history...
65
66
        require_once __DIR__ . DIRECTORY_SEPARATOR . 'panel.phtml';
67
        return strval(ob_get_clean());
68
    }
69
70
    /**
71
     * @param Nette\Routing\RouteList|iterable<object> $routeList
72
     * @return array<IResourceRouter>
73
     */
74
    private function getResourceRoutes(iterable|Nette\Routing\RouteList $routeList): array
75
    {
76
        static $resourceRoutes = [];
77
        $iter = is_object($routeList) && is_a($routeList, Nette\Routing\RouteList::class)
78
            ? $routeList->getRouters()
79
            : $routeList
80
        ;
81
        foreach ($iter as $route) {
82
            if ($route instanceof Traversable) {
83
                $this->getResourceRoutes($route);
84
            }
85
            if ($route instanceof IResourceRouter) {
86
                $resourceRoutes[] = $route;
87
            }
88
        }
89
        return $resourceRoutes;
90
    }
91
}
92