X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FPsy%2FCommand%2FListCommand%2FEnumerator.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FPsy%2FCommand%2FListCommand%2FEnumerator.php;h=5e612cb086f65b32d5788f7822028e42ccf49f26;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/psy/psysh/src/Psy/Command/ListCommand/Enumerator.php b/vendor/psy/psysh/src/Psy/Command/ListCommand/Enumerator.php new file mode 100644 index 000000000..5e612cb08 --- /dev/null +++ b/vendor/psy/psysh/src/Psy/Command/ListCommand/Enumerator.php @@ -0,0 +1,146 @@ +presenter = $presenter; + } + + /** + * Return a list of categorized things with the given input options and target. + * + * @param InputInterface $input + * @param Reflector $reflector + * @param mixed $target + * + * @return array + */ + public function enumerate(InputInterface $input, \Reflector $reflector = null, $target = null) + { + $this->setFilter($input); + + return $this->listItems($input, $reflector, $target); + } + + /** + * Enumerate specific items with the given input options and target. + * + * Implementing classes should return an array of arrays: + * + * [ + * 'Constants' => [ + * 'FOO' => [ + * 'name' => 'FOO', + * 'style' => 'public', + * 'value' => '123', + * ], + * ], + * ] + * + * @param InputInterface $input + * @param Reflector $reflector + * @param mixed $target + * + * @return array + */ + abstract protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null); + + protected function presentRef($value) + { + return $this->presenter->presentRef($value); + } + + protected function showItem($name) + { + return $this->filter === false || (preg_match($this->pattern, $name) xor $this->invertFilter); + } + + private function setFilter(InputInterface $input) + { + if ($pattern = $input->getOption('grep')) { + if (substr($pattern, 0, 1) !== '/' || substr($pattern, -1) !== '/' || strlen($pattern) < 3) { + $pattern = '/' . preg_quote($pattern, '/') . '/'; + } + + if ($input->getOption('insensitive')) { + $pattern .= 'i'; + } + + $this->validateRegex($pattern); + + $this->filter = true; + $this->pattern = $pattern; + $this->invertFilter = $input->getOption('invert'); + } else { + $this->filter = false; + } + } + + /** + * 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(); + } + + protected function presentSignature($target) + { + // This might get weird if the signature is actually for a reflector. Hrm. + if (!$target instanceof \Reflector) { + $target = Mirror::get($target); + } + + return SignatureFormatter::format($target); + } +}