Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -0,0 +1,26 @@
<?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\Output;
use Symfony\Component\Console\Output\OutputInterface;
/**
* An output pager is much the same as a regular OutputInterface, but allows
* the stream to be flushed to a pager periodically.
*/
interface OutputPager extends OutputInterface
{
/**
* Close the current pager process.
*/
public function close();
}

View File

@@ -0,0 +1,39 @@
<?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\Output;
use Symfony\Component\Console\Output\StreamOutput;
/**
* A passthrough pager is a no-op. It simply wraps a StreamOutput's stream and
* does nothing when the pager is closed.
*/
class PassthruPager extends StreamOutput implements OutputPager
{
/**
* Constructor.
*
* @param StreamOutput $output
*/
public function __construct(StreamOutput $output)
{
parent::__construct($output->getStream());
}
/**
* Close the current pager process.
*/
public function close()
{
// nothing to do here
}
}

View File

@@ -0,0 +1,105 @@
<?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\Output;
use Symfony\Component\Console\Output\StreamOutput;
/**
* ProcOutputPager class.
*
* A ProcOutputPager instance wraps a regular StreamOutput's stream. Rather
* than writing directly to the stream, it shells out to a pager process and
* gives that process the stream as stdout. This means regular *nix commands
* like `less` and `more` can be used to page large amounts of output.
*/
class ProcOutputPager extends StreamOutput implements OutputPager
{
private $proc;
private $pipe;
private $stream;
private $cmd;
/**
* Constructor.
*
* @param StreamOutput $output
* @param string $cmd Pager process command (default: 'less -R -F -X')
*/
public function __construct(StreamOutput $output, string $cmd = 'less -R -F -X')
{
$this->stream = $output->getStream();
$this->cmd = $cmd;
}
/**
* Writes a message to the output.
*
* @param string $message A message to write to the output
* @param bool $newline Whether to add a newline or not
*
* @throws \RuntimeException When unable to write output (should never happen)
*/
public function doWrite($message, $newline)
{
$pipe = $this->getPipe();
if (false === @\fwrite($pipe, $message.($newline ? \PHP_EOL : ''))) {
// @codeCoverageIgnoreStart
// should never happen
$this->close();
throw new \RuntimeException('Unable to write output');
// @codeCoverageIgnoreEnd
}
\fflush($pipe);
}
/**
* Close the current pager process.
*/
public function close()
{
if (isset($this->pipe)) {
\fclose($this->pipe);
}
if (isset($this->proc)) {
$exit = \proc_close($this->proc);
if ($exit !== 0) {
throw new \RuntimeException('Error closing output stream');
}
}
$this->pipe = null;
$this->proc = null;
}
/**
* Get a pipe for paging output.
*
* If no active pager process exists, fork one and return its input pipe.
*/
private function getPipe()
{
if (!isset($this->pipe) || !isset($this->proc)) {
$desc = [['pipe', 'r'], $this->stream, \fopen('php://stderr', 'w')];
$this->proc = \proc_open($this->cmd, $desc, $pipes);
if (!\is_resource($this->proc)) {
throw new \RuntimeException('Error opening output stream');
}
$this->pipe = $pipes[0];
}
return $this->pipe;
}
}

View File

