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.3';
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;
78 * Create a new Psy Shell.
80 * @param Configuration $config (default: null)
82 public function __construct(Configuration $config = null)
84 $this->config = $config ?: new Configuration();
85 $this->cleaner = $this->config->getCodeCleaner();
86 $this->loop = new ExecutionLoop();
87 $this->context = new Context();
89 $this->readline = $this->config->getReadline();
90 $this->inputBuffer = [];
91 $this->codeStack = [];
92 $this->stdoutBuffer = '';
93 $this->loopListeners = $this->getDefaultLoopListeners();
95 parent::__construct('Psy Shell', self::VERSION);
97 $this->config->setShell($this);
99 // Register the current shell session's config with \Psy\info
100 \Psy\info($this->config);
104 * Check whether the first thing in a backtrace is an include call.
106 * This is used by the psysh bin to decide whether to start a shell on boot,
107 * or to simply autoload the library.
109 public static function isIncluded(array $trace)
111 return isset($trace[0]['function']) &&
112 in_array($trace[0]['function'], ['require', 'include', 'require_once', 'include_once']);
116 * Invoke a Psy Shell from the current context.
119 * @deprecated will be removed in 1.0. Use \Psy\debug instead
121 * @param array $vars Scope variables from the calling context (default: array())
122 * @param object $boundObject Bound object ($this) value for the shell
124 * @return array Scope variables from the debugger session
126 public static function debug(array $vars = [], $boundObject = null)
128 return \Psy\debug($vars, $boundObject);
132 * Adds a command object.
136 * @param BaseCommand $command A Symfony Console Command object
138 * @return BaseCommand The registered command
140 public function add(BaseCommand $command)
142 if ($ret = parent::add($command)) {
143 if ($ret instanceof ContextAware) {
144 $ret->setContext($this->context);
147 if ($ret instanceof PresenterAware) {
148 $ret->setPresenter($this->config->getPresenter());
151 if (isset($this->commandsMatcher)) {
152 $this->commandsMatcher->setCommands($this->all());
160 * Gets the default input definition.
162 * @return InputDefinition An InputDefinition instance
164 protected function getDefaultInputDefinition()
166 return new InputDefinition([
167 new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
168 new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message.'),
173 * Gets the default commands that should always be available.
175 * @return array An array of default Command instances
177 protected function getDefaultCommands()
179 $sudo = new Command\SudoCommand();
180 $sudo->setReadline($this->readline);
182 $hist = new Command\HistoryCommand();
183 $hist->setReadline($this->readline);
186 new Command\HelpCommand(),
187 new Command\ListCommand(),
188 new Command\DumpCommand(),
189 new Command\DocCommand(),
190 new Command\ShowCommand($this->config->colorMode()),
191 new Command\WtfCommand($this->config->colorMode()),
192 new Command\WhereamiCommand($this->config->colorMode()),
193 new Command\ThrowUpCommand(),
194 new Command\TimeitCommand(),
195 new Command\TraceCommand(),
196 new Command\BufferCommand(),
197 new Command\ClearCommand(),
198 new Command\EditCommand($this->config->getRuntimeDir()),
199 // new Command\PsyVersionCommand(),
202 new Command\ExitCommand(),
209 protected function getDefaultMatchers()
211 // Store the Commands Matcher for later. If more commands are added,
212 // we'll update the Commands Matcher too.
213 $this->commandsMatcher = new Matcher\CommandsMatcher($this->all());
216 $this->commandsMatcher,
217 new Matcher\KeywordsMatcher(),
218 new Matcher\VariablesMatcher(),
219 new Matcher\ConstantsMatcher(),
220 new Matcher\FunctionsMatcher(),
221 new Matcher\ClassNamesMatcher(),
222 new Matcher\ClassMethodsMatcher(),
223 new Matcher\ClassAttributesMatcher(),
224 new Matcher\ObjectMethodsMatcher(),
225 new Matcher\ObjectAttributesMatcher(),
226 new Matcher\ClassMethodDefaultParametersMatcher(),
227 new Matcher\ObjectMethodDefaultParametersMatcher(),
228 new Matcher\FunctionDefaultParametersMatcher(),
233 * @deprecated Nothing should use this anymore
235 protected function getTabCompletionMatchers()
237 @trigger_error('getTabCompletionMatchers is no longer used', E_USER_DEPRECATED);
241 * Gets the default command loop listeners.
243 * @return array An array of Execution Loop Listener instances
245 protected function getDefaultLoopListeners()
249 if (ProcessForker::isSupported() && $this->config->usePcntl()) {
250 $listeners[] = new ProcessForker();
253 if (RunkitReloader::isSupported()) {
254 $listeners[] = new RunkitReloader();
261 * Add tab completion matchers.
263 * @param array $matchers
265 public function addMatchers(array $matchers)
267 $this->matchers = array_merge($this->matchers, $matchers);
269 if (isset($this->autoCompleter)) {
270 $this->addMatchersToAutoCompleter($matchers);
275 * @deprecated Call `addMatchers` instead
277 * @param array $matchers
279 public function addTabCompletionMatchers(array $matchers)
281 $this->addMatchers($matchers);
285 * Set the Shell output.
287 * @param OutputInterface $output
289 public function setOutput(OutputInterface $output)
291 $this->output = $output;
295 * Runs the current application.
297 * @param InputInterface $input An Input instance
298 * @param OutputInterface $output An Output instance
300 * @return int 0 if everything went fine, or an error code
302 public function run(InputInterface $input = null, OutputInterface $output = null)
304 $this->initializeTabCompletion();
306 if ($input === null && !isset($_SERVER['argv'])) {
307 $input = new ArgvInput([]);
310 if ($output === null) {
311 $output = $this->config->getOutput();
315 return parent::run($input, $output);
316 } catch (\Exception $e) {
317 $this->writeException($e);
324 * Runs the current application.
326 * @throws Exception if thrown via the `throw-up` command
328 * @param InputInterface $input An Input instance
329 * @param OutputInterface $output An Output instance
331 * @return int 0 if everything went fine, or an error code
333 public function doRun(InputInterface $input, OutputInterface $output)
335 $this->setOutput($output);
337 $this->resetCodeBuffer();
339 $this->setAutoExit(false);
340 $this->setCatchExceptions(false);
342 $this->readline->readHistory();
344 $this->output->writeln($this->getHeader());
345 $this->writeVersionInfo();
346 $this->writeStartupMessage();
350 $this->loop->run($this);
352 } catch (ThrowUpException $e) {
353 throw $e->getPrevious();
354 } catch (BreakException $e) {
355 // The ProcessForker throws a BreakException to finish the main thread.
363 * This will continue fetching user input until the code buffer contains
366 * @throws BreakException if user hits Ctrl+D
368 public function getInput()
370 $this->codeBufferOpen = false;
373 // reset output verbosity (in case it was altered by a subcommand)
374 $this->output->setVerbosity(ShellOutput::VERBOSITY_VERBOSE);
376 $input = $this->readline();
379 * Handle Ctrl+D. It behaves differently in different cases:
381 * 1) In an expression, like a function or "if" block, clear the input buffer
382 * 2) At top-level session, behave like the exit command
384 if ($input === false) {
385 $this->output->writeln('');
387 if ($this->hasCode()) {
388 $this->resetCodeBuffer();
390 throw new BreakException('Ctrl+D');
394 // handle empty input
395 if (trim($input) === '') {
399 $input = $this->onInput($input);
401 if ($this->hasCommand($input)) {
402 $this->addHistory($input);
403 $this->runCommand($input);
408 $this->addCode($input);
409 } while (!$this->hasValidCode());
413 * Run execution loop listeners before the shell session.
415 protected function beforeRun()
417 foreach ($this->loopListeners as $listener) {
418 $listener->beforeRun($this);
423 * Run execution loop listeners at the start of each loop.
425 public function beforeLoop()
427 foreach ($this->loopListeners as $listener) {
428 $listener->beforeLoop($this);
433 * Run execution loop listeners on user input.
435 * @param string $input
439 public function onInput($input)
441 foreach ($this->loopListeners as $listeners) {
442 if (($return = $listeners->onInput($this, $input)) !== null) {
451 * Run execution loop listeners on code to be executed.
453 * @param string $code
457 public function onExecute($code)
459 foreach ($this->loopListeners as $listener) {
460 if (($return = $listener->onExecute($this, $code)) !== null) {
469 * Run execution loop listeners after each loop.
471 public function afterLoop()
473 foreach ($this->loopListeners as $listener) {
474 $listener->afterLoop($this);
479 * Run execution loop listers after the shell session.
481 protected function afterRun()
483 foreach ($this->loopListeners as $listener) {
484 $listener->afterRun($this);
489 * Set the variables currently in scope.
493 public function setScopeVariables(array $vars)
495 $this->context->setAll($vars);
499 * Return the set of variables currently in scope.
501 * @param bool $includeBoundObject Pass false to exclude 'this'. If you're
502 * passing the scope variables to `extract`
503 * in PHP 7.1+, you _must_ exclude 'this'
505 * @return array Associative array of scope variables
507 public function getScopeVariables($includeBoundObject = true)
509 $vars = $this->context->getAll();
511 if (!$includeBoundObject) {
512 unset($vars['this']);
519 * Return the set of magic variables currently in scope.
521 * @param bool $includeBoundObject Pass false to exclude 'this'. If you're
522 * passing the scope variables to `extract`
523 * in PHP 7.1+, you _must_ exclude 'this'
525 * @return array Associative array of magic scope variables
527 public function getSpecialScopeVariables($includeBoundObject = true)
529 $vars = $this->context->getSpecialVariables();
531 if (!$includeBoundObject) {
532 unset($vars['this']);
539 * Get the set of unused command-scope variable names.
541 * @return array Array of unused variable names
543 public function getUnusedCommandScopeVariableNames()
545 return $this->context->getUnusedCommandScopeVariableNames();
549 * Get the set of variable names currently in scope.
551 * @return array Array of variable names
553 public function getScopeVariableNames()
555 return array_keys($this->context->getAll());
559 * Get a scope variable value by name.
561 * @param string $name
565 public function getScopeVariable($name)
567 return $this->context->get($name);
571 * Set the bound object ($this variable) for the interactive shell.
573 * @param object|null $boundObject
575 public function setBoundObject($boundObject)
577 $this->context->setBoundObject($boundObject);
581 * Get the bound object ($this variable) for the interactive shell.
583 * @return object|null
585 public function getBoundObject()
587 return $this->context->getBoundObject();
591 * Add includes, to be parsed and executed before running the interactive shell.
593 * @param array $includes
595 public function setIncludes(array $includes = [])
597 $this->includes = $includes;
601 * Get PHP files to be parsed and executed before running the interactive shell.
605 public function getIncludes()
607 return array_merge($this->config->getDefaultIncludes(), $this->includes);
611 * Check whether this shell's code buffer contains code.
613 * @return bool True if the code buffer contains code
615 public function hasCode()
617 return !empty($this->codeBuffer);
621 * Check whether the code in this shell's code buffer is valid.
623 * If the code is valid, the code buffer should be flushed and evaluated.
625 * @return bool True if the code buffer content is valid
627 protected function hasValidCode()
629 return !$this->codeBufferOpen && $this->code !== false;
633 * Add code to the code buffer.
635 * @param string $code
636 * @param bool $silent
638 public function addCode($code, $silent = false)
641 // Code lines ending in \ keep the buffer open
642 if (substr(rtrim($code), -1) === '\\') {
643 $this->codeBufferOpen = true;
644 $code = substr(rtrim($code), 0, -1);
646 $this->codeBufferOpen = false;
649 $this->codeBuffer[] = $silent ? new SilentInput($code) : $code;
650 $this->code = $this->cleaner->clean($this->codeBuffer, $this->config->requireSemicolons());
651 } catch (\Exception $e) {
652 // Add failed code blocks to the readline history.
653 $this->addCodeBufferToHistory();
660 * Set the code buffer.
662 * This is mostly used by `Shell::execute`. Any existing code in the input
663 * buffer is pushed onto a stack and will come back after this new code is
666 * @throws \InvalidArgumentException if $code isn't a complete statement
668 * @param string $code
669 * @param bool $silent
671 private function setCode($code, $silent = false)
673 if ($this->hasCode()) {
674 $this->codeStack[] = [$this->codeBuffer, $this->codeBufferOpen, $this->code];
677 $this->resetCodeBuffer();
679 $this->addCode($code, $silent);
680 } catch (\Throwable $e) {
681 $this->popCodeStack();
684 } catch (\Exception $e) {
685 $this->popCodeStack();
690 if (!$this->hasValidCode()) {
691 $this->popCodeStack();
693 throw new \InvalidArgumentException('Unexpected end of input');
698 * Get the current code buffer.
700 * This is useful for commands which manipulate the buffer.
704 public function getCodeBuffer()
706 return $this->codeBuffer;
710 * Run a Psy Shell command given the user input.
712 * @throws InvalidArgumentException if the input is not a valid command
714 * @param string $input User input string
716 * @return mixed Who knows?
718 protected function runCommand($input)
720 $command = $this->getCommand($input);
722 if (empty($command)) {
723 throw new \InvalidArgumentException('Command not found: ' . $input);
726 $input = new ShellInput(str_replace('\\', '\\\\', rtrim($input, " \t\n\r\0\x0B;")));
728 if ($input->hasParameterOption(['--help', '-h'])) {
729 $helpCommand = $this->get('help');
730 $helpCommand->setCommand($command);
732 return $helpCommand->run($input, $this->output);
735 return $command->run($input, $this->output);
739 * Reset the current code buffer.
741 * This should be run after evaluating user input, catching exceptions, or
742 * on demand by commands such as BufferCommand.
744 public function resetCodeBuffer()
746 $this->codeBuffer = [];
751 * Inject input into the input buffer.
753 * This is useful for commands which want to replay history.
755 * @param string|array $input
756 * @param bool $silent
758 public function addInput($input, $silent = false)
760 foreach ((array) $input as $line) {
761 $this->inputBuffer[] = $silent ? new SilentInput($line) : $line;
766 * Flush the current (valid) code buffer.
768 * If the code buffer is valid, resets the code buffer and returns the
771 * @return string PHP code buffer contents
773 public function flushCode()
775 if ($this->hasValidCode()) {
776 $this->addCodeBufferToHistory();
778 $this->popCodeStack();
785 * Reset the code buffer and restore any code pushed during `execute` calls.
787 private function popCodeStack()
789 $this->resetCodeBuffer();
791 if (empty($this->codeStack)) {
795 list($codeBuffer, $codeBufferOpen, $code) = array_pop($this->codeStack);
797 $this->codeBuffer = $codeBuffer;
798 $this->codeBufferOpen = $codeBufferOpen;
803 * (Possibly) add a line to the readline history.
805 * Like Bash, if the line starts with a space character, it will be omitted
806 * from history. Note that an entire block multi-line code input will be
807 * omitted iff the first line begins with a space.
809 * Additionally, if a line is "silent", i.e. it was initially added with the
810 * silent flag, it will also be omitted.
812 * @param string|SilentInput $line
814 private function addHistory($line)
816 if ($line instanceof SilentInput) {
820 // Skip empty lines and lines starting with a space
821 if (trim($line) !== '' && substr($line, 0, 1) !== ' ') {
822 $this->readline->addHistory($line);
827 * Filter silent input from code buffer, write the rest to readline history.
829 private function addCodeBufferToHistory()
831 $codeBuffer = array_filter($this->codeBuffer, function ($line) {
832 return !$line instanceof SilentInput;
835 $this->addHistory(implode("\n", $codeBuffer));
839 * Get the current evaluation scope namespace.
841 * @see CodeCleaner::getNamespace
843 * @return string Current code namespace
845 public function getNamespace()
847 if ($namespace = $this->cleaner->getNamespace()) {
848 return implode('\\', $namespace);
853 * Write a string to stdout.
855 * This is used by the shell loop for rendering output from evaluated code.
858 * @param int $phase Output buffering phase
860 public function writeStdout($out, $phase = PHP_OUTPUT_HANDLER_END)
862 $isCleaning = $phase & PHP_OUTPUT_HANDLER_CLEAN;
865 if ($out !== '' && !$isCleaning) {
866 $this->output->write($out, false, ShellOutput::OUTPUT_RAW);
867 $this->outputWantsNewline = (substr($out, -1) !== "\n");
868 $this->stdoutBuffer .= $out;
871 // Output buffering is done!
872 if ($phase & PHP_OUTPUT_HANDLER_END) {
873 // Write an extra newline if stdout didn't end with one
874 if ($this->outputWantsNewline) {
875 $this->output->writeln(sprintf('<aside>%s</aside>', $this->config->useUnicode() ? '⏎' : '\\n'));
876 $this->outputWantsNewline = false;
879 // Save the stdout buffer as $__out
880 if ($this->stdoutBuffer !== '') {
881 $this->context->setLastStdout($this->stdoutBuffer);
882 $this->stdoutBuffer = '';
888 * Write a return value to stdout.
890 * The return value is formatted or pretty-printed, and rendered in a
891 * visibly distinct manner (in this case, as cyan).
893 * @see self::presentValue
897 public function writeReturnValue($ret)
899 if ($ret instanceof NoReturnValue) {
903 $this->context->setReturnValue($ret);
904 $ret = $this->presentValue($ret);
905 $indent = str_repeat(' ', strlen(static::RETVAL));
907 $this->output->writeln(static::RETVAL . str_replace(PHP_EOL, PHP_EOL . $indent, $ret));
911 * Renders a caught Exception.
913 * Exceptions are formatted according to severity. ErrorExceptions which were
914 * warnings or Strict errors aren't rendered as harshly as real errors.
916 * Stores $e as the last Exception in the Shell Context.
918 * @param \Exception $e An exception instance
920 public function writeException(\Exception $e)
922 $this->context->setLastException($e);
923 $this->output->writeln($this->formatException($e));
924 $this->resetCodeBuffer();
928 * Helper for formatting an exception for writeException().
930 * @todo extract this to somewhere it makes more sense
932 * @param \Exception $e
936 public function formatException(\Exception $e)
938 $message = $e->getMessage();
939 if (!$e instanceof PsyException) {
940 if ($message === '') {
941 $message = get_class($e);
943 $message = sprintf('%s with message \'%s\'', get_class($e), $message);
947 $message = preg_replace(
948 "#(\\w:)?(/\\w+)*/src/ExecutionClosure.php\(\d+\) : eval\(\)'d code#",
950 str_replace('\\', '/', $message)
953 $message = str_replace(" in eval()'d code", ' in Psy Shell code', $message);
955 $severity = ($e instanceof \ErrorException) ? $this->getSeverity($e) : 'error';
957 return sprintf('<%s>%s</%s>', $severity, OutputFormatter::escape($message), $severity);
961 * Helper for getting an output style for the given ErrorException's level.
963 * @param \ErrorException $e
967 protected function getSeverity(\ErrorException $e)
969 $severity = $e->getSeverity();
970 if ($severity & error_reporting()) {
975 case E_COMPILE_WARNING:
985 // Since this is below the user's reporting threshold, it's always going to be a warning.
991 * Execute code in the shell execution context.
993 * @param string $code
994 * @param bool $throwExceptions
998 public function execute($code, $throwExceptions = false)
1000 $this->setCode($code, true);
1001 $closure = new ExecutionClosure($this);
1003 if ($throwExceptions) {
1004 return $closure->execute();
1008 return $closure->execute();
1009 } catch (\TypeError $_e) {
1010 $this->writeException(TypeErrorException::fromTypeError($_e));
1011 } catch (\Error $_e) {
1012 $this->writeException(ErrorException::fromError($_e));
1013 } catch (\Exception $_e) {
1014 $this->writeException($_e);
1019 * Helper for throwing an ErrorException.
1021 * This allows us to:
1023 * set_error_handler(array($psysh, 'handleError'));
1025 * Unlike ErrorException::throwException, this error handler respects the
1026 * current error_reporting level; i.e. it logs warnings and notices, but
1027 * doesn't throw an exception unless it's above the current error_reporting
1028 * threshold. This should probably only be used in the inner execution loop
1029 * of the shell, as most of the time a thrown exception is much more useful.
1031 * If the error type matches the `errorLoggingLevel` config, it will be
1032 * logged as well, regardless of the `error_reporting` level.
1034 * @see \Psy\Exception\ErrorException::throwException
1035 * @see \Psy\Shell::writeException
1037 * @throws \Psy\Exception\ErrorException depending on the current error_reporting level
1039 * @param int $errno Error type
1040 * @param string $errstr Message
1041 * @param string $errfile Filename
1042 * @param int $errline Line number
1044 public function handleError($errno, $errstr, $errfile, $errline)
1046 if ($errno & error_reporting()) {
1047 ErrorException::throwException($errno, $errstr, $errfile, $errline);
1048 } elseif ($errno & $this->config->errorLoggingLevel()) {
1049 // log it and continue...
1050 $this->writeException(new ErrorException($errstr, 0, $errno, $errfile, $errline));
1055 * Format a value for display.
1057 * @see Presenter::present
1061 * @return string Formatted value
1063 protected function presentValue($val)
1065 return $this->config->getPresenter()->present($val);
1069 * Get a command (if one exists) for the current input string.
1071 * @param string $input
1073 * @return null|BaseCommand
1075 protected function getCommand($input)
1077 $input = new StringInput($input);
1078 if ($name = $input->getFirstArgument()) {
1079 return $this->get($name);
1084 * Check whether a command is set for the current input string.
1086 * @param string $input
1088 * @return bool True if the shell has a command for the given input
1090 protected function hasCommand($input)
1092 $input = new StringInput($input);
1093 if ($name = $input->getFirstArgument()) {
1094 return $this->has($name);
1101 * Get the current input prompt.
1105 protected function getPrompt()
1107 if ($this->hasCode()) {
1108 return static::BUFF_PROMPT;
1111 return $this->config->getPrompt() ?: static::PROMPT;
1115 * Read a line of user input.
1117 * This will return a line from the input buffer (if any exist). Otherwise,
1118 * it will ask the user for input.
1120 * If readline is enabled, this delegates to readline. Otherwise, it's an
1121 * ugly `fgets` call.
1123 * @return string One line of user input
1125 protected function readline()
1127 if (!empty($this->inputBuffer)) {
1128 $line = array_shift($this->inputBuffer);
1129 if (!$line instanceof SilentInput) {
1130 $this->output->writeln(sprintf('<aside>%s %s</aside>', static::REPLAY, OutputFormatter::escape($line)));
1136 if ($bracketedPaste = $this->config->useBracketedPaste()) {
1137 printf("\e[?2004h"); // Enable bracketed paste
1140 $line = $this->readline->readline($this->getPrompt());
1142 if ($bracketedPaste) {
1143 printf("\e[?2004l"); // ... and disable it again
1150 * Get the shell output header.
1154 protected function getHeader()
1156 return sprintf('<aside>%s by Justin Hileman</aside>', $this->getVersion());
1160 * Get the current version of Psy Shell.
1164 public function getVersion()
1166 $separator = $this->config->useUnicode() ? '—' : '-';
1168 return sprintf('Psy Shell %s (PHP %s %s %s)', self::VERSION, phpversion(), $separator, php_sapi_name());
1172 * Get a PHP manual database instance.
1176 public function getManualDb()
1178 return $this->config->getManualDb();
1182 * @deprecated Tab completion is provided by the AutoCompleter service
1184 protected function autocomplete($text)
1186 @trigger_error('Tab completion is provided by the AutoCompleter service', E_USER_DEPRECATED);
1190 * Initialize tab completion matchers.
1192 * If tab completion is enabled this adds tab completion matchers to the
1193 * auto completer and sets context if needed.
1195 protected function initializeTabCompletion()
1197 if (!$this->config->useTabCompletion()) {
1201 $this->autoCompleter = $this->config->getAutoCompleter();
1203 // auto completer needs shell to be linked to configuration because of
1204 // the context aware matchers
1205 $this->addMatchersToAutoCompleter($this->getDefaultMatchers());
1206 $this->addMatchersToAutoCompleter($this->matchers);
1208 $this->autoCompleter->activate();
1212 * Add matchers to the auto completer, setting context if needed.
1214 * @param array $matchers
1216 private function addMatchersToAutoCompleter(array $matchers)
1218 foreach ($matchers as $matcher) {
1219 if ($matcher instanceof ContextAware) {
1220 $matcher->setContext($this->context);
1222 $this->autoCompleter->addMatcher($matcher);
1227 * @todo Implement self-update
1228 * @todo Implement prompt to start update
1230 * @return void|string
1232 protected function writeVersionInfo()
1234 if (PHP_SAPI !== 'cli') {
1239 $client = $this->config->getChecker();
1240 if (!$client->isLatest()) {
1241 $this->output->writeln(sprintf('New version is available (current: %s, latest: %s)', self::VERSION, $client->getLatest()));
1243 } catch (\InvalidArgumentException $e) {
1244 $this->output->writeln($e->getMessage());
1249 * Write a startup message if set.
1251 protected function writeStartupMessage()
1253 $message = $this->config->getStartupMessage();
1254 if ($message !== null && $message !== '') {
1255 $this->output->writeln($message);