4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
14 use Psy\CodeCleaner\NoReturnValue;
15 use Psy\Exception\BreakException;
16 use Psy\Exception\ErrorException;
17 use Psy\Exception\Exception as PsyException;
18 use Psy\Exception\ThrowUpException;
19 use Psy\Exception\TypeErrorException;
20 use Psy\ExecutionLoop\ProcessForker;
21 use Psy\ExecutionLoop\RunkitReloader;
22 use Psy\Input\ShellInput;
23 use Psy\Input\SilentInput;
24 use Psy\Output\ShellOutput;
25 use Psy\TabCompletion\Matcher;
26 use Psy\VarDumper\PresenterAware;
27 use Symfony\Component\Console\Application;
28 use Symfony\Component\Console\Command\Command as BaseCommand;
29 use Symfony\Component\Console\Formatter\OutputFormatter;
30 use Symfony\Component\Console\Input\ArgvInput;
31 use Symfony\Component\Console\Input\InputArgument;
32 use Symfony\Component\Console\Input\InputDefinition;
33 use Symfony\Component\Console\Input\InputInterface;
34 use Symfony\Component\Console\Input\InputOption;
35 use Symfony\Component\Console\Input\StringInput;
36 use Symfony\Component\Console\Output\OutputInterface;
39 * The Psy Shell application.
46 * @author Justin Hileman <justin@justinhileman.info>
48 class Shell extends Application
50 const VERSION = 'v0.9.9';
52 const PROMPT = '>>> ';
53 const BUFF_PROMPT = '... ';
54 const REPLAY = '--> ';
64 private $codeBufferOpen;
66 private $stdoutBuffer;
70 private $outputWantsNewline = false;
72 private $loopListeners;
73 private $autoCompleter;
74 private $matchers = [];
75 private $commandsMatcher;
76 private $lastExecSuccess = true;
79 * Create a new Psy Shell.
81 * @param Configuration $config (default: null)
83 public function __construct(Configuration $config = null)
85 $this->config = $config ?: new Configuration();
86 $this->cleaner = $this->config->getCodeCleaner();
87 $this->loop = new ExecutionLoop();
88 $this->context = new Context();
90 $this->readline = $this->config->getReadline();
91 $this->inputBuffer = [];
92 $this->codeStack = [];
93 $this->stdoutBuffer = '';
94 $this->loopListeners = $this->getDefaultLoopListeners();
96 parent::__construct('Psy Shell', self::VERSION);
98 $this->config->setShell($this);
100 // Register the current shell session's config with \Psy\info
101 \Psy\info($this->config);
105 * Check whether the first thing in a backtrace is an include call.
107 * This is used by the psysh bin to decide whether to start a shell on boot,
108 * or to simply autoload the library.
110 public static function isIncluded(array $trace)
112 return isset($trace[0]['function']) &&
113 \in_array($trace[0]['function'], ['require', 'include', 'require_once', 'include_once']);
117 * Invoke a Psy Shell from the current context.
120 * @deprecated will be removed in 1.0. Use \Psy\debug instead
122 * @param array $vars Scope variables from the calling context (default: array())
123 * @param object|string $bindTo Bound object ($this) or class (self) value for the shell
125 * @return array Scope variables from the debugger session
127 public static function debug(array $vars = [], $bindTo = null)
129 return \Psy\debug($vars, $bindTo);
133 * Adds a command object.
137 * @param BaseCommand $command A Symfony Console Command object
139 * @return BaseCommand The registered command
141 public function add(BaseCommand $command)
143 if ($ret = parent::add($command)) {
144 if ($ret instanceof ContextAware) {
145 $ret->setContext($this->context);
148 if ($ret instanceof PresenterAware) {
149 $ret->setPresenter($this->config->getPresenter());
152 if (isset($this->commandsMatcher)) {
153 $this->commandsMatcher->setCommands($this->all());
161 * Gets the default input definition.
163 * @return InputDefinition An InputDefinition instance
165 protected function getDefaultInputDefinition()
167 return new InputDefinition([
168 new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
169 new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message.'),
174 * Gets the default commands that should always be available.
176 * @return array An array of default Command instances
178 protected function getDefaultCommands()
180 $sudo = new Command\SudoCommand();
181 $sudo->setReadline($this->readline);
183 $hist = new Command\HistoryCommand();
184 $hist->setReadline($this->readline);
187 new Command\HelpCommand(),
188 new Command\ListCommand(),
189 new Command\DumpCommand(),
190 new Command\DocCommand(),
191 new Command\ShowCommand($this->config->colorMode()),
192 new Command\WtfCommand($this->config->colorMode()),
193 new Command\WhereamiCommand($this->config->colorMode()),
194 new Command\ThrowUpCommand(),
195 new Command\TimeitCommand(),
196 new Command\TraceCommand(),
197 new Command\BufferCommand(),
198 new Command\ClearCommand(),
199 new Command\EditCommand($this->config->getRuntimeDir()),
200 // new Command\PsyVersionCommand(),
203 new Command\ExitCommand(),
210 protected function getDefaultMatchers()
212 // Store the Commands Matcher for later. If more commands are added,
213 // we'll update the Commands Matcher too.
214 $this->commandsMatcher = new Matcher\CommandsMatcher($this->all());
217 $this->commandsMatcher,
218 new Matcher\KeywordsMatcher(),
219 new Matcher\VariablesMatcher(),
220 new Matcher\ConstantsMatcher(),
221 new Matcher\FunctionsMatcher(),
222 new Matcher\ClassNamesMatcher(),
223 new Matcher\ClassMethodsMatcher(),
224 new Matcher\ClassAttributesMatcher(),
225 new Matcher\ObjectMethodsMatcher(),
226 new Matcher\ObjectAttributesMatcher(),
227 new Matcher\ClassMethodDefaultParametersMatcher(),
228 new Matcher\ObjectMethodDefaultParametersMatcher(),
229 new Matcher\FunctionDefaultParametersMatcher(),
234 * @deprecated Nothing should use this anymore
236 protected function getTabCompletionMatchers()
238 @\trigger_error('getTabCompletionMatchers is no longer used', E_USER_DEPRECATED);
242 * Gets the default command loop listeners.
244 * @return array An array of Execution Loop Listener instances
246 protected function getDefaultLoopListeners()
250 if (ProcessForker::isSupported() && $this->config->usePcntl()) {
251 $listeners[] = new ProcessForker();
254 if (RunkitReloader::isSupported()) {
255 $listeners[] = new RunkitReloader();
262 * Add tab completion matchers.
264 * @param array $matchers
266 public function addMatchers(array $matchers)
268 $this->matchers = \array_merge($this->matchers, $matchers);
270 if (isset($this->autoCompleter)) {
271 $this->addMatchersToAutoCompleter($matchers);
276 * @deprecated Call `addMatchers` instead
278 * @param array $matchers
280 public function addTabCompletionMatchers(array $matchers)
282 $this->addMatchers($matchers);
286 * Set the Shell output.
288 * @param OutputInterface $output
290 public function setOutput(OutputInterface $output)
292 $this->output = $output;
296 * Runs the current application.
298 * @param InputInterface $input An Input instance
299 * @param OutputInterface $output An Output instance
301 * @return int 0 if everything went fine, or an error code
303 public function run(InputInterface $input = null, OutputInterface $output = null)
305 $this->initializeTabCompletion();
307 if ($input === null && !isset($_SERVER['argv'])) {
308 $input = new ArgvInput([]);
311 if ($output === null) {
312 $output = $this->config->getOutput();
316 return parent::run($input, $output);
317 } catch (\Exception $e) {
318 $this->writeException($e);
325 * Runs the current application.
327 * @throws Exception if thrown via the `throw-up` command
329 * @param InputInterface $input An Input instance
330 * @param OutputInterface $output An Output instance
332 * @return int 0 if everything went fine, or an error code
334 public function doRun(InputInterface $input, OutputInterface $output)
336 $this->setOutput($output);
338 $this->resetCodeBuffer();
340 $this->setAutoExit(false);
341 $this->setCatchExceptions(false);
343 $this->readline->readHistory();
345 $this->output->writeln($this->getHeader());
346 $this->writeVersionInfo();
347 $this->writeStartupMessage();
351 $this->loop->run($this);
353 } catch (ThrowUpException $e) {
354 throw $e->getPrevious();
355 } catch (BreakException $e) {
356 // The ProcessForker throws a BreakException to finish the main thread.
364 * This will continue fetching user input until the code buffer contains
367 * @throws BreakException if user hits Ctrl+D
369 public function getInput()
371 $this->codeBufferOpen = false;
374 // reset output verbosity (in case it was altered by a subcommand)
375 $this->output->setVerbosity(ShellOutput::VERBOSITY_VERBOSE);
377 $input = $this->readline();
380 * Handle Ctrl+D. It behaves differently in different cases:
382 * 1) In an expression, like a function or "if" block, clear the input buffer
383 * 2) At top-level session, behave like the exit command
385 if ($input === false) {
386 $this->output->writeln('');
388 if ($this->hasCode()) {
389 $this->resetCodeBuffer();
391 throw new BreakException('Ctrl+D');
395 // handle empty input
396 if (\trim($input) === '' && !$this->codeBufferOpen) {
400 $input = $this->onInput($input);
402 // If the input isn't in an open string or comment, check for commands to run.
403 if ($this->hasCommand($input) && !$this->inputInOpenStringOrComment($input)) {
404 $this->addHistory($input);
405 $this->runCommand($input);
410 $this->addCode($input);
411 } while (!$this->hasValidCode());
415 * Check whether the code buffer (plus current input) is in an open string or comment.
417 * @param string $input current line of input
419 * @return bool true if the input is in an open string or comment
421 private function inputInOpenStringOrComment($input)
423 if (!$this->hasCode()) {
427 $code = $this->codeBuffer;
428 \array_push($code, $input);
429 $tokens = @\token_get_all('<?php ' . \implode("\n", $code));
430 $last = \array_pop($tokens);
432 return $last === '"' || $last === '`' ||
433 (\is_array($last) && \in_array($last[0], [T_ENCAPSED_AND_WHITESPACE, T_START_HEREDOC, T_COMMENT]));
437 * Run execution loop listeners before the shell session.
439 protected function beforeRun()
441 foreach ($this->loopListeners as $listener) {
442 $listener->beforeRun($this);
447 * Run execution loop listeners at the start of each loop.
449 public function beforeLoop()
451 foreach ($this->loopListeners as $listener) {
452 $listener->beforeLoop($this);
457 * Run execution loop listeners on user input.
459 * @param string $input
463 public function onInput($input)
465 foreach ($this->loopListeners as $listeners) {
466 if (($return = $listeners->onInput($this, $input)) !== null) {
475 * Run execution loop listeners on code to be executed.
477 * @param string $code
481 public function onExecute($code)
483 foreach ($this->loopListeners as $listener) {
484 if (($return = $listener->onExecute($this, $code)) !== null) {
493 * Run execution loop listeners after each loop.
495 public function afterLoop()
497 foreach ($this->loopListeners as $listener) {
498 $listener->afterLoop($this);
503 * Run execution loop listers after the shell session.
505 protected function afterRun()
507 foreach ($this->loopListeners as $listener) {
508 $listener->afterRun($this);
513 * Set the variables currently in scope.
517 public function setScopeVariables(array $vars)
519 $this->context->setAll($vars);
523 * Return the set of variables currently in scope.
525 * @param bool $includeBoundObject Pass false to exclude 'this'. If you're
526 * passing the scope variables to `extract`
527 * in PHP 7.1+, you _must_ exclude 'this'
529 * @return array Associative array of scope variables
531 public function getScopeVariables($includeBoundObject = true)
533 $vars = $this->context->getAll();
535 if (!$includeBoundObject) {
536 unset($vars['this']);
543 * Return the set of magic variables currently in scope.
545 * @param bool $includeBoundObject Pass false to exclude 'this'. If you're
546 * passing the scope variables to `extract`
547 * in PHP 7.1+, you _must_ exclude 'this'
549 * @return array Associative array of magic scope variables
551 public function getSpecialScopeVariables($includeBoundObject = true)
553 $vars = $this->context->getSpecialVariables();
555 if (!$includeBoundObject) {
556 unset($vars['this']);
563 * Return the set of variables currently in scope which differ from the
564 * values passed as $currentVars.
566 * This is used inside the Execution Loop Closure to pick up scope variable
567 * changes made by commands while the loop is running.
569 * @param array $currentVars
571 * @return array Associative array of scope variables which differ from $currentVars
573 public function getScopeVariablesDiff(array $currentVars)
577 foreach ($this->getScopeVariables(false) as $key => $value) {
578 if (!array_key_exists($key, $currentVars) || $currentVars[$key] !== $value) {
579 $newVars[$key] = $value;
587 * Get the set of unused command-scope variable names.
589 * @return array Array of unused variable names
591 public function getUnusedCommandScopeVariableNames()
593 return $this->context->getUnusedCommandScopeVariableNames();
597 * Get the set of variable names currently in scope.
599 * @return array Array of variable names
601 public function getScopeVariableNames()
603 return \array_keys($this->context->getAll());
607 * Get a scope variable value by name.
609 * @param string $name
613 public function getScopeVariable($name)
615 return $this->context->get($name);
619 * Set the bound object ($this variable) for the interactive shell.
621 * @param object|null $boundObject
623 public function setBoundObject($boundObject)
625 $this->context->setBoundObject($boundObject);
629 * Get the bound object ($this variable) for the interactive shell.
631 * @return object|null
633 public function getBoundObject()
635 return $this->context->getBoundObject();
639 * Set the bound class (self) for the interactive shell.
641 * @param string|null $boundClass
643 public function setBoundClass($boundClass)
645 $this->context->setBoundClass($boundClass);
649 * Get the bound class (self) for the interactive shell.
651 * @return string|null
653 public function getBoundClass()
655 return $this->context->getBoundClass();
659 * Add includes, to be parsed and executed before running the interactive shell.
661 * @param array $includes
663 public function setIncludes(array $includes = [])
665 $this->includes = $includes;
669 * Get PHP files to be parsed and executed before running the interactive shell.
673 public function getIncludes()
675 return \array_merge($this->config->getDefaultIncludes(), $this->includes);
679 * Check whether this shell's code buffer contains code.
681 * @return bool True if the code buffer contains code
683 public function hasCode()
685 return !empty($this->codeBuffer);
689 * Check whether the code in this shell's code buffer is valid.
691 * If the code is valid, the code buffer should be flushed and evaluated.
693 * @return bool True if the code buffer content is valid
695 protected function hasValidCode()
697 return !$this->codeBufferOpen && $this->code !== false;
701 * Add code to the code buffer.
703 * @param string $code
704 * @param bool $silent
706 public function addCode($code, $silent = false)
709 // Code lines ending in \ keep the buffer open
710 if (\substr(\rtrim($code), -1) === '\\') {
711 $this->codeBufferOpen = true;
712 $code = \substr(\rtrim($code), 0, -1);
714 $this->codeBufferOpen = false;
717 $this->codeBuffer[] = $silent ? new SilentInput($code) : $code;
718 $this->code = $this->cleaner->clean($this->codeBuffer, $this->config->requireSemicolons());
719 } catch (\Exception $e) {
720 // Add failed code blocks to the readline history.
721 $this->addCodeBufferToHistory();
728 * Set the code buffer.
730 * This is mostly used by `Shell::execute`. Any existing code in the input
731 * buffer is pushed onto a stack and will come back after this new code is
734 * @throws \InvalidArgumentException if $code isn't a complete statement
736 * @param string $code
737 * @param bool $silent
739 private function setCode($code, $silent = false)
741 if ($this->hasCode()) {
742 $this->codeStack[] = [$this->codeBuffer, $this->codeBufferOpen, $this->code];
745 $this->resetCodeBuffer();
747 $this->addCode($code, $silent);
748 } catch (\Throwable $e) {
749 $this->popCodeStack();
752 } catch (\Exception $e) {
753 $this->popCodeStack();
758 if (!$this->hasValidCode()) {
759 $this->popCodeStack();
761 throw new \InvalidArgumentException('Unexpected end of input');
766 * Get the current code buffer.
768 * This is useful for commands which manipulate the buffer.
772 public function getCodeBuffer()
774 return $this->codeBuffer;
778 * Run a Psy Shell command given the user input.
780 * @throws InvalidArgumentException if the input is not a valid command
782 * @param string $input User input string
784 * @return mixed Who knows?
786 protected function runCommand($input)
788 $command = $this->getCommand($input);
790 if (empty($command)) {
791 throw new \InvalidArgumentException('Command not found: ' . $input);
794 $input = new ShellInput(\str_replace('\\', '\\\\', \rtrim($input, " \t\n\r\0\x0B;")));
796 if ($input->hasParameterOption(['--help', '-h'])) {
797 $helpCommand = $this->get('help');
798 $helpCommand->setCommand($command);
800 return $helpCommand->run($input, $this->output);
803 return $command->run($input, $this->output);
807 * Reset the current code buffer.
809 * This should be run after evaluating user input, catching exceptions, or
810 * on demand by commands such as BufferCommand.
812 public function resetCodeBuffer()
814 $this->codeBuffer = [];
819 * Inject input into the input buffer.
821 * This is useful for commands which want to replay history.
823 * @param string|array $input
824 * @param bool $silent
826 public function addInput($input, $silent = false)
828 foreach ((array) $input as $line) {
829 $this->inputBuffer[] = $silent ? new SilentInput($line) : $line;
834 * Flush the current (valid) code buffer.
836 * If the code buffer is valid, resets the code buffer and returns the
839 * @return string PHP code buffer contents
841 public function flushCode()
843 if ($this->hasValidCode()) {
844 $this->addCodeBufferToHistory();
846 $this->popCodeStack();
853 * Reset the code buffer and restore any code pushed during `execute` calls.
855 private function popCodeStack()
857 $this->resetCodeBuffer();
859 if (empty($this->codeStack)) {
863 list($codeBuffer, $codeBufferOpen, $code) = \array_pop($this->codeStack);
865 $this->codeBuffer = $codeBuffer;
866 $this->codeBufferOpen = $codeBufferOpen;
871 * (Possibly) add a line to the readline history.
873 * Like Bash, if the line starts with a space character, it will be omitted
874 * from history. Note that an entire block multi-line code input will be
875 * omitted iff the first line begins with a space.
877 * Additionally, if a line is "silent", i.e. it was initially added with the
878 * silent flag, it will also be omitted.
880 * @param string|SilentInput $line
882 private function addHistory($line)
884 if ($line instanceof SilentInput) {
888 // Skip empty lines and lines starting with a space
889 if (\trim($line) !== '' && \substr($line, 0, 1) !== ' ') {
890 $this->readline->addHistory($line);
895 * Filter silent input from code buffer, write the rest to readline history.
897 private function addCodeBufferToHistory()
899 $codeBuffer = \array_filter($this->codeBuffer, function ($line) {
900 return !$line instanceof SilentInput;
903 $this->addHistory(\implode("\n", $codeBuffer));
907 * Get the current evaluation scope namespace.
909 * @see CodeCleaner::getNamespace
911 * @return string Current code namespace
913 public function getNamespace()
915 if ($namespace = $this->cleaner->getNamespace()) {
916 return \implode('\\', $namespace);
921 * Write a string to stdout.
923 * This is used by the shell loop for rendering output from evaluated code.
926 * @param int $phase Output buffering phase
928 public function writeStdout($out, $phase = PHP_OUTPUT_HANDLER_END)
930 $isCleaning = $phase & PHP_OUTPUT_HANDLER_CLEAN;
933 if ($out !== '' && !$isCleaning) {
934 $this->output->write($out, false, ShellOutput::OUTPUT_RAW);
935 $this->outputWantsNewline = (\substr($out, -1) !== "\n");
936 $this->stdoutBuffer .= $out;
939 // Output buffering is done!
940 if ($phase & PHP_OUTPUT_HANDLER_END) {
941 // Write an extra newline if stdout didn't end with one
942 if ($this->outputWantsNewline) {
943 $this->output->writeln(\sprintf('<aside>%s</aside>', $this->config->useUnicode() ? '⏎' : '\\n'));
944 $this->outputWantsNewline = false;
947 // Save the stdout buffer as $__out
948 if ($this->stdoutBuffer !== '') {
949 $this->context->setLastStdout($this->stdoutBuffer);
950 $this->stdoutBuffer = '';
956 * Write a return value to stdout.
958 * The return value is formatted or pretty-printed, and rendered in a
959 * visibly distinct manner (in this case, as cyan).
961 * @see self::presentValue
965 public function writeReturnValue($ret)
967 $this->lastExecSuccess = true;
969 if ($ret instanceof NoReturnValue) {
973 $this->context->setReturnValue($ret);
974 $ret = $this->presentValue($ret);
975 $indent = \str_repeat(' ', \strlen(static::RETVAL));
977 $this->output->writeln(static::RETVAL . \str_replace(PHP_EOL, PHP_EOL . $indent, $ret));
981 * Renders a caught Exception.
983 * Exceptions are formatted according to severity. ErrorExceptions which were
984 * warnings or Strict errors aren't rendered as harshly as real errors.
986 * Stores $e as the last Exception in the Shell Context.
988 * @param \Exception $e An exception instance
990 public function writeException(\Exception $e)
992 $this->lastExecSuccess = false;
993 $this->context->setLastException($e);
994 $this->output->writeln($this->formatException($e));
995 $this->resetCodeBuffer();
999 * Check whether the last exec was successful.
1001 * Returns true if a return value was logged rather than an exception.
1005 public function getLastExecSuccess()
1007 return $this->lastExecSuccess;
1011 * Helper for formatting an exception for writeException().
1013 * @todo extract this to somewhere it makes more sense
1015 * @param \Exception $e
1019 public function formatException(\Exception $e)
1021 $message = $e->getMessage();
1022 if (!$e instanceof PsyException) {
1023 if ($message === '') {
1024 $message = \get_class($e);
1026 $message = \sprintf('%s with message \'%s\'', \get_class($e), $message);
1030 $message = \preg_replace(
1031 "#(\\w:)?(/\\w+)*/src/Execution(?:Loop)?Closure.php\(\d+\) : eval\(\)'d code#",
1033 \str_replace('\\', '/', $message)
1036 $message = \str_replace(" in eval()'d code", ' in Psy Shell code', $message);
1038 $severity = ($e instanceof \ErrorException) ? $this->getSeverity($e) : 'error';
1040 return \sprintf('<%s>%s</%s>', $severity, OutputFormatter::escape($message), $severity);
1044 * Helper for getting an output style for the given ErrorException's level.
1046 * @param \ErrorException $e
1050 protected function getSeverity(\ErrorException $e)
1052 $severity = $e->getSeverity();
1053 if ($severity & \error_reporting()) {
1054 switch ($severity) {
1057 case E_CORE_WARNING:
1058 case E_COMPILE_WARNING:
1059 case E_USER_WARNING:
1068 // Since this is below the user's reporting threshold, it's always going to be a warning.
1074 * Execute code in the shell execution context.
1076 * @param string $code
1077 * @param bool $throwExceptions
1081 public function execute($code, $throwExceptions = false)
1083 $this->setCode($code, true);
1084 $closure = new ExecutionClosure($this);
1086 if ($throwExceptions) {
1087 return $closure->execute();
1091 return $closure->execute();
1092 } catch (\TypeError $_e) {
1093 $this->writeException(TypeErrorException::fromTypeError($_e));
1094 } catch (\Error $_e) {
1095 $this->writeException(ErrorException::fromError($_e));
1096 } catch (\Exception $_e) {
1097 $this->writeException($_e);
1102 * Helper for throwing an ErrorException.
1104 * This allows us to:
1106 * set_error_handler(array($psysh, 'handleError'));
1108 * Unlike ErrorException::throwException, this error handler respects the
1109 * current error_reporting level; i.e. it logs warnings and notices, but
1110 * doesn't throw an exception unless it's above the current error_reporting
1111 * threshold. This should probably only be used in the inner execution loop
1112 * of the shell, as most of the time a thrown exception is much more useful.
1114 * If the error type matches the `errorLoggingLevel` config, it will be
1115 * logged as well, regardless of the `error_reporting` level.
1117 * @see \Psy\Exception\ErrorException::throwException
1118 * @see \Psy\Shell::writeException
1120 * @throws \Psy\Exception\ErrorException depending on the current error_reporting level
1122 * @param int $errno Error type
1123 * @param string $errstr Message
1124 * @param string $errfile Filename
1125 * @param int $errline Line number
1127 public function handleError($errno, $errstr, $errfile, $errline)
1129 if ($errno & \error_reporting()) {
1130 ErrorException::throwException($errno, $errstr, $errfile, $errline);
1131 } elseif ($errno & $this->config->errorLoggingLevel()) {
1132 // log it and continue...
1133 $this->writeException(new ErrorException($errstr, 0, $errno, $errfile, $errline));
1138 * Format a value for display.
1140 * @see Presenter::present
1144 * @return string Formatted value
1146 protected function presentValue($val)
1148 return $this->config->getPresenter()->present($val);
1152 * Get a command (if one exists) for the current input string.
1154 * @param string $input
1156 * @return null|BaseCommand
1158 protected function getCommand($input)
1160 $input = new StringInput($input);
1161 if ($name = $input->getFirstArgument()) {
1162 return $this->get($name);
1167 * Check whether a command is set for the current input string.
1169 * @param string $input
1171 * @return bool True if the shell has a command for the given input
1173 protected function hasCommand($input)
1175 if (\preg_match('/([^\s]+?)(?:\s|$)/A', \ltrim($input), $match)) {
1176 return $this->has($match[1]);
1183 * Get the current input prompt.
1187 protected function getPrompt()
1189 if ($this->hasCode()) {
1190 return static::BUFF_PROMPT;
1193 return $this->config->getPrompt() ?: static::PROMPT;
1197 * Read a line of user input.
1199 * This will return a line from the input buffer (if any exist). Otherwise,
1200 * it will ask the user for input.
1202 * If readline is enabled, this delegates to readline. Otherwise, it's an
1203 * ugly `fgets` call.
1205 * @return string One line of user input
1207 protected function readline()
1209 if (!empty($this->inputBuffer)) {
1210 $line = \array_shift($this->inputBuffer);
1211 if (!$line instanceof SilentInput) {
1212 $this->output->writeln(\sprintf('<aside>%s %s</aside>', static::REPLAY, OutputFormatter::escape($line)));
1218 if ($bracketedPaste = $this->config->useBracketedPaste()) {
1219 \printf("\e[?2004h"); // Enable bracketed paste
1222 $line = $this->readline->readline($this->getPrompt());
1224 if ($bracketedPaste) {
1225 \printf("\e[?2004l"); // ... and disable it again
1232 * Get the shell output header.
1236 protected function getHeader()
1238 return \sprintf('<aside>%s by Justin Hileman</aside>', $this->getVersion());
1242 * Get the current version of Psy Shell.
1246 public function getVersion()
1248 $separator = $this->config->useUnicode() ? '—' : '-';
1250 return \sprintf('Psy Shell %s (PHP %s %s %s)', self::VERSION, PHP_VERSION, $separator, PHP_SAPI);
1254 * Get a PHP manual database instance.
1258 public function getManualDb()
1260 return $this->config->getManualDb();
1264 * @deprecated Tab completion is provided by the AutoCompleter service
1266 protected function autocomplete($text)
1268 @\trigger_error('Tab completion is provided by the AutoCompleter service', E_USER_DEPRECATED);
1272 * Initialize tab completion matchers.
1274 * If tab completion is enabled this adds tab completion matchers to the
1275 * auto completer and sets context if needed.
1277 protected function initializeTabCompletion()
1279 if (!$this->config->useTabCompletion()) {
1283 $this->autoCompleter = $this->config->getAutoCompleter();
1285 // auto completer needs shell to be linked to configuration because of
1286 // the context aware matchers
1287 $this->addMatchersToAutoCompleter($this->getDefaultMatchers());
1288 $this->addMatchersToAutoCompleter($this->matchers);
1290 $this->autoCompleter->activate();
1294 * Add matchers to the auto completer, setting context if needed.
1296 * @param array $matchers
1298 private function addMatchersToAutoCompleter(array $matchers)
1300 foreach ($matchers as $matcher) {
1301 if ($matcher instanceof ContextAware) {
1302 $matcher->setContext($this->context);
1304 $this->autoCompleter->addMatcher($matcher);
1309 * @todo Implement self-update
1310 * @todo Implement prompt to start update
1312 * @return void|string
1314 protected function writeVersionInfo()
1316 if (PHP_SAPI !== 'cli') {
1321 $client = $this->config->getChecker();
1322 if (!$client->isLatest()) {
1323 $this->output->writeln(\sprintf('New version is available (current: %s, latest: %s)', self::VERSION, $client->getLatest()));
1325 } catch (\InvalidArgumentException $e) {
1326 $this->output->writeln($e->getMessage());
1331 * Write a startup message if set.
1333 protected function writeStartupMessage()
1335 $message = $this->config->getStartupMessage();
1336 if ($message !== null && $message !== '') {
1337 $this->output->writeln($message);