Constructor
__construct(?Configuration $config = null)
Create a new Psy Shell instance.
Configuration instance. If not provided, a default Configuration will be created.
Public Methods
run(?InputInterface $input = null, ?OutputInterface $output = null): int
Run the PsySH REPL.
Symfony Console input instance (optional)
Symfony Console output instance (optional)
Exit code (0 on success)
boot(?InputInterface $input = null, ?OutputInterface $output = null): void
Boot the shell, initializing the CodeCleaner and Readline.
This is called lazily when commands or methods require these dependencies.
Input for trust prompts (optional)
Output for trust prompts (optional)
execute(string $code, bool $throwExceptions = false): mixed
Execute code in the shell execution context.
PHP code to execute
Whether to throw exceptions instead of catching them
The result of the executed code
addCommand(BaseCommand|callable $command): ?BaseCommand
Add a command to the shell.
A Symfony Console Command object or callable
The registered command, or null
setScopeVariables(array $vars): void
Set the variables currently in scope.
Associative array of variable names to values
getScopeVariables(bool $includeBoundObject = true): array
Return the set of variables currently in scope.
Pass false to exclude ‘this’. If you’re passing the scope variables to
extract() you must exclude ‘this’.Associative array of scope variables
getScopeVariable(string $name): mixed
Get a scope variable value by name.
Variable name (without $)
Variable value
setBoundObject($boundObject): void
Set the bound object ($this variable) for the interactive shell.
Object to bind as $this in the shell
getBoundObject(): ?object
Get the bound object ($this variable) for the interactive shell.
The bound object, or null
setBoundClass($boundClass): void
Set the bound class (self) for the interactive shell.
Fully qualified class name to bind as self
getBoundClass(): ?string
Get the bound class (self) for the interactive shell.
The bound class name, or null
setIncludes(array $includes = []): void
Add includes to be parsed and executed before running the interactive shell.
Array of file paths to include
getIncludes(): array
Get PHP files to be parsed and executed before running the interactive shell.
Array of file paths
addMatchers(array $matchers): void
Add tab completion matchers.
Array of matcher instances
setOutput(OutputInterface $output): void
Set the shell output.
Symfony Console output instance
setForceReload(bool $force): void
Enable or disable force-reload mode for code reloaders.
Used by the yolo command to bypass safety warnings when reloading code.
Whether to force reload
hasCode(): bool
Check whether this shell’s code buffer contains code.
True if the code buffer contains code
addCode(string $code, bool $silent = false): void
Add code to the code buffer.
PHP code to add
Whether to add silently (won’t appear in history)
getCodeBuffer(): array
Get the current code buffer.
This is useful for commands which manipulate the buffer.
Array of code lines in the buffer
resetCodeBuffer(): void
Reset the current code buffer.
This should be run after evaluating user input, catching exceptions, or on demand by commands.
flushCode(): ?string
Flush the current (valid) code buffer.
If the code buffer is valid, resets the code buffer and returns the current code.
PHP code buffer contents, or null if invalid
addInput($input, bool $silent = false): void
Inject input into the input buffer.
This is useful for commands which want to replay history.
Input line(s) to inject
Whether to add silently
getNamespace(): ?string
Get the current evaluation scope namespace.
Current code namespace, or null
writeStdout(string $out, int $phase = PHP_OUTPUT_HANDLER_END): string
Write a string to stdout.
This is used by the shell loop for rendering output from evaluated code.
Output to write
Output buffering phase
Empty string
writeReturnValue($ret, bool $rawOutput = false): void
Write a return value to stdout.
The return value is formatted or pretty-printed, and rendered in a visibly distinct manner.
Return value to write
Write raw var_export-style values
writeException(Throwable $e): void
Render and write a caught exception or error.
Exceptions are formatted according to severity. Stores the exception as the last exception in the shell context.
Exception or error instance to write
getLastExecSuccess(): bool
Check whether the last execution was successful.
Returns true if a return value was logged rather than an exception.
True if last execution was successful
formatException(Throwable $e): string
Helper for formatting an exception or error for writeException().
Exception to format
Formatted exception message
handleError(int $errno, string $errstr, string $errfile, int $errline): void
Error handler for throwing ErrorExceptions.
This respects error levels and the errorLoggingLevel config.
Error type
Error message
Filename where error occurred
Line number where error occurred
Static Methods
isIncluded(array $trace): bool
Check whether the first thing in a backtrace is an include call.
This is used by the psysh bin to decide whether to start a shell on boot, or to simply autoload the library.
Backtrace array from debug_backtrace()
True if the trace shows an include
isPhar(): bool
Check if the currently running PsySH bin is a phar archive.
True if running from a phar
getVersionHeader(bool $useUnicode = false): string
Get a pretty header including the current version of PsySH.
Whether to use Unicode characters
Version header string
Constants
Current PsySH version number (e.g., ‘v0.12.20’)