Pressroom template verwijderd, website naar root van repo

This commit is contained in:
2020-03-22 15:30:52 +01:00
parent 2cb6a77425
commit f3d1c41e91
7620 changed files with 0 additions and 186900 deletions

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Input;
use Symfony\Component\Console\Input\InputArgument;
/**
* An input argument for code.
*
* A CodeArgument must be the final argument of the command. Once all options
* and other arguments are used, any remaining input until the end of the string
* is considered part of a single CodeArgument, regardless of spaces, quoting,
* escaping, etc.
*
* This means commands can support crazy input like
*
* parse function() { return "wheee\n"; }
*
* ... without having to put the code in a quoted string and escape everything.
*/
class CodeArgument extends InputArgument
{
/**
* Constructor.
*
* @param string $name The argument name
* @param int $mode The argument mode: self::REQUIRED or self::OPTIONAL
* @param string $description A description text
* @param mixed $default The default value (for self::OPTIONAL mode only)
*
* @throws \InvalidArgumentException When argument mode is not valid
*/
public function __construct($name, $mode = null, $description = '', $default = null)
{
if ($mode & InputArgument::IS_ARRAY) {
throw new \InvalidArgumentException('Argument mode IS_ARRAY is not valid.');
}
parent::__construct($name, $mode, $description, $default);
}
}

View File

@@ -0,0 +1,143 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Input;
use Psy\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
/**
* Parse, validate and match --grep, --insensitive and --invert command options.
*/
class FilterOptions
{
private $filter = false;
private $pattern;
private $insensitive;
private $invert;
/**
* Get input option definitions for filtering.
*
* @return InputOption[]
*/
public static function getOptions()
{
return array(
new InputOption('grep', 'G', InputOption::VALUE_REQUIRED, 'Limit to items matching the given pattern (string or regex).'),
new InputOption('insensitive', 'i', InputOption::VALUE_NONE, 'Case-insensitive search (requires --grep).'),
new InputOption('invert', 'v', InputOption::VALUE_NONE, 'Inverted search (requires --grep).'),
);
}
/**
* Bind input and prepare filter.
*
* @param InputInterface $input
*/
public function bind(InputInterface $input)
{
$this->validateInput($input);
if (!$pattern = $input->getOption('grep')) {
$this->filter = false;
return;
}
if (!$this->stringIsRegex($pattern)) {
$pattern = '/' . preg_quote($pattern, '/') . '/';
}
if ($insensitive = $input->getOption('insensitive')) {
$pattern .= 'i';
}
$this->validateRegex($pattern);
$this->filter = true;
$this->pattern = $pattern;
$this->insensitive = $insensitive;
$this->invert = $input->getOption('invert');
}
/**
* Check whether the bound input has filter options.
*
* @return bool
*/
public function hasFilter()
{
return $this->filter;
}
/**
* Check whether a string matches the current filter options.
*
* @param string $string
* @param array $matches
*
* @return bool
*/
public function match($string, array &$matches = null)
{
return $this->filter === false || (preg_match($this->pattern, $string, $matches) xor $this->invert);
}
/**
* Validate that grep, invert and insensitive input options are consistent.
*
* @param InputInterface $input
*
* @return bool
*/
private function validateInput(InputInterface $input)
{
if (!$input->getOption('grep')) {
foreach (array('invert', 'insensitive') as $option) {
if ($input->getOption($option)) {
throw new RuntimeException('--' . $option . ' does not make sense without --grep');
}
}
}
}
/**
* Check whether a string appears to be a regular expression.
*
* @param string $string
*
* @return bool
*/
private function stringIsRegex($string)
{
return substr($string, 0, 1) === '/' && substr($string, -1) === '/' && strlen($string) >= 3;
}
/**
* Validate that $pattern is a valid regular expression.
*
* @param string $pattern
*
* @return bool
*/
private function validateRegex($pattern)
{
set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
try {
preg_match($pattern, '');
} catch (ErrorException $e) {
throw new RuntimeException(str_replace('preg_match(): ', 'Invalid regular expression: ', $e->getRawMessage()));
}
restore_error_handler();
}
}

