treehousetim / command
一个简单、无依赖的PHP壳命令类。
0.0.4
2019-11-14 21:08 UTC
Requires
- php: ^7.0
- psr/log: ^1.1
Requires (Dev)
- monolog/monolog: ^1.0
- phpunit/phpunit: ^6
Suggests
- monolog/monolog: PSR compatible logger for logging your command executions
This package is auto-updated.
Last update: 2024-09-26 09:24:31 UTC
README
简单、无依赖、简单的壳脚本命令类
[目录]
安装
composer require treehousetim/command
用法
带有参数的简单命令
<?php
$command = new \treehousetim\command\command();
$command
->command( 'ls' )
->arg( '-lah' );
echo $command->getExecutableString();
// output:
// ls '-lah'
?>
可以管道的简单命令
注意这里使用了工厂方法来使代码更美观。
<?php
echo \treehousetim\command\command::factory()
->command( 'ls' )
->arg( '-lah' )
->pipeCommand(
command::factory()
->command( 'more' )
)
->getExecutableString();
// output:
// ls '-lah' | more
高级用法
treehousetim/command支持子命令,结合stderr和stdout以及输出重定向到文件。
<?php
use \treehousetim\command\command;
$connection = 'user@example.com';
$loginPath = 'mylogin';
$db = 'my_database';
$tables = ['table1', 'table2'];
$where = 'id > 1200';
echo command::factory()
->command( 'ssh' )
->arg( $connection )
->subCommand(
command::factory()
->command( '/usr/bin/mysqldump' )
->arg( '--login-path=' . $loginPath )
->arg( '--skip-add-drop-table' )
->arg( '--no-create-info' )
->arg( $db )
->arg( implode( ',', $tables ) )
->arg( '--where=' . $where )
->stdoutStderrCombine()
)
->stdoutStderrCombine()
->outputFile( '/path/to/file.sql' )
->getExecutableString();
// output
// ssh 'user@example.com' "/usr/bin/mysqldump '--login-path=mylogin' '--skip-add-drop-table' '--no-create-info' 'my_database' 'table1,table2' '--where=id > 1200' 2>&1" 2>&1 > /path/to/file.sql
// minor difference: outputAppendFile - uses >> instead of >
echo command::factory()
->command( 'ssh' )
->arg( $connection )
->subCommand(
command::factory()
->command( '/usr/bin/mysqldump' )
->arg( '--login-path=' . $loginPath )
->arg( '--skip-add-drop-table' )
->arg( '--no-create-info' )
->arg( $db )
->arg( implode( ',', $tables ) )
->arg( '--where=' . $where )
->stdoutStderrCombine()
)
->stdoutStderrCombine()
->outputAppendFile( '/path/to/file.sql' )
->getExecutableString();
// output
// ssh 'user@example.com' "/usr/bin/mysqldump '--login-path=mylogin' '--skip-add-drop-table' '--no-create-info' 'my_database' 'table1,table2' '--where=id > 1200' 2>&1" 2>&1 >> /path/to/file.sql
执行您的命令
只需调用$command->exec();来执行您的命令。
每次调用->exec();时,在$command->execResults数组中创建一个条目。
$command->execResults数组
$command->execResults[]的格式如下
[
'content' => $content,
'status' => $returnStatus,
'command' => $command
];
如果您只想获取已执行的命令列表,而不需要其他数组条目,可以使用$command->commandLog[]。
命令日志
echo $command->commandLog[0];
您的命令的输出是从exec()方法返回的。
日志记录
命令执行可以被记录。要设置日志记录,请将任何\Psr\Log\LoggerInterface对象传递给构造函数或\treehousetim\command\command上的工厂方法以记录执行。
使用Monolog的示例
<?php
use \treehousetim\command\command;
$log = new \Monolog\Logger( 'My Example Program' );
$log->pushHandler( new StreamHandler( './example.log', \Monolog\Logger::DEBUG ) );
$command = command::factory( $log )
->command( 'ls' )
->arg( '-lah' )
->exec();
$output = $command->exec();
echo $command->commandLog[0];
echo PHP_EOL;
echo $output
?>
测试代码库
如果您已经克隆了这个仓库,可以运行测试。
运行composer install来安装PHPUnit和Monolog。
composer install./vendor/bin/phpunit test