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
|
|
|
|