@@ -0,0 +1,210 @@
<?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\Output;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
* A ConsoleOutput subclass specifically for Psy Shell output.
*/
class ShellOutput extends ConsoleOutput
{
const NUMBER_LINES = 128;
private $paging = 0;
/** @var OutputPager */
private $pager;
/** @var Theme */
private $theme;
/**
* Construct a ShellOutput instance.
*
* @param mixed $verbosity (default: self::VERBOSITY_NORMAL)
* @param bool|null $decorated (default: null)
* @param OutputFormatterInterface|null $formatter (default: null)
* @param string|OutputPager|null $pager (default: null)
*/
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null, $pager = null, $theme = null)
{
parent::__construct($verbosity, $decorated, $formatter);
$this->theme = $theme ?? new Theme('modern');
$this->initFormatters();
if ($pager === null) {
$this->pager = new PassthruPager($this);
} elseif (\is_string($pager)) {
$this->pager = new ProcOutputPager($this, $pager);
} elseif ($pager instanceof OutputPager) {
$this->pager = $pager;
} else {
throw new \InvalidArgumentException('Unexpected pager parameter: '.$pager);
}
}
/**
* Page multiple lines of output.
*
* The output pager is started
*
* If $messages is callable, it will be called, passing this output instance
* for rendering. Otherwise, all passed $messages are paged to output.
*
* Upon completion, the output pager is flushed.
*
* @param string|array|\Closure $messages A string, array of strings or a callback
* @param int $type (default: 0)
*/
public function page($messages, int $type = 0)
{
if (\is_string($messages)) {
$messages = (array) $messages;
}
if (!\is_array($messages) && !\is_callable($messages)) {
throw new \InvalidArgumentException('Paged output requires a string, array or callback');
}
$this->startPaging();
if (\is_callable($messages)) {
$messages($this);
} else {
$this->write($messages, true, $type);
}
$this->stopPaging();
}
/**
* Start sending output to the output pager.
*/
public function startPaging()
{
$this->paging++;
}
/**
* Stop paging output and flush the output pager.
*/
public function stopPaging()
{
$this->paging--;
$this->closePager();
}
/**
* Writes a message to the output.
*
* Optionally, pass `$type | self::NUMBER_LINES` as the $type parameter to
* number the lines of output.
*
* @throws \InvalidArgumentException When unknown output type is given
*
* @param string|array $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline or not
* @param int $type The type of output
*/
public function write($messages, $newline = false, $type = 0)
{
if ($this->getVerbosity() === self::VERBOSITY_QUIET) {
return;
}
$messages = (array) $messages;
if ($type & self::NUMBER_LINES) {
$pad = \strlen((string) \count($messages));
$template = $this->isDecorated() ? "<aside>%{$pad}s</aside>: %s" : "%{$pad}s: %s";
if ($type & self::OUTPUT_RAW) {
$messages = \array_map([OutputFormatter::class, 'escape'], $messages);
}
foreach ($messages as $i => $line) {
$messages[$i] = \sprintf($template, $i, $line);
}
// clean this up for super.
$type = $type & ~self::NUMBER_LINES & ~self::OUTPUT_RAW;
}
parent::write($messages, $newline, $type);
}
/**
* Writes a message to the output.
*
* Handles paged output, or writes directly to the output stream.
*
* @param string $message A message to write to the output
* @param bool $newline Whether to add a newline or not
*/
public function doWrite($message, $newline)
{
if ($this->paging > 0) {
$this->pager->doWrite($message, $newline);
} else {
parent::doWrite($message, $newline);
}
}
/**
* Set the output Theme.
*/
public function setTheme(Theme $theme)
{
$this->theme = $theme;
$this->initFormatters();
}
/**
* Flush and close the output pager.
*/
private function closePager()
{
if ($this->paging <= 0) {
$this->pager->close();
}
}
/**
* Initialize output formatter styles.
*/
private function initFormatters()
{
$useGrayFallback = !$this->grayExists();
$this->theme->applyStyles($this->getFormatter(), $useGrayFallback);
$this->theme->applyErrorStyles($this->getErrorOutput()->getFormatter(), $useGrayFallback);
}
/**
* Checks if the "gray" color exists on the output.
*
* @return bool
*/
private function grayExists(): bool
{
try {
$this->write('<fg=gray></>');
} catch (\InvalidArgumentException $e) {
return false;
}
return true;
}
}

285
vendor/psy/psysh/src/Output/Theme.php vendored Normal file
View File

