Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions src/IO/FStat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/**
* This file is part of the yannoff/console library
*
* (c) Yannoff (https://github.com/yannoff)
*
* @project yannoff/console
* @link https://github.com/yannoff/console
* @license https://github.com/yannoff/console/blob/master/LICENSE
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Yannoff\Component\Console\IO;

/**
* Class FStat
* Handle the inode file statistics
*
* @source https://stackoverflow.com/a/11327451
*
* @package Yannoff\Component\Console\IO
*/
class FStat
{
/**
* Octal mode values from libc headers
*/
const S_IFMT = 0170000;
const S_IFIFO = 0010000;
const S_IFCHR = 0020000;
const S_IFDIR = 0040000;
const S_IFBLK = 0060000;
const S_IFREG = 0100000;
const S_IFLNK = 0120000;
const S_IFSOCK = 0140000;

/**
* Get the handle current protection mode
*
* @param resource $handle
*
* @return int
*/
protected static function imode($handle)
{
$stat = fstat($handle);
return $stat['mode'] & self::S_IFMT;
}

/**
* Check whether the handle is a FIFO
*
* @param resource $handle
*
* @return bool
*/
public static function isFifo($handle)
{
return self::imode($handle) == self::S_IFIFO;
}

/**
* Check whether the handle is a character
*
* @param resource $handle
*
* @return bool
*/
public static function isChar($handle)
{
return self::imode($handle) == self::S_IFCHR;
}

/**
* Check whether the handle is a directory
*
* @param resource $handle
*
* @return bool
*/
public static function isDir($handle)
{
return self::imode($handle) == self::S_IFDIR;
}

/**
* Check whether the handle is a block device
*
* @param resource $handle
*
* @return bool
*/
public static function isBlock($handle)
{
return self::imode($handle) == self::S_IFBLK;
}

/**
* Check whether the handle is a regular file redirect
*
* @param resource $handle
*
* @return bool
*/
public static function isRegularFile($handle)
{
return self::imode($handle) == self::S_IFREG;
}

/**
* Check whether the handle is a symlink
*
* @param resource $handle
*
* @return bool
*/
public static function isLink($handle)
{
return self::imode($handle) == self::S_IFLNK;
}

/**
* Check whether the handle is a socket
*
* @param resource $handle
*
* @return bool
*/
public static function isSocket($handle)
{
return self::imode($handle) == self::S_IFSOCK;
}
}
6 changes: 5 additions & 1 deletion src/IO/Stream/IOReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ interface IOReader extends IOStream
/**
* Fetch contents from the input stream
*
* @param bool $interactive Whether to accept contents from user input or not
* If no contents are provided via a pipe or a regular file redirect, controls whether
* the terminal should wait for user input - until a terminating sequence is emitted
*
* @return string|false The contents or **false** in case of failure
*/
public function read();
public function read($interactive = false);
}
11 changes: 9 additions & 2 deletions src/IO/Stream/StandardInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ class StandardInput extends Wrapper implements IOReader
/**
* {@inheritdoc}
*/
public function read()
public function read($interactive = false)
{
return stream_get_contents($this->handle);
// Unless explicitly forced using the $interactive parameter,
// assume we only want to read from stdin when it's a piped
// input or a regular file redirect
if ($interactive || $this->isPiped() || $this->isFile()) {
return stream_get_contents($this->handle);
}

return '';
}
}
21 changes: 21 additions & 0 deletions src/IO/Stream/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Yannoff\Component\Console\Exception\IO\StreamLogicException;
use Yannoff\Component\Console\Exception\UndefinedConstantException;
use Yannoff\Component\Console\IO\ConstantAccessor;
use Yannoff\Component\Console\IO\FStat;

/**
* Class Wrapper
Expand Down Expand Up @@ -90,6 +91,26 @@ public function getOpenMode()
return $this->constant('MODE');
}

/**
* Check whether the stream is a FIFO
*
* @return bool
*/
public function isPiped()
{
return FStat::isFifo($this->handle);
}

/**
* Check whether the stream is a regular file redirect
*
* @return bool
*/
public function isFile()
{
return FStat::isRegularFile($this->handle);
}

/**
* Generic getter for Wrapper child classes constants
*
Expand Down
6 changes: 4 additions & 2 deletions src/IO/StreamAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ public function ioerror($message = '', $ending = Formatter::LF)
/**
* Read contents from the standard input
*
* @param bool $interactive Whether to accept user input
*
* @return string|false The contents or **false** in case of failure
*/
public function ioread()
public function ioread($interactive = false)
{
$reader = StreamInitializer::getReader($this, IOStream::STDIN);

return $reader->read();
return $reader->read($interactive);
}
}
Loading