在 ThinkPHP 5.1 中封装 PsySH 交互式命令行(交互式调试器)工具
什么是 PsySH ?
PsySH
是一个PHP
的运行时开发平台,交互式调试器和Read-Eval-Print Loop (REPL)
。说的简单点,就像你用
Chrome 开发者工具
的console
调试你的JavaScript
代码一样。
安装 PsySH
官网给出了两种方法
- 直接下载(包括git clone 和 普通方式下载)
- Composer安装
我们现在使用
Composer
来进行安装,TP 5.1
所有的扩展功能都是用Composer
来管理, 所以我们直接使用Composer
来安装更加方便快捷.我们在
windows 10
系统在进行安装, 确保你的电脑上已经安装上:PHP 7.0+
Composer
.
进入框架根目录 , 输入 ls
命令, 你将看到下方文件 (注意: 请使用 PowerShell 来运行而不是 cmd)
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2019/1/24 15:21 .idea
d----- 2019/1/24 15:14 application
d----- 2019/1/22 16:33 config
d----- 2019/1/22 16:33 extend
d----- 2019/1/22 16:33 public
d----- 2019/1/22 16:33 route
d----- 2019/1/24 14:18 runtime
d----- 2019/1/22 16:33 thinkphp
d----- 2019/1/24 14:22 vendor
-a---- 2019/1/22 16:33 54 .gitignore
-a---- 2019/1/22 16:33 2038 .travis.yml
-a---- 2019/1/22 16:33 1070 build.php
-a---- 2019/1/22 16:33 32387 CHANGELOG.md
-a---- 2019/1/24 15:14 725 composer.json
-a---- 2019/1/24 15:14 27908 composer.lock
-a---- 2019/1/22 16:33 1822 LICENSE.txt
-a---- 2019/1/22 16:33 6613 README.md
-a---- 2019/1/22 16:33 823 think
确定进入目录后, 使用 Composer
命令来安装 PsySH
composer require psy/psysh
运行命令后, 出现下方提示则表示安装成功, 对比时注意文章发布时间, 如年代久远则有些许差异.
PS E:\project\ucenter> composer require psy/psysh
Using version ^0.9.9 for psy/psysh
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 10 installs, 0 updates, 0 removals
- Installing symfony/polyfill-php72 (v1.10.0): Loading from cache
- Installing symfony/polyfill-mbstring (v1.10.0): Loading from cache
- Installing symfony/var-dumper (v4.2.2): Loading from cache
- Installing symfony/contracts (v1.0.2): Loading from cache
- Installing symfony/console (v4.2.2): Loading from cache
- Installing nikic/php-parser (v4.2.0): Loading from cache
- Installing jakub-onderka/php-console-color (v0.2): Loading from cache
- Installing jakub-onderka/php-console-highlighter (v0.4): Loading from cache
- Installing dnoegel/php-xdg-base-dir (0.1): Loading from cache
- Installing psy/psysh (v0.9.9): Loading from cache
symfony/contracts suggests installing psr/cache (When using the Cache contracts)
symfony/contracts suggests installing psr/container (When using the Service contracts)
symfony/contracts suggests installing symfony/cache-contracts-implementation ()
symfony/contracts suggests installing symfony/service-contracts-implementation ()
symfony/contracts suggests installing symfony/translation-contracts-implementation ()
symfony/console suggests installing psr/log-implementation (For using the console logger)
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/lock ()
symfony/console suggests installing symfony/process ()
psy/psysh suggests installing ext-pcntl (Enabling the PCNTL extension makes PsySH a lot happier :))
psy/psysh suggests installing ext-posix (If you have PCNTL, you'll want the POSIX extension as well.)
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
psy/psysh suggests installing hoa/console (A pure PHP readline implementation. You'll want this if your PHP install doesn't already support readline or libedit.)
Writing lock file
Generating autoload files
在次检查 PsySH
是否安装成功
- 打开
vendor
文件夹- 寻找是否存在
psy\psysh
文件夹- 存在则安装成功.
封装到 TP 5.1
引言: 我们将使用TP 5.1
中的命令行/自定义指令
功能来封装至框架中. 官方文档:https://www.kancloud.cn/manual/thinkphp5_1/354146
创建一个自定义命令类文件,新建 application/common/command/Psysh.php
<?php
namespace app\common\command;
use Psy\Shell;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Psysh extends Command
{
protected function configure()
{
$this->setName('Psysh')
->setDescription('PHP command');
}
protected function execute(Input $input, Output $output)
{
// 启动Psysh
$sh = new Shell();
$sh->run();
}
}
配置 application/command.php
文件
return [
'psysh' => 'app\common\command\Psysh',
];
使用方法
在框架根目录输入命令, 出现下方效果则表示安装成功
PS E:\project\ucenter> php .\think psysh
Psy Shell v0.9.9 (PHP 7.2.4 — cli) by Justin Hileman
>>>
在 PsySH 命令行下我们可以直接使用框架下的类、常量.
使用 PsySH 运行 Db 类查询数据库
确保你的数据库配置是可用的
PS E:\project\ucenter> php .\think psysh
Psy Shell v0.9.9 (PHP 7.2.4 — cli) by Justin Hileman
>>> Db::table('test')->find()
=> [
"id" => 1,
"name" => "test1",
]
>>>
哇 厉害厉害 多谢