View File

@@ -0,0 +1,322 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Input;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\StringInput;
/**
* A StringInput subclass specialized for code arguments.
*/
class ShellInput extends StringInput
{
private $hasCodeArgument = false;
/**
* Unlike the parent implementation's tokens, this contains an array of
* token/rest pairs, so that code arguments can be handled while parsing.
*/
private $tokenPairs;
private $parsed;
/**
* Constructor.
*
* @param string $input An array of parameters from the CLI (in the argv format)
*/
public function __construct($input)
{
parent::__construct($input);
$this->tokenPairs = $this->tokenize($input);
}
/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException if $definition has CodeArgument before the final argument position
*/
public function bind(InputDefinition $definition)
{
$hasCodeArgument = false;
if ($definition->getArgumentCount() > 0) {
$args = $definition->getArguments();
$lastArg = array_pop($args);
foreach ($args as $arg) {
if ($arg instanceof CodeArgument) {
$msg = sprintf('Unexpected CodeArgument before the final position: %s', $arg->getName());
throw new \InvalidArgumentException($msg);
}
}
if ($lastArg instanceof CodeArgument) {
$hasCodeArgument = true;
}
}
$this->hasCodeArgument = $hasCodeArgument;
return parent::bind($definition);
}
/**
* Tokenizes a string.
*
* The version of this on StringInput is good, but doesn't handle code
* arguments if they're at all complicated. This does :)
*
* @param string $input The input to tokenize
*
* @return array An array of token/rest pairs
*
* @throws \InvalidArgumentException When unable to parse input (should never happen)
*/
private function tokenize($input)
{
$tokens = array();
$length = strlen($input);
$cursor = 0;
while ($cursor < $length) {
if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
} elseif (preg_match('/([^="\'\s]+?)(=?)(' . StringInput::REGEX_QUOTED_STRING . '+)/A', $input, $match, null, $cursor)) {
$tokens[] = array(
$match[1] . $match[2] . stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2))),
substr($input, $cursor),
);
} elseif (preg_match('/' . StringInput::REGEX_QUOTED_STRING . '/A', $input, $match, null, $cursor)) {
$tokens[] = array(
stripcslashes(substr($match[0], 1, strlen($match[0]) - 2)),
substr($input, $cursor),
);
} elseif (preg_match('/' . StringInput::REGEX_STRING . '/A', $input, $match, null, $cursor)) {
$tokens[] = array(
stripcslashes($match[1]),
substr($input, $cursor),
);
} else {
// should never happen
throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
}
$cursor += strlen($match[0]);
}
return $tokens;
}
/**
* Same as parent, but with some bonus handling for code arguments.
*/
protected function parse()
{
$parseOptions = true;
$this->parsed = $this->tokenPairs;
while (null !== $tokenPair = array_shift($this->parsed)) {
// token is what you'd expect. rest is the remainder of the input
// string, including token, and will be used if this is a code arg.
list($token, $rest) = $tokenPair;
if ($parseOptions && '' === $token) {
$this->parseShellArgument($token, $rest);
} elseif ($parseOptions && '--' === $token) {
$parseOptions = false;
} elseif ($parseOptions && 0 === strpos($token, '--')) {
$this->parseLongOption($token);
} elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
$this->parseShortOption($token);
} else {
$this->parseShellArgument($token, $rest);
}
}
}
/**
* Parses an argument, with bonus handling for code arguments.
*
* @param string $token The current token
* @param string $rest The remaining unparsed input, including the current token
*
* @throws \RuntimeException When too many arguments are given
*/
private function parseShellArgument($token, $rest)
{
$c = count($this->arguments);
// if input is expecting another argument, add it
if ($this->definition->hasArgument($c)) {
$arg = $this->definition->getArgument($c);
if ($arg instanceof CodeArgument) {
// When we find a code argument, we're done parsing. Add the
// remaining input to the current argument and call it a day.
$this->parsed = array();
$this->arguments[$arg->getName()] = $rest;
} else {
$this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
}
// if last argument isArray(), append token to last argument
} elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
$arg = $this->definition->getArgument($c - 1);
$this->arguments[$arg->getName()][] = $token;
// unexpected argument
} else {
$all = $this->definition->getArguments();
if (count($all)) {
throw new \RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
}
throw new \RuntimeException(sprintf('No arguments expected, got "%s".', $token));
}
}
// Everything below this is copypasta from ArgvInput private methods
/**
* Parses a short option.
*
* @param string $token The current token
*/
private function parseShortOption($token)
{
$name = substr($token, 1);
if (strlen($name) > 1) {
if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
// an option with a value (with no space)
$this->addShortOption($name[0], substr($name, 1));
} else {
$this->parseShortOptionSet($name);
}
} else {
$this->addShortOption($name, null);
}
}
/**
* Parses a short option set.
*
* @param string $name The current token
*
* @throws \RuntimeException When option given doesn't exist
*/
private function parseShortOptionSet($name)
{
$len = strlen($name);
for ($i = 0; $i < $len; ++$i) {
if (!$this->definition->hasShortcut($name[$i])) {
throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
}
$option = $this->definition->getOptionForShortcut($name[$i]);
if ($option->acceptValue()) {
$this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
break;
} else {
$this->addLongOption($option->getName(), null);
}
}
}
/**
* Parses a long option.
*
* @param string $token The current token
*/
private function parseLongOption($token)
{
$name = substr($token, 2);
if (false !== $pos = strpos($name, '=')) {
if (0 === strlen($value = substr($name, $pos + 1))) {
// if no value after "=" then substr() returns "" since php7 only, false before
// see http://php.net/manual/fr/migration70.incompatible.php#119151
if (PHP_VERSION_ID < 70000 && false === $value) {
$value = '';
}
array_unshift($this->parsed, array($value, null));
}
$this->addLongOption(substr($name, 0, $pos), $value);
} else {
$this->addLongOption($name, null);
}
}
/**
* Adds a short option value.
*
* @param string $shortcut The short option key
* @param mixed $value The value for the option
*
* @throws \RuntimeException When option given doesn't exist
*/
private function addShortOption($shortcut, $value)
{
if (!$this->definition->hasShortcut($shortcut)) {
throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
}
$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
}
/**
* Adds a long option value.
*
* @param string $name The long option key
* @param mixed $value The value for the option
*
* @throws \RuntimeException When option given doesn't exist
*/
private function addLongOption($name, $value)
{
if (!$this->definition->hasOption($name)) {
throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
}
$option = $this->definition->getOption($name);
if (null !== $value && !$option->acceptValue()) {
throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
}
if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
// if option accepts an optional or mandatory argument
// let's see if there is one provided
$next = array_shift($this->parsed);
$nextToken = $next[0];
if ((isset($nextToken[0]) && '-' !== $nextToken[0]) || in_array($nextToken, array('', null), true)) {
$value = $nextToken;
} else {
array_unshift($this->parsed, $next);
}
}
if (null === $value) {
if ($option->isValueRequired()) {
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
}
if (!$option->isArray() && !$option->isValueOptional()) {
$value = true;
}
}
if ($option->isArray()) {
$this->options[$name][] = $value;
} else {
$this->options[$name] = $value;
}
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Input;
/**
* A simple class used internally by PsySH to represent silent input.
*
* Silent input is generally used for non-user-generated code, such as the
* rewritten user code run by sudo command. Silent input isn't echoed before
* evaluating, and it's not added to the readline history.
*/
class SilentInput
{
private $inputString;
/**
* Constructor.
*
* @param string $inputString
*/
public function __construct($inputString)
{
$this->inputString = $inputString;
}
/**
* To. String.
*
* @return string
*/
public function __toString()
{
return $this->inputString;
}
}