treehousetim/command 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

treehousetim/command

Composer 安装命令:

composer create-project treehousetim/command

包简介

A simple, zero-dependency, shell command class for php.

README 文档

README

simple, dependency-free, uncomplicated shell scripting command class

Build Status

[TOC]

Installing

composer require treehousetim/command

Usage

Simple command with argument

<?php

$command = new \treehousetim\command\command();
$command
	->command( 'ls' )
	->arg( '-lah' );

echo $command->getExecutableString();

// output:
// ls '-lah'
?>

Simple command that is piped

Notice the use of the factory method here to make code pretty.

<?php

echo \treehousetim\command\command::factory()
->command( 'ls' )
	->arg( '-lah' )
->pipeCommand(
	command::factory()
		->command( 'more' )
	)
->getExecutableString();

// output:
// ls '-lah' | more


Advanced Usage

treehousetim/command supports sub commands, combining stderr and stdout and output redirection to a file.

<?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

Executing your command

Simply call $command->exec(); to execute your command.

Each time ->exec(); is called, an entry in the $command->execResults array is created.

$command->execResults array

The format of $command->execResults[] is as follows:

[
	'content' => $content,
	'status' => $returnStatus,
	'command' => $command
];

If all you want is a list of commands that have been executed, without the other array entries you may use $command->commandLog[].

Command log

echo $command->commandLog[0];

The output from your command is returned from the exec() method.

Logging

Command executions may be logged. To set up logging, pass any \Psr\Log\LoggerInterface object to the constructor or the factory method on \treehousetim\command\command to log executions.

Example using 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

?>

Testing the codebase

If you have cloned this repo, you can run the tests.

Run composer install to install PHPUnit and Monolog.

  1. composer install
  2. ./vendor/bin/phpunit test

统计信息

  • 总下载量: 139
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固