Primo Committ
This commit is contained in:
179
vendor/psy/psysh/src/Readline/GNUReadline.php
vendored
Normal file
179
vendor/psy/psysh/src/Readline/GNUReadline.php
vendored
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2022 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Readline;
|
||||
|
||||
/**
|
||||
* A Readline interface implementation for GNU Readline.
|
||||
*
|
||||
* This is by far the coolest way to do it, if you can.
|
||||
*
|
||||
* Oh well.
|
||||
*/
|
||||
class GNUReadline implements Readline
|
||||
{
|
||||
/** @var string|false */
|
||||
protected $historyFile;
|
||||
/** @var int */
|
||||
protected $historySize;
|
||||
/** @var bool */
|
||||
protected $eraseDups;
|
||||
|
||||
/**
|
||||
* GNU Readline is supported iff `readline_list_history` is defined. PHP
|
||||
* decided it would be awesome to swap out GNU Readline for Libedit, but
|
||||
* they ended up shipping an incomplete implementation. So we've got this.
|
||||
*
|
||||
* NOTE: As of PHP 7.4, PHP sometimes has history support in the Libedit
|
||||
* wrapper, so that will use the GNUReadline implementation as well!
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \function_exists('readline') && \function_exists('readline_list_history');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this readline implementation supports bracketed paste.
|
||||
*
|
||||
* Currently, the GNU readline implementation does, but the libedit wrapper does not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supportsBracketedPaste(): bool
|
||||
{
|
||||
return self::isSupported() && \stripos(\readline_info('library_version') ?: '', 'editline') === false;
|
||||
}
|
||||
|
||||
/**
|
||||
* GNU Readline constructor.
|
||||
*
|
||||
* @param string|false $historyFile
|
||||
* @param int $historySize
|
||||
* @param bool $eraseDups
|
||||
*/
|
||||
public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
|
||||
{
|
||||
$this->historyFile = ($historyFile !== null) ? $historyFile : false;
|
||||
$this->historySize = $historySize;
|
||||
$this->eraseDups = $eraseDups;
|
||||
|
||||
\readline_info('readline_name', 'psysh');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addHistory(string $line): bool
|
||||
{
|
||||
if ($res = \readline_add_history($line)) {
|
||||
$this->writeHistory();
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clearHistory(): bool
|
||||
{
|
||||
if ($res = \readline_clear_history()) {
|
||||
$this->writeHistory();
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listHistory(): array
|
||||
{
|
||||
return \readline_list_history();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readHistory(): bool
|
||||
{
|
||||
\readline_read_history();
|
||||
\readline_clear_history();
|
||||
|
||||
return \readline_read_history($this->historyFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readline(string $prompt = null)
|
||||
{
|
||||
return \readline($prompt);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function redisplay()
|
||||
{
|
||||
\readline_redisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeHistory(): bool
|
||||
{
|
||||
// We have to write history first, since it is used
|
||||
// by Libedit to list history
|
||||
if ($this->historyFile !== false) {
|
||||
$res = \readline_write_history($this->historyFile);
|
||||
} else {
|
||||
$res = true;
|
||||
}
|
||||
|
||||
if (!$res || !$this->eraseDups && !$this->historySize > 0) {
|
||||
return $res;
|
||||
}
|
||||
|
||||
$hist = $this->listHistory();
|
||||
if (!$hist) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->eraseDups) {
|
||||
// flip-flip technique: removes duplicates, latest entries win.
|
||||
$hist = \array_flip(\array_flip($hist));
|
||||
// sort on keys to get the order back
|
||||
\ksort($hist);
|
||||
}
|
||||
|
||||
if ($this->historySize > 0) {
|
||||
$histsize = \count($hist);
|
||||
if ($histsize > $this->historySize) {
|
||||
$hist = \array_slice($hist, $histsize - $this->historySize);
|
||||
}
|
||||
}
|
||||
|
||||
\readline_clear_history();
|
||||
foreach ($hist as $line) {
|
||||
\readline_add_history($line);
|
||||
}
|
||||
|
||||
if ($this->historyFile !== false) {
|
||||
return \readline_write_history($this->historyFile);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
129
vendor/psy/psysh/src/Readline/HoaConsole.php
vendored
Normal file
129
vendor/psy/psysh/src/Readline/HoaConsole.php
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2022 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Readline;
|
||||
|
||||
use Hoa\Console\Console;
|
||||
use Hoa\Console\Cursor;
|
||||
use Hoa\Console\Readline\Readline as HoaReadline;
|
||||
use Psy\Exception\BreakException;
|
||||
|
||||
/**
|
||||
* Hoa\Console Readline implementation.
|
||||
*/
|
||||
class HoaConsole implements Readline
|
||||
{
|
||||
/** @var HoaReadline */
|
||||
private $hoaReadline;
|
||||
|
||||
/** @var string|null */
|
||||
private $lastPrompt;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \class_exists(Console::class, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function supportsBracketedPaste(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->hoaReadline = new HoaReadline();
|
||||
$this->hoaReadline->addMapping('\C-l', function () {
|
||||
$this->redisplay();
|
||||
|
||||
return HoaReadline::STATE_NO_ECHO;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addHistory(string $line): bool
|
||||
{
|
||||
$this->hoaReadline->addHistory($line);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clearHistory(): bool
|
||||
{
|
||||
$this->hoaReadline->clearHistory();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listHistory(): array
|
||||
{
|
||||
$i = 0;
|
||||
$list = [];
|
||||
while (($item = $this->hoaReadline->getHistory($i++)) !== null) {
|
||||
$list[] = $item;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readHistory(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws BreakException if user hits Ctrl+D
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function readline(string $prompt = null)
|
||||
{
|
||||
$this->lastPrompt = $prompt;
|
||||
|
||||
return $this->hoaReadline->readLine($prompt);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function redisplay()
|
||||
{
|
||||
$currentLine = $this->hoaReadline->getLine();
|
||||
Cursor::clear('all');
|
||||
echo $this->lastPrompt, $currentLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeHistory(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
118
vendor/psy/psysh/src/Readline/Libedit.php
vendored
Normal file
118
vendor/psy/psysh/src/Readline/Libedit.php
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2022 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Readline;
|
||||
|
||||
use Psy\Util\Str;
|
||||
|
||||
/**
|
||||
* A Libedit-based Readline implementation.
|
||||
*
|
||||
* This is largely the same as the Readline implementation, but it emulates
|
||||
* support for `readline_list_history` since PHP decided it was a good idea to
|
||||
* ship a fake Readline implementation that is missing history support.
|
||||
*
|
||||
* NOTE: As of PHP 7.4, PHP sometimes has history support in the Libedit
|
||||
* wrapper, so it will use the GNUReadline implementation rather than this one.
|
||||
*/
|
||||
class Libedit extends GNUReadline
|
||||
{
|
||||
private $hasWarnedOwnership = false;
|
||||
|
||||
/**
|
||||
* Let's emulate GNU Readline by manually reading and parsing the history file!
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \function_exists('readline') && !\function_exists('readline_list_history');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function supportsBracketedPaste(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listHistory(): array
|
||||
{
|
||||
$history = \file_get_contents($this->historyFile);
|
||||
if (!$history) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// libedit doesn't seem to support non-unix line separators.
|
||||
$history = \explode("\n", $history);
|
||||
|
||||
// remove history signature if it exists
|
||||
if ($history[0] === '_HiStOrY_V2_') {
|
||||
\array_shift($history);
|
||||
}
|
||||
|
||||
// decode the line
|
||||
$history = \array_map([$this, 'parseHistoryLine'], $history);
|
||||
// filter empty lines & comments
|
||||
return \array_values(\array_filter($history));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeHistory(): bool
|
||||
{
|
||||
$res = parent::writeHistory();
|
||||
|
||||
// Libedit apparently refuses to save history if the history file is not
|
||||
// owned by the user, even if it is writable. Warn when this happens.
|
||||
//
|
||||
// See https://github.com/bobthecow/psysh/issues/552
|
||||
if ($res === false && !$this->hasWarnedOwnership) {
|
||||
if (\is_file($this->historyFile) && \is_writable($this->historyFile)) {
|
||||
$this->hasWarnedOwnership = true;
|
||||
$msg = \sprintf('Error writing history file, check file ownership: %s', $this->historyFile);
|
||||
\trigger_error($msg, \E_USER_NOTICE);
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* From GNUReadline (readline/histfile.c & readline/histexpand.c):
|
||||
* lines starting with "\0" are comments or timestamps;
|
||||
* if "\0" is found in an entry,
|
||||
* everything from it until the next line is a comment.
|
||||
*
|
||||
* @param string $line The history line to parse
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function parseHistoryLine(string $line)
|
||||
{
|
||||
// empty line, comment or timestamp
|
||||
if (!$line || $line[0] === "\0") {
|
||||
return;
|
||||
}
|
||||
// if "\0" is found in an entry, then
|
||||
// everything from it until the end of line is a comment.
|
||||
if (($pos = \strpos($line, "\0")) !== false) {
|
||||
$line = \substr($line, 0, $pos);
|
||||
}
|
||||
|
||||
return ($line !== '') ? Str::unvis($line) : null;
|
||||
}
|
||||
}
|
||||
83
vendor/psy/psysh/src/Readline/Readline.php
vendored
Normal file
83
vendor/psy/psysh/src/Readline/Readline.php
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2022 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Readline;
|
||||
|
||||
/**
|
||||
* An interface abstracting the various readline_* functions.
|
||||
*/
|
||||
interface Readline
|
||||
{
|
||||
/**
|
||||
* Check whether this Readline class is supported by the current system.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool;
|
||||
|
||||
/**
|
||||
* Check whether this Readline class supports bracketed paste.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supportsBracketedPaste(): bool;
|
||||
|
||||
/**
|
||||
* Add a line to the command history.
|
||||
*
|
||||
* @param string $line
|
||||
*
|
||||
* @return bool Success
|
||||
*/
|
||||
public function addHistory(string $line): bool;
|
||||
|
||||
/**
|
||||
* Clear the command history.
|
||||
*
|
||||
* @return bool Success
|
||||
*/
|
||||
public function clearHistory(): bool;
|
||||
|
||||
/**
|
||||
* List the command history.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listHistory(): array;
|
||||
|
||||
/**
|
||||
* Read the command history.
|
||||
*
|
||||
* @return bool Success
|
||||
*/
|
||||
public function readHistory(): bool;
|
||||
|
||||
/**
|
||||
* Read a single line of input from the user.
|
||||
*
|
||||
* @param string|null $prompt
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function readline(string $prompt = null);
|
||||
|
||||
/**
|
||||
* Redraw readline to redraw the display.
|
||||
*/
|
||||
public function redisplay();
|
||||
|
||||
/**
|
||||
* Write the command history to a file.
|
||||
*
|
||||
* @return bool Success
|
||||
*/
|
||||
public function writeHistory(): bool;
|
||||
}
|
||||
155
vendor/psy/psysh/src/Readline/Transient.php
vendored
Normal file
155
vendor/psy/psysh/src/Readline/Transient.php
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2022 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\Readline;
|
||||
|
||||
use Psy\Exception\BreakException;
|
||||
|
||||
/**
|
||||
* An array-based Readline emulation implementation.
|
||||
*/
|
||||
class Transient implements Readline
|
||||
{
|
||||
private $history;
|
||||
private $historySize;
|
||||
private $eraseDups;
|
||||
private $stdin;
|
||||
|
||||
/**
|
||||
* Transient Readline is always supported.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function supportsBracketedPaste(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transient Readline constructor.
|
||||
*/
|
||||
public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
|
||||
{
|
||||
// don't do anything with the history file...
|
||||
$this->history = [];
|
||||
$this->historySize = $historySize;
|
||||
$this->eraseDups = $eraseDups;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addHistory(string $line): bool
|
||||
{
|
||||
if ($this->eraseDups) {
|
||||
if (($key = \array_search($line, $this->history)) !== false) {
|
||||
unset($this->history[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->history[] = $line;
|
||||
|
||||
if ($this->historySize > 0) {
|
||||
$histsize = \count($this->history);
|
||||
if ($histsize > $this->historySize) {
|
||||
$this->history = \array_slice($this->history, $histsize - $this->historySize);
|
||||
}
|
||||
}
|
||||
|
||||
$this->history = \array_values($this->history);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function clearHistory(): bool
|
||||
{
|
||||
$this->history = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listHistory(): array
|
||||
{
|
||||
return $this->history;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readHistory(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws BreakException if user hits Ctrl+D
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function readline(string $prompt = null)
|
||||
{
|
||||
echo $prompt;
|
||||
|
||||
return \rtrim(\fgets($this->getStdin()), "\n\r");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function redisplay()
|
||||
{
|
||||
// noop
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function writeHistory(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a STDIN file handle.
|
||||
*
|
||||
* @throws BreakException if user hits Ctrl+D
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
private function getStdin()
|
||||
{
|
||||
if (!isset($this->stdin)) {
|
||||
$this->stdin = \fopen('php://stdin', 'r');
|
||||
}
|
||||
|
||||
if (\feof($this->stdin)) {
|
||||
throw new BreakException('Ctrl+D');
|
||||
}
|
||||
|
||||
return $this->stdin;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user