ToolbarMiddleware::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 12
c 10
b 0
f 0
dl 0
loc 39
ccs 0
cts 14
cp 0
rs 9.8666
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Viewer\Middleware;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Assets\AssetManager;
12
use Yiisoft\Router\UrlGeneratorInterface;
13
use Yiisoft\View\WebView;
14
use Yiisoft\Yii\Debug\Viewer\Asset\ToolbarAsset;
15
16
final class ToolbarMiddleware implements MiddlewareInterface
17
{
18
    public function __construct(
19
        private string $containerId,
20
        private string $viewerUrl,
21
        private string $backendUrl,
22
        private string $editorUrl,
23
        private string $staticUrl,
24
        private AssetManager $assetManager,
25
        private WebView $view,
26
        private UrlGeneratorInterface $urlGenerator,
27
    ) {
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
34
    {
35
        $baseUriPrefix = $this->urlGenerator->getUriPrefix();
36
        $this->assetManager->registerCustomized(ToolbarAsset::class, ['baseUrl' => $this->staticUrl]);
37
        $this->view->registerJs(
38
            <<<JS
39
            (function(){
40
            const containerId = '{$this->containerId}';
41
            const container = document.createElement('div');
42
            container.setAttribute('id', containerId);
43
            container.style.flex = "0";
44
            document.body.append(container);
45
46
            window['YiiDevPanelToolbarWidget'] = window['YiiDevPanelToolbarWidget'] ?? {};
47
            window['YiiDevPanelToolbarWidget'].config = {
48
                containerId: containerId,
49
                options: {
50
                    application: {
51
                        editorUrl: '{$this->editorUrl}',
52
                    },
53
                    modules: {
54
                         toolbar: '1',
55
                     },
56
                    router: {
57
                        basename: '{$baseUriPrefix}',
58
                        useHashRouter: false,
59
                    },
60
                    backend: {
61
                        baseUrl: '{$this->backendUrl}{$baseUriPrefix}',
62
                        usePreferredUrl: true,
63
                    }
64
                },
65
            };
66
            })();
67
            JS,
68
            WebView::POSITION_LOAD,
69
        );
70
71
        return $handler->handle($request);
72
    }
73
}
74