定制 hexmode/io-mode 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

hexmode/io-mode

最新稳定版本:1.0

Composer 安装命令:

composer require hexmode/io-mode

包简介

More than just posix_isatty()

README 文档

README

IOMode is based on a StackOverflow answer and incorportes that code into a reusable library.

From SO:

I also needed a slightly more flexible solution than posix_isatty that could detect:

Is the script being run from the terminal Is the script receiving data via a pipe or from a file Is the output being redirected to a file

Example

This library can be used in the following way:

use Hexmode\IOMode;

if ( IOMode::isTTY() ) {
    print "tty\n";
}
if ( IOMode::isFifo() ) {
    print "pipe\n";
}
if ( IOMode::isReg() ) {
    print "regular file (redirection)\n";
}
if ( IOMode::isChr() ) {
    print "character device (normal)\n";
}
if ( IOMode::isDir() ) {
    print "directory (?!?!)\n";
}
if ( IOMode::isBlk() ) {
    print "block device\n";
}
if ( IOMode::isLnk() ) {
        print "symlink\n";
}
if ( IOMode::isSock() ) {
    print "socket\n";
}

(This above script is in the included example/doc1.php.)

Given the following commands, you’ll get different output:

$ php example/doc1.php
tty
character device (normal)
$ echo 1 | php example/doc1.php
pipe
$ mkdir tmp; php example/doc1.php < tmp;rmdir tmp
directory (?!?!)
$ sudo sh -c ‘php example/doc1.php> < /dev/sda1'
block device

Using with other handles

In addition to the is*() static methods that provide information on STDIN, there are methods that can be invoked on a IOMode object.

The following code is included as example/doc2.php:

require( "vendor/autoload.php" );
use Hexmode\IOMode\IOMode;

$stderr = new IOMode( STDERR );
$stdout = new IOMode( STDOUT );
$stdin  = new IOMode( STDIN );

foreach (
	[ "in" => $stdin, "out" => $stdout, "err" => $stderr ] as $label => $io
) {
	if ( $io->TTY() ) {
		print "$label: tty\n";
	}
	if ( $io->Fifo() ) {
		print "$label: pipe\n";
	}
	if ( $io->Reg() ) {
		print "$label: regular file (redirection)\n";
	}
	if ( $io->Chr() ) {
		print "$label: character device (normal)\n";
	}
	if ( $io->Dir() ) {
		print "$label: directory (?!?!)\n";
	}
	if ( $io->Blk() ) {
		print "$label: block device\n";
	}
	if ( $io->Lnk() ) {
		print "$label: symlink\n";
	}
	if ( $io->Sock() ) {
		print "$label: socket\n";
	}
}

Given the following commands, you’ll get different output:

$ sudo sh -c 'php example/doc2.php < /dev/nvme0n1p1'
in: block device
out: tty
out: character device (normal)
err: tty
err: character device (normal)
$ mkdir tmp; php example/doc2.php < tmp 2> /tmp/t;rmdir tmp
in: directory (?!?!)
out: tty
out: character device (normal)
err: regular file (redirection)
$ example/doc2.php > /tmp/t
$ cat /tmp/t; rm /tmp/t
in: tty
in: character device (normal)
out: regular file (redirection)
err: tty
err: character device (normal)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2019-06-24