Passed
Push — master ( b5e06a...fd43a7 )
by Dmitriy
13:41 queued 11:12
created

DevPanelMiddleware::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 12
cp 0
rs 9.9332
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\View\WebView;
13
use Yiisoft\Yii\Debug\Viewer\Asset\DevPanelAsset;
14
15
final class DevPanelMiddleware implements MiddlewareInterface
16
{
17
    public function __construct(
18
        private string $containerId,
19
        private string $viewerUrl,
20
        private string $backendUrl,
21
        private string $editorUrl,
22
        private string $staticUrl,
23
        private AssetManager $assetManager,
24
        private WebView $view,
25
    ) {
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33
        $this->assetManager->registerCustomized(DevPanelAsset::class, ['baseUrl' => $this->staticUrl]);
34
        $this->view->registerJs(
35
            <<<JS
36
            const containerId = '{$this->containerId}';
37
            const container = document.createElement('div');
38
            container.setAttribute('id', containerId);
39
            container.style.flex = "1";
40
            document.body.append(container);
41
42
            window['YiiDevPanelWidget'] = window['YiiDevPanelWidget'] ?? {};
43
            window['YiiDevPanelWidget'].config = {
44
                containerId: containerId,
45
                options: {
46
                    application: {
47
                        editorUrl: '{$this->editorUrl}',
48
                    },
49
                    router: {
50
                        basename: '{$this->viewerUrl}',
51
                        useHashRouter: false,
52
                    },
53
                    backend: {
54
                        baseUrl: '{$this->backendUrl}',
55
                    }
56
                },
57
            };
58
            JS,
59
            WebView::POSITION_LOAD,
60
        );
61
62
        return $handler->handle($request);
63
    }
64
}
65