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