@@ -0,0 +1,285 @@
<?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\Output;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
/**
* An output Theme, which controls prompt strings, formatter styles, and compact output.
*/
class Theme
{
const MODERN_THEME = []; // Defaults :)
const COMPACT_THEME = [
'compact' => true,
];
const CLASSIC_THEME = [
'compact' => true,
'prompt' => '>>> ',
'bufferPrompt' => '... ',
'replayPrompt' => '--> ',
'returnValue' => '=> ',
];
const DEFAULT_STYLES = [
'info' => ['white', 'blue', ['bold']],
'warning' => ['black', 'yellow'],
'error' => ['white', 'red', ['bold']],
'whisper' => ['gray'],
'aside' => ['blue'],
'strong' => [null, null, ['bold']],
'return' => ['cyan'],
'urgent' => ['red'],
'hidden' => ['black'],
// Visibility
'public' => [null, null, ['bold']],
'protected' => ['yellow'],
'private' => ['red'],
'global' => ['cyan', null, ['bold']],
'const' => ['cyan'],
'class' => ['blue', null, ['underscore']],
'function' => [null],
'default' => [null],
// Types
'number' => ['magenta'],
'integer' => ['magenta'],
'float' => ['yellow'],
'string' => ['green'],
'bool' => ['cyan'],
'keyword' => ['yellow'],
'comment' => ['blue'],
'object' => ['blue'],
'resource' => ['yellow'],
// Code-specific formatting
'inline_html' => ['cyan'],
];
const ERROR_STYLES = ['info', 'warning', 'error', 'whisper'];
private $compact = false;
private $prompt = '> ';
private $bufferPrompt = '. ';
private $replayPrompt = '- ';
private $returnValue = '= ';
private $grayFallback = 'blue';
private $styles = [];
/**
* @param string|array $config theme name or config options
*/
public function __construct($config = 'modern')
{
if (\is_string($config)) {
switch ($config) {
case 'modern':
$config = static::MODERN_THEME;
break;
case 'compact':
$config = static::COMPACT_THEME;
break;
case 'classic':
$config = static::CLASSIC_THEME;
break;
default:
\trigger_error(\sprintf('Unknown theme: %s', $config), \E_USER_NOTICE);
$config = static::MODERN_THEME;
break;
}
}
if (!\is_array($config)) {
throw new \InvalidArgumentException('Invalid theme config');
}
foreach ($config as $name => $value) {
switch ($name) {
case 'compact':
$this->setCompact($value);
break;
case 'prompt':
$this->setPrompt($value);
break;
case 'bufferPrompt':
$this->setBufferPrompt($value);
break;
case 'replayPrompt':
$this->setReplayPrompt($value);
break;
case 'returnValue':
$this->setReturnValue($value);
break;
case 'grayFallback':
$this->setGrayFallback($value);
break;
case 'compact':
$this->setCompact($value);
break;
}
}
$this->setStyles($config['styles'] ?? []);
}
/**
* Enable or disable compact output.
*/
public function setCompact(bool $compact)
{
$this->compact = $compact;
}
/**
* Get whether to use compact output.
*/
public function compact(): bool
{
return $this->compact;
}
/**
* Set the prompt string.
*/
public function setPrompt(string $prompt)
{
$this->prompt = $prompt;
}
/**
* Get the prompt string.
*/
public function prompt(): string
{
return $this->prompt;
}
/**
* Set the buffer prompt string (used for multi-line input continuation).
*/
public function setBufferPrompt(string $bufferPrompt)
{
$this->bufferPrompt = $bufferPrompt;
}
/**
* Get the buffer prompt string (used for multi-line input continuation).
*/
public function bufferPrompt(): string
{
return $this->bufferPrompt;
}
/**
* Set the prompt string used when replaying history.
*/
public function setReplayPrompt(string $replayPrompt)
{
$this->replayPrompt = $replayPrompt;
}
/**
* Get the prompt string used when replaying history.
*/
public function replayPrompt(): string
{
return $this->replayPrompt;
}
/**
* Set the return value marker.
*/
public function setReturnValue(string $returnValue)
{
$this->returnValue = $returnValue;
}
/**
* Get the return value marker.
*/
public function returnValue(): string
{
return $this->returnValue;
}
/**
* Set the fallback color when "gray" is unavailable.
*/
public function setGrayFallback(string $grayFallback)
{
$this->grayFallback = $grayFallback;
}
/**
* Set the shell output formatter styles.
*
* Accepts a map from style name to [fg, bg, options], for example:
*
* [
* 'error' => ['white', 'red', ['bold']],
* 'warning' => ['black', 'yellow'],
* ]
*
* Foreground, background or options can be null, or even omitted entirely.
*/
public function setStyles(array $styles)
{
foreach (\array_keys(static::DEFAULT_STYLES) as $name) {
$this->styles[$name] = $styles[$name] ?? static::DEFAULT_STYLES[$name];
}
}
/**
* Apply the current output formatter styles.
*/
public function applyStyles(OutputFormatterInterface $formatter, bool $useGrayFallback)
{
foreach (\array_keys(static::DEFAULT_STYLES) as $name) {
$formatter->setStyle($name, new OutputFormatterStyle(...$this->getStyle($name, $useGrayFallback)));
}
}
/**
* Apply the current output formatter error styles.
*/
public function applyErrorStyles(OutputFormatterInterface $errorFormatter, bool $useGrayFallback)
{
foreach (static::ERROR_STYLES as $name) {
$errorFormatter->setStyle($name, new OutputFormatterStyle(...$this->getStyle($name, $useGrayFallback)));
}
}
private function getStyle(string $name, bool $useGrayFallback): array
{
return \array_map(function ($style) use ($useGrayFallback) {
return ($useGrayFallback && $style === 'gray') ? $this->grayFallback : $style;
}, $this->styles[$name]);
}
}