Skip to content

Commit

Permalink
支持symfony/var-dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jan 18, 2022
1 parent a61c26c commit 70a1f7f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -73,3 +73,23 @@ return [
];

```

## 支持`symfony/var-dumper`

由于应用是通过php cli启动的,所以默认`symfony/var-dumper`会将调试信息打印在控制台, 通过配置中间件来支持将调试信息输出在网页上 如下是直接在配置在全局中间件上,也可以在路由定义的时候配置

```php
// app/middleware.php

<?php
// 全局中间件定义文件
return [
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
//\think\middleware\SessionInit::class,
\think\swoole\middleware\InteractsWithVarDumper::class,
];
```
37 changes: 37 additions & 0 deletions src/middleware/InteractsWithVarDumper.php
@@ -0,0 +1,37 @@
<?php

namespace think\swoole\middleware;

use Closure;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\VarDumper;
use think\Request;
use think\Response;

class InteractsWithVarDumper
{
/**
* @param Request $request
* @param Closure $next
* @return Response
*/
public function handle($request, Closure $next)
{
if (class_exists(VarDumper::class)) {
$cloner = new VarCloner();
$dumper = new HtmlDumper();

$prevHandler = VarDumper::setHandler(function ($var) use ($dumper, $cloner) {
$dumper->dump($cloner->cloneVar($var));
});

/** @var Response $response */
$response = $next($request);

VarDumper::setHandler($prevHandler);
return $response;
}
return $next($request);
}
}

0 comments on commit 70a1f7f

Please sign in to comment.