Skip to content

Rewrite #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 22, 2018
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
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php

php:
- 7.1
- 7.2

install:
- composer self-update
- composer install

before_script:
- mkdir -p build/logs

script:
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml
- composer cs
- composer static

after_script:
- bash <(curl -s https://codecov.io/bash)
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover ./build/logs/clover.xml
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"email": "[email protected]"
}
],
"require-dev": {
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.0"
},
"require": {
"php" : ">=7.0",
"ext-posix": "*",
"myclabs/php-enum": "^1.5"
"php" : ">=7.1",
"ext-posix": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.1",
"squizlabs/php_codesniffer": "^3.2",
"phpstan/phpstan": "^0.9.2"
},
"autoload" : {
"psr-4" : {
Expand All @@ -34,6 +34,9 @@
"cs" : [
"phpcs src --standard=PSR2",
"phpcs test --standard=PSR2"
],
"static" : [
"phpstan analyse src --level=7"
]
}
}
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true">
<testsuite name="Terminal Test Suite">
<directory>./test</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
19 changes: 19 additions & 0 deletions src/Exception/NotInteractiveTerminal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace PhpSchool\Terminal\Exception;

/**
* @author Aydin Hassan <[email protected]>
*/
class NotInteractiveTerminal extends \RuntimeException
{
public static function inputNotInteractive() : self
{
return new self('Input stream is not interactive (non TTY)');
}

public static function outputNotInteractive() : self
{
return new self('Output stream is not interactive (non TTY)');
}
}
40 changes: 40 additions & 0 deletions src/IO/BufferedOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PhpSchool\Terminal\IO;

/**
* @author Aydin Hassan <[email protected]>
*/
class BufferedOutput implements OutputStream
{
private $buffer = '';

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

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

if ($clean) {
$this->buffer = '';
}

return $buffer;
}

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

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool
{
return false;
}
}
20 changes: 20 additions & 0 deletions src/IO/InputStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpSchool\Terminal\IO;

/**
* @author Aydin Hassan <[email protected]>
*/
interface InputStream
{
/**
* Callback should be called with the number of bytes requested
* when ready.
*/
public function read(int $numBytes, callable $callback) : void;

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool;
}
19 changes: 19 additions & 0 deletions src/IO/OutputStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace PhpSchool\Terminal\IO;

/**
* @author Aydin Hassan <[email protected]>
*/
interface OutputStream
{
/**
* Write the buffer to the stream
*/
public function write(string $buffer) : void;

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool;
}
47 changes: 47 additions & 0 deletions src/IO/ResourceInputStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace PhpSchool\Terminal\IO;

use function is_resource;
use function get_resource_type;
use function stream_get_meta_data;
use function strpos;

/**
* @author Aydin Hassan <[email protected]>
*/
class ResourceInputStream implements InputStream
{
/**
* @var resource
*/
private $stream;

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

$meta = stream_get_meta_data($stream);
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
throw new \InvalidArgumentException('Expected a readable stream');
}

$this->stream = $stream;
}

public function read(int $numBytes, callable $callback) : void
{
$buffer = fread($this->stream, $numBytes);
$callback($buffer);
}

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool
{
return posix_isatty($this->stream);
}
}
46 changes: 46 additions & 0 deletions src/IO/ResourceOutputStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace PhpSchool\Terminal\IO;

use function is_resource;
use function get_resource_type;
use function stream_get_meta_data;
use function strpos;

/**
* @author Aydin Hassan <[email protected]>
*/
class ResourceOutputStream implements OutputStream
{
/**
* @var resource
*/
private $stream;

public function __construct($stream = STDOUT)
{
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
throw new \InvalidArgumentException('Expected a valid stream');
}

$meta = stream_get_meta_data($stream);
if (strpos($meta['mode'], 'r') !== false && strpos($meta['mode'], '+') === false) {
throw new \InvalidArgumentException('Expected a writable stream');
}

$this->stream = $stream;
}

public function write(string $buffer): void
{
fwrite($this->stream, $buffer);
}

/**
* Whether the stream is connected to an interactive terminal
*/
public function isInteractive() : bool
{
return posix_isatty($this->stream);
}
}
126 changes: 126 additions & 0 deletions src/InputCharacter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace PhpSchool\Terminal;

use function in_array;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels wierd, is it needed? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really - the EA inspections plugin for PHP storm recommends it for optimisation - I can remove if you want?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind it being there, just didn't understand why it was


/**
* @author Aydin Hassan <[email protected]>
*/
class InputCharacter
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this represents a single character or control character read from the input source. It contains a bunch of helper methods for dealing with control sequences and validation of them.

{
/**
* @var string
*/
private $data;

public const UP = 'UP';
public const DOWN = 'DOWN';
public const RIGHT = 'RIGHT';
public const LEFT = 'LEFT';
public const CTRLA = 'CTRLA';
public const CTRLB = 'CTRLB';
public const CTRLE = 'CTRLE';
public const CTRLF = 'CTRLF';
public const BACKSPACE = 'BACKSPACE';
public const CTRLW = 'CTRLW';
public const ENTER = 'ENTER';
public const TAB = 'TAB';

private static $controls = [
"\033[A" => self::UP,
"\033[B" => self::DOWN,
"\033[C" => self::RIGHT,
"\033[D" => self::LEFT,
"\001" => self::CTRLA,
"\002" => self::CTRLB,
"\005" => self::CTRLE,
"\006" => self::CTRLF,
"\010" => self::BACKSPACE,
"\177" => self::BACKSPACE,
"\027" => self::CTRLW,
"\n" => self::ENTER,
"\t" => self::TAB,
];

public function __construct(string $data)
{
$this->data = $data;
}

/**
* Is this character a control sequence?
*/
public function isControl() : bool
{
return isset(static::$controls[$this->data]);
}

/**
* Is this character a normal character?
*/
public function isNotControl() : bool
{
return ! $this->isControl();
}

/**
* Get the raw character or control sequence
*/
public function get() : string
{
return $this->data;
}

/**
* Get the actual control name that this sequence represents.
* One of the class constants. Eg. self::UP.
*
* Throws an exception if the character is not actually a control sequence
*/
public function getControl() : string
{
if (!isset(static::$controls[$this->data])) {
throw new \RuntimeException(sprintf('Character "%s" is not a control', $this->data));
}

return static::$controls[$this->data];
}

/**
* Get the raw character or control sequence
*/
public function __toString() : string
{
return $this->get();
}

/**
* Does the given control name exist? eg self::UP.
*/
public static function controlExists(string $controlName) : bool
{
return in_array($controlName, static::$controls, true);
}

/**
* Get all of the available control names
*/
public static function getControls() : array
{
return array_values(array_unique(static::$controls));
}

/**
* Create a instance from a given control name. Throws an exception if the
* control name does not exist.
*/
public static function fromControlName(string $controlName) : self
{
if (!static::controlExists($controlName)) {
throw new \InvalidArgumentException(sprintf('Control "%s" does not exist', $controlName));
}

return new static(array_search($controlName, static::$controls, true));
}
}
Loading