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
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI

on:
push:
branches: [master]
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
tests:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
TERM: xterm-256color
strategy:
fail-fast: false
matrix:
php: ['8.2', '8.3', '8.4', '8.5']
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
- uses: ramsey/composer-install@v3
- run: vendor/bin/phpunit --stop-on-error
- run: composer cs
- run: composer static
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor/
.idea
/composer.lock
.phpunit.cache
/.phpunit.result.cache
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
</p>

<p align="center">
<a href="https://travis-ci.org/php-school/terminal" title="Build Status" target="_blank">
<img src="https://img.shields.io/travis/php-school/terminal/master.svg?style=flat-square&label=Linux" />
</a
<a href="https://codecov.io/github/php-school/terminal" title="Coverage Status" target="_blank">
<img src="https://img.shields.io/codecov/c/github/php-school/terminal.svg?style=flat-square" />
</a>
<a href="https://scrutinizer-ci.com/g/php-school/terminal/" title="Scrutinizer Code Quality" target="_blank">
<img src="https://img.shields.io/scrutinizer/g/php-school/terminal.svg?style=flat-square" />
<a href="https://github.com/php-school/terminal/actions/workflows/ci.yml" title="Build Status" target="_blank">
<img src="https://img.shields.io/github/actions/workflow/status/php-school/terminal/ci.yml?branch=master&style=flat-square&label=CI" />
</a>
<a href="https://phpschool-team.slack.com/messages">
<img src="https://phpschool.herokuapp.com/badge.svg">
Expand Down
34 changes: 21 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"name": "php-school/terminal",
"description": "A command line terminal utility in PHP",
"keywords": ["cli", "console", "terminal", "phpschool", "php-school"],
"keywords": [
"cli",
"console",
"terminal",
"phpschool",
"php-school"
],
"license": "MIT",
"authors": [
{
Expand All @@ -14,27 +20,29 @@
}
],
"require": {
"php" : ">=7.2"
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "^7.1",
"squizlabs/php_codesniffer": "^3.2",
"phpstan/phpstan": "^0.9.2"
"phpunit/phpunit": "^11.5",
"squizlabs/php_codesniffer": "^3.13",
"phpstan/phpstan": "^2.1"
},
"autoload" : {
"psr-4" : {
"autoload": {
"psr-4": {
"PhpSchool\\Terminal\\": "src"
}
},
"autoload-dev": {
"psr-4": { "PhpSchool\\TerminalTest\\": "test" }
"psr-4": {
"PhpSchool\\TerminalTest\\": "test"
}
},
"scripts" : {
"cs" : [
"phpcs src --standard=PSR2",
"phpcs test --standard=PSR2"
"scripts": {
"cs": [
"phpcs src --standard=PSR12",
"phpcs test --standard=PSR12"
],
"static" : [
"static": [
"phpstan analyse src --level=7"
]
}
Expand Down
30 changes: 20 additions & 10 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnWarning="true">
<testsuites>
<testsuite name="Terminal Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>

<phpunit colors="true">
<testsuite name="Terminal Test Suite">
<directory>./test</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Exception/NotInteractiveTerminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/
class NotInteractiveTerminal extends \RuntimeException
{
public static function inputNotInteractive() : self
public static function inputNotInteractive(): self
{
return new self('Input stream is not interactive (non TTY)');
}

public static function outputNotInteractive() : self
public static function outputNotInteractive(): self
{
return new self('Output stream is not interactive (non TTY)');
}
Expand Down
9 changes: 6 additions & 3 deletions src/IO/BufferedOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
*/
class BufferedOutput implements OutputStream
{
/**
* @var string
*/
private $buffer = '';

public function write(string $buffer): void
{
$this->buffer .= $buffer;
}

public function fetch(bool $clean = true) : string
public function fetch(bool $clean = true): string
{
$buffer = $this->buffer;

Expand All @@ -25,15 +28,15 @@ public function fetch(bool $clean = true) : string
return $buffer;
}

public function __toString() : string
public function __toString(): string
{
return $this->fetch();
}

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool
public function isInteractive(): bool
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/IO/InputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ interface InputStream
* Callback should be called with the number of bytes requested
* when ready.
*/
public function read(int $numBytes, callable $callback) : void;
public function read(int $numBytes, callable $callback): void;

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool;
public function isInteractive(): bool;
}
4 changes: 2 additions & 2 deletions src/IO/OutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ interface OutputStream
/**
* Write the buffer to the stream
*/
public function write(string $buffer) : void;
public function write(string $buffer): void;

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool;
public function isInteractive(): bool;
}
21 changes: 16 additions & 5 deletions src/IO/ResourceInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,41 @@ class ResourceInputStream implements InputStream
*/
private $blocking;

/**
* @param resource $stream
*/
public function __construct($stream = \STDIN)
{
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
throw new \InvalidArgumentException('Expected a valid stream');
}

/** @var array<string, mixed> $meta Not every stream type (eg php://memory) reports 'blocked' */
$meta = stream_get_meta_data($stream);
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
throw new \InvalidArgumentException('Expected a readable stream');
}

$this->blocking = $meta['blocked'];
$this->blocking = (bool) ($meta['blocked'] ?? true);
$this->stream = $stream;
}

/**
* Restore the blocking state.
*/
public function __destruct() {
stream_set_blocking($this->stream, $this->blocking);
public function __destruct()
{
if (is_resource($this->stream)) {
stream_set_blocking($this->stream, $this->blocking);
}
}

public function read(int $numBytes, callable $callback) : void
public function read(int $numBytes, callable $callback): void
{
if ($numBytes < 1) {
throw new \InvalidArgumentException('Number of bytes to read must be greater than 0');
}

$buffer = fread($this->stream, $numBytes);
if (!empty($buffer)) {
// Prevent blocking to handle pasted input.
Expand All @@ -61,7 +72,7 @@ public function read(int $numBytes, callable $callback) : void
/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool
public function isInteractive(): bool
{
return stream_isatty($this->stream);
}
Expand Down
5 changes: 4 additions & 1 deletion src/IO/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ResourceOutputStream implements OutputStream
*/
private $stream;

/**
* @param resource $stream
*/
public function __construct($stream = \STDOUT)
{
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
Expand All @@ -39,7 +42,7 @@ public function write(string $buffer): void
/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool
public function isInteractive(): bool
{
return stream_isatty($this->stream);
}
Expand Down
Loading
Loading