Skip to content

Commit

Permalink
Complete panel integration
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Apr 7, 2024
1 parent 306f3b5 commit 9750d16
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
8 changes: 6 additions & 2 deletions config/di-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

declare(strict_types=1);

use Yiisoft\Yii\Debug\Viewer\Middleware\DevPanelMiddleware;
use Yiisoft\Yii\Debug\Viewer\Middleware\ToolbarMiddleware;

/** @var array $params */

return [
ToolbarMiddleware::class => [
'__construct()' => [
'apiUrl' => $params['yiisoft/yii-debug-viewer']['apiUrl'],
'editorUrl' => $params['yiisoft/yii-debug-viewer']['editorUrl'],
'containerId' => $params['yiisoft/yii-debug-viewer']['containerId'],
],
],
DevPanelMiddleware::class => [
'__construct()' => [
'viewerUrl' => $params['yiisoft/yii-debug-viewer']['viewerUrl'],
],
],
];
2 changes: 2 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\Yii\Debug\Viewer\IndexController;
use Yiisoft\Yii\Debug\Viewer\Middleware\DevPanelMiddleware;
use Yiisoft\Yii\Debug\Viewer\Middleware\ToolbarMiddleware;
use Yiisoft\Yii\Middleware\CorsAllowAll;

Expand All @@ -21,6 +22,7 @@
->routes(
Route::get('{path:.*}')
->middleware(FormatDataResponseAsHtml::class)
->middleware(DevPanelMiddleware::class)
->action([IndexController::class, 'index'])
->name('debug/index'),
),
Expand Down
3 changes: 0 additions & 3 deletions resources/views/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* @var AssetManager $assetManager
*/

$assetManager->register(DevPanelAsset::class);

$this->addCssFiles($assetManager->getCssFiles());
$this->addCssStrings($assetManager->getCssStrings());
$this->addJsFiles($assetManager->getJsFiles());
Expand Down Expand Up @@ -48,7 +46,6 @@
<?php
$this->beginBody() ?>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root" style="flex:1"></div>
<?php
$this->endBody() ?>
</body>
Expand Down
3 changes: 3 additions & 0 deletions src/Asset/DevPanelAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ final class DevPanelAsset extends AssetBundle
public array $js = [
[
'https://yiisoft.github.io/yii-dev-panel/bundle.js',
//'http://localhost:4173/bundle.js',
WebView::POSITION_END,
'type' => 'module',
],
'https://yiisoft.github.io/yii-dev-panel/registerSW.js',
//'http://localhost:4173/registerSW.js',
];

public array $css = [
'https://yiisoft.github.io/yii-dev-panel/bundle.css',
//'http://localhost:4173/bundle.css',
];
}
55 changes: 55 additions & 0 deletions src/Middleware/DevPanelMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Viewer\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Assets\AssetManager;
use Yiisoft\View\WebView;
use Yiisoft\Yii\Debug\Viewer\Asset\DevPanelAsset;
use Yiisoft\Yii\Debug\Viewer\Asset\ToolbarAsset;

final class DevPanelMiddleware implements MiddlewareInterface
{
public function __construct(
private string $viewerUrl,
private AssetManager $assetManager,
private WebView $view,
) {
}

/**
* @inheritDoc
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->assetManager->register(DevPanelAsset::class);
$this->view->registerJs(
<<<JS
const devPanelContainerId = 'yii-dev-panel';
const devPanel = document.createElement('div');
devPanel.setAttribute('id', devPanelContainerId);
devPanel.style.flex= "1";
document.body.append(devPanel);
console.log('window.YiiDevPanelWidget', window.YiiDevPanelWidget)
window['YiiDevPanelWidget'] = window['YiiDevPanelWidget'] ?? {};
window['YiiDevPanelWidget'].config = {
containerId: devPanelContainerId,
options: {
router: {
basename: '{$this->viewerUrl}',
}
},
};
JS,
WebView::POSITION_LOAD,
);

return $handler->handle($request);
}
}

0 comments on commit 9750d16

Please # to comment.