Update v1.0.6

This commit is contained in:
Bhanu Slathia
2016-02-16 23:24:52 +05:30
parent c710c20b9e
commit b1f62846ab
7662 changed files with 1361647 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 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, but it doesn't work with new PHP.
*
* Oh well.
*/
class GNUReadline implements Readline
{
protected $historyFile;
protected $historySize;
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.
*
* @return bool
*/
public static function isSupported()
{
return function_exists('readline_list_history');
}
/**
* GNU Readline constructor.
*/
public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
{
$this->historyFile = $historyFile;
$this->historySize = $historySize;
$this->eraseDups = $eraseDups;
}
/**
* {@inheritDoc}
*/
public function addHistory($line)
{
if ($res = readline_add_history($line)) {
$this->writeHistory();
}
return $res;
}
/**
* {@inheritDoc}
*/
public function clearHistory()
{
if ($res = readline_clear_history()) {
$this->writeHistory();
}
return $res;
}
/**
* {@inheritDoc}
*/
public function listHistory()
{
return readline_list_history();
}
/**
* {@inheritDoc}
*/
public function readHistory()
{
// Workaround PHP bug #69054
//
// If open_basedir is set, readline_read_history() segfaults. This will be fixed in 5.6.7:
//
// https://github.com/php/php-src/blob/423a057023ef3c00d2ffc16a6b43ba01d0f71796/NEWS#L19-L21
//
// TODO: add a PHP version check after next point release
if (!ini_get('open_basedir')) {
readline_read_history();
}
readline_clear_history();
return readline_read_history($this->historyFile);
}
/**
* {@inheritDoc}
*/
public function readline($prompt = null)
{
return readline($prompt);
}
/**
* {@inheritDoc}
*/
public function redisplay()
{
readline_redisplay();
}
/**
* {@inheritDoc}
*/
public function writeHistory()
{
// We have to write history first, since it is used
// by Libedit to list history
$res = readline_write_history($this->historyFile);
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);
}
return readline_write_history($this->historyFile);
}
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 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\String;
/**
* 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.
*/
class Libedit extends GNUReadline
{
/**
* Let's emulate GNU Readline by manually reading and parsing the history file!
*
* @return boolean
*/
public static function isSupported()
{
return function_exists('readline') && !function_exists('readline_list_history');
}
/**
* {@inheritDoc}
*/
public function listHistory()
{
$history = file_get_contents($this->historyFile);
if (!$history) {
return array();
}
// libedit doesn't seem to support non-unix line separators.
$history = explode("\n", $history);
// shift the history signature, ensure it's valid
if (array_shift($history) !== '_HiStOrY_V2_') {
return array();
}
// decode the line
$history = array_map(array($this, 'parseHistoryLine'), $history);
// filter empty lines & comments
return array_values(array_filter($history));
}
/**
* 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($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 !== '') ? String::unvis($line) : null;
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 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 boolean
*/
public static function isSupported();
/**
* Add a line to the command history.
*
* @param string $line
*
* @return bool Success
*/
public function addHistory($line);
/**
* Clear the command history.
*
* @return bool Success
*/
public function clearHistory();
/**
* List the command history.
*
* @return array
*/
public function listHistory();
/**
* Read the command history.
*
* @return bool Success
*/
public function readHistory();
/**
* Read a single line of input from the user.
*
* @param null|string $prompt
*
* @return false|string
*/
public function readline($prompt = null);
/**
* Redraw readline to redraw the display.
*/
public function redisplay();
/**
* Write the command history to a file.
*
* @return bool Success
*/
public function writeHistory();
}

View File

@@ -0,0 +1,146 @@
<?php
/*
* This file is part of Psy Shell
*
* (c) 2012-2014 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;
/**
* Transient Readline is always supported.
*
* {@inheritDoc}
*/
public static function isSupported()
{
return true;
}
/**
* Transient Readline constructor.
*/
public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
{
// don't do anything with the history file...
$this->history = array();
$this->historySize = $historySize;
$this->eraseDups = $eraseDups;
}
/**
* {@inheritDoc}
*/
public function addHistory($line)
{
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()
{
$this->history = array();
return true;
}
/**
* {@inheritDoc}
*/
public function listHistory()
{
return $this->history;
}
/**
* {@inheritDoc}
*/
public function readHistory()
{
return true;
}
/**
* {@inheritDoc}
*
* @throws BreakException if user hits Ctrl+D
*
* @return string
*/
public function readline($prompt = null)
{
echo $prompt;
return rtrim(fgets($this->getStdin(), 1024));
}
/**
* {@inheritDoc}
*/
public function redisplay()
{
// noop
}
/**
* {@inheritDoc}
*/
public function writeHistory()
{
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;
}
}