超好用的 phpstorm debug工具,yasd

前言

平常在开发中,总会有一些需要 debug 的时候,手动打断点 var_dumpdie 等方式总是耗时耗力还不优雅,这时候可能会有人选择安装 xdebug 扩展,在 phpstorm 中配置 xdebug 来调试。

但当你需要用到 swoole 框架时,并需要调试 swoole 框架中代码时,xdebug 是不能用在 swoole 上的,是和 swoole 有冲突的。

然后我发现了一款比较好用的,可以兼容 fpm 框架,和 swoole 框架的 debug 调试工具,yasd.

安装

1. Mac上安装 boots 库

brew install boost

2. 从 Github 上下载 yasd 源码

git clone https://github.com/swoole/yasd.git

3. 编译安装 yasd

cd yasd
phpize --clean && \
phpize && \
./configure && \
make clean && \
make && \
make install

4. 修改 php.ini

zend_extension="yasd.so"

;命令行调试
;yasd.debug_mode=cmd
;远程调试
yasd.debug_mode=remote
;本地开发地址
yasd.remote_host=127.0.0.1
;本地开发监听端口
yasd.remote_port=9000

查看扩展信息

➜  yasd php --ri yasd

yasd

Yasd => enabled
Author => codinghuang <codinghuang@qq.com>
Version => 0.3.9-alpha
Built => Jan 15 2022 14:09:47

Directive => Local Value => Master Value
yasd.breakpoints_file => no value => no value
yasd.debug_mode => remote => remote
yasd.remote_host => 127.0.0.1 => 127.0.0.1
yasd.remote_port => 9000 => 9000
yasd.depth => 1 => 1
yasd.log_level => -1 => -1
yasd.max_executed_opline_num => 0 => 0
yasd.init_file => no value => no value
yasd.open_extended_info => 0 => 0
xdebug.coverage_enable => 1 => 1
xdebug.profiler_enable => 1 => 1
xdebug.remote_autostart => 1 => 1
xdebug.remote_connect_back => 0 => 0
xdebug.remote_mode => req => req
xdebug.idekey => hantaohuang => hantaohuang

5. 调试脚本

使用 yasd 调试脚本,必须要添加 -ephp 参数,例如这样

# 调试普通php脚本
php -e test.php
# 调试 laravel 的 command
php -e artisan test
# 调试 hyperf 框架
php -e bin/hyperf start

配置 phpstorm

1. 配置 phpstorm 监听 9000 端口

超好用的 phpstorm debug工具,yasd

2. 点击 phpstorm 右上角的电话图标启动监听

超好用的 phpstorm debug工具,yasd

3. 测试 debug 调试

在 Laravel 中随便写一个 test command

php -e artisan test

超好用的 phpstorm debug工具,yasd
到这里就已经成功了

调试 Hyperf 框架

上面说了在 fpm 中调试,接下来说一下在 Hyperf 框架中如何调试

需要注意的几点

  1. 在 Hyperf 中使用需要将 config.php 中的 scan_cacheable 参数设置为 true,该参数为是否扫描代理类,若设置为 true,则每次启动都会直接扫描缓存代理类,而不是重新扫描在生成代理类。
    设置为 true 后需要注意,每次修改代码需要手动生成代理类,composer dump-autoload -o,然后在启动。
    或者直接在代理类中修改代码,当 debug 结束后,再将代理类中的代码复制到真实类中。
    代理类生成路径在 runtime/container/proxy
  2. 在 Hyperf 框架中如果使用到了 Swoole Server,需要将 worker_num 设置为 1 否则断点可能不会生效。该参数在 server.php 中。

剩下的调试就和上面讲的没什么区别了。

参考文章:

官方文档
yasd Github
yasd调试swoole框架代码
在IDE中使用yasd调试器-视频教程
Mac M1 swoole yasd安装调试过程及问题记录

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 2年前 自动加精
讨论数量: 6
janus

来瞅瞅

2年前 评论
goStruct

yasd是不是不支持php8.1

2年前 评论
邢闯洋 (楼主) 2年前
windawake

把scan_cacheable=true和worker_num=1可以调试composer test的单元测试代码

2年前 评论
第二天堂 2年前
windawake (作者) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!