refactor: susun semula struktur folder — Laravel source ke src/
This commit is contained in:
64
vendor/psy/psysh/src/ExecutionLoop/AbstractListener.php
vendored
Normal file
64
vendor/psy/psysh/src/ExecutionLoop/AbstractListener.php
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use Psy\Shell;
|
||||
|
||||
/**
|
||||
* Abstract Execution Loop Listener class.
|
||||
*/
|
||||
abstract class AbstractListener implements Listener
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function beforeRun(Shell $shell)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function beforeLoop(Shell $shell)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onInput(Shell $shell, string $input)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onExecute(Shell $shell, string $code)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function afterLoop(Shell $shell)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function afterRun(Shell $shell, int $exitCode = 0)
|
||||
{
|
||||
}
|
||||
}
|
||||
51
vendor/psy/psysh/src/ExecutionLoop/ExecutionLoggingListener.php
vendored
Normal file
51
vendor/psy/psysh/src/ExecutionLoop/ExecutionLoggingListener.php
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use Psy\Shell;
|
||||
use Psy\ShellLogger;
|
||||
|
||||
/**
|
||||
* Execution logging listener.
|
||||
*
|
||||
* Logs code about to be executed to a ShellLogger.
|
||||
*/
|
||||
class ExecutionLoggingListener extends AbstractListener
|
||||
{
|
||||
private ShellLogger $logger;
|
||||
|
||||
/**
|
||||
* @param ShellLogger $logger
|
||||
*/
|
||||
public function __construct(ShellLogger $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onExecute(Shell $shell, string $code)
|
||||
{
|
||||
$this->logger->logExecute($code);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
51
vendor/psy/psysh/src/ExecutionLoop/InputLoggingListener.php
vendored
Normal file
51
vendor/psy/psysh/src/ExecutionLoop/InputLoggingListener.php
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use Psy\Shell;
|
||||
use Psy\ShellLogger;
|
||||
|
||||
/**
|
||||
* Input logging listener.
|
||||
*
|
||||
* Logs user code input to a ShellLogger.
|
||||
*/
|
||||
class InputLoggingListener extends AbstractListener
|
||||
{
|
||||
private ShellLogger $logger;
|
||||
|
||||
/**
|
||||
* @param ShellLogger $logger
|
||||
*/
|
||||
public function __construct(ShellLogger $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onInput(Shell $shell, string $input)
|
||||
{
|
||||
$this->logger->logInput($input);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
82
vendor/psy/psysh/src/ExecutionLoop/Listener.php
vendored
Normal file
82
vendor/psy/psysh/src/ExecutionLoop/Listener.php
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use Psy\Shell;
|
||||
|
||||
/**
|
||||
* Execution Loop Listener interface.
|
||||
*/
|
||||
interface Listener
|
||||
{
|
||||
/**
|
||||
* Determines whether this listener should be active.
|
||||
*/
|
||||
public static function isSupported(): bool;
|
||||
|
||||
/**
|
||||
* Called once before the REPL session starts.
|
||||
*
|
||||
* @param Shell $shell
|
||||
*/
|
||||
public function beforeRun(Shell $shell);
|
||||
|
||||
/**
|
||||
* Called at the start of each loop.
|
||||
*
|
||||
* @param Shell $shell
|
||||
*/
|
||||
public function beforeLoop(Shell $shell);
|
||||
|
||||
/**
|
||||
* Called on user input.
|
||||
*
|
||||
* Return a new string to override or rewrite user input.
|
||||
*
|
||||
* @param Shell $shell
|
||||
* @param string $input
|
||||
*
|
||||
* @return string|null User input override
|
||||
*/
|
||||
public function onInput(Shell $shell, string $input);
|
||||
|
||||
/**
|
||||
* Called before executing user code.
|
||||
*
|
||||
* Return a new string to override or rewrite user code.
|
||||
*
|
||||
* Note that this is run *after* the Code Cleaner, so if you return invalid
|
||||
* or unsafe PHP here, it'll be executed without any of the safety Code
|
||||
* Cleaner provides. This comes with the big kid warranty :)
|
||||
*
|
||||
* @param Shell $shell
|
||||
* @param string $code
|
||||
*
|
||||
* @return string|null User code override
|
||||
*/
|
||||
public function onExecute(Shell $shell, string $code);
|
||||
|
||||
/**
|
||||
* Called at the end of each loop.
|
||||
*
|
||||
* @param Shell $shell
|
||||
*/
|
||||
public function afterLoop(Shell $shell);
|
||||
|
||||
/**
|
||||
* Called once after the REPL session ends.
|
||||
*
|
||||
* @param Shell $shell
|
||||
* @param int $exitCode Exit code from the execution loop
|
||||
*/
|
||||
public function afterRun(Shell $shell, int $exitCode = 0);
|
||||
}
|
||||
429
vendor/psy/psysh/src/ExecutionLoop/ProcessForker.php
vendored
Normal file
429
vendor/psy/psysh/src/ExecutionLoop/ProcessForker.php
vendored
Normal file
@@ -0,0 +1,429 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use Psy\Context;
|
||||
use Psy\Exception\BreakException;
|
||||
use Psy\Exception\InterruptException;
|
||||
use Psy\Shell;
|
||||
use Psy\Util\DependencyChecker;
|
||||
|
||||
/**
|
||||
* An execution loop listener that forks the process before executing code.
|
||||
*
|
||||
* This is awesome, as the session won't die prematurely if user input includes
|
||||
* a fatal error, such as redeclaring a class or function.
|
||||
*/
|
||||
class ProcessForker extends AbstractListener
|
||||
{
|
||||
private ?int $savegame = null;
|
||||
/** @var resource */
|
||||
private $up;
|
||||
private bool $sigintHandlerInstalled = false;
|
||||
private bool $restoreStty = false;
|
||||
private ?string $originalStty = null;
|
||||
|
||||
public const PCNTL_FUNCTIONS = [
|
||||
'pcntl_fork',
|
||||
'pcntl_signal_dispatch',
|
||||
'pcntl_signal',
|
||||
'pcntl_waitpid',
|
||||
'pcntl_wexitstatus',
|
||||
];
|
||||
|
||||
public const POSIX_FUNCTIONS = [
|
||||
'posix_getpid',
|
||||
'posix_kill',
|
||||
];
|
||||
|
||||
/**
|
||||
* Process forker is supported if pcntl and posix extensions are available.
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return DependencyChecker::functionsAvailable(self::PCNTL_FUNCTIONS)
|
||||
&& DependencyChecker::functionsAvailable(self::POSIX_FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that all required pcntl functions are, in fact, available.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public static function isPcntlSupported(): bool
|
||||
{
|
||||
return DependencyChecker::functionsAvailable(self::PCNTL_FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether required pcntl functions are disabled.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public static function disabledPcntlFunctions()
|
||||
{
|
||||
return DependencyChecker::functionsDisabled(self::PCNTL_FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that all required posix functions are, in fact, available.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public static function isPosixSupported(): bool
|
||||
{
|
||||
return DependencyChecker::functionsAvailable(self::POSIX_FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether required posix functions are disabled.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public static function disabledPosixFunctions()
|
||||
{
|
||||
return DependencyChecker::functionsDisabled(self::POSIX_FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forks into a main and a loop process.
|
||||
*
|
||||
* The loop process will handle the evaluation of all instructions, then
|
||||
* return its state via a socket upon completion.
|
||||
*
|
||||
* @param Shell $shell
|
||||
*/
|
||||
public function beforeRun(Shell $shell)
|
||||
{
|
||||
// Temporarily disable socket timeout for IPC sockets, to avoid losing our child process
|
||||
// communication after 60 seconds.
|
||||
$originalTimeout = @\ini_set('default_socket_timeout', '-1');
|
||||
|
||||
list($up, $down) = \stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
|
||||
|
||||
if ($originalTimeout !== false) {
|
||||
@\ini_set('default_socket_timeout', $originalTimeout);
|
||||
}
|
||||
|
||||
if (!$up) {
|
||||
throw new \RuntimeException('Unable to create socket pair');
|
||||
}
|
||||
|
||||
$pid = \pcntl_fork();
|
||||
if ($pid < 0) {
|
||||
throw new \RuntimeException('Unable to start execution loop');
|
||||
} elseif ($pid > 0) {
|
||||
// This is the main (parent) process. Install SIGINT handler and wait for child.
|
||||
|
||||
// We won't be needing this one.
|
||||
\fclose($up);
|
||||
|
||||
// Install SIGINT handler in parent to interrupt child
|
||||
\pcntl_async_signals(true);
|
||||
$interrupted = false;
|
||||
$sigintHandlerInstalled = \pcntl_signal(\SIGINT, function () use (&$interrupted, $pid) {
|
||||
$interrupted = true;
|
||||
// Send SIGINT to child so it can handle interruption gracefully
|
||||
\posix_kill($pid, \SIGINT);
|
||||
});
|
||||
|
||||
// Wait for a return value from the loop process.
|
||||
$read = [$down];
|
||||
$write = null;
|
||||
$except = null;
|
||||
|
||||
do {
|
||||
if ($interrupted) {
|
||||
// Wait for child to exit (it should handle SIGINT gracefully)
|
||||
\pcntl_waitpid($pid, $status);
|
||||
|
||||
// Try to read any final output from child before it exited
|
||||
$content = @\stream_get_contents($down);
|
||||
\fclose($down);
|
||||
|
||||
if ($sigintHandlerInstalled) {
|
||||
\pcntl_signal(\SIGINT, \SIG_DFL);
|
||||
}
|
||||
|
||||
$this->clearStdinBuffer();
|
||||
|
||||
// Restore scope variables and exit code if child sent any
|
||||
// If child didn't send data, use the actual process exit status
|
||||
$exitCode = \pcntl_wexitstatus($status);
|
||||
if ($content) {
|
||||
$data = @\unserialize($content);
|
||||
if (\is_array($data) && isset($data['exitCode'], $data['scopeVars'])) {
|
||||
$exitCode = $data['exitCode'];
|
||||
$shell->setScopeVariables($data['scopeVars']);
|
||||
}
|
||||
}
|
||||
|
||||
throw new BreakException('Exiting main thread', $exitCode);
|
||||
}
|
||||
|
||||
$n = @\stream_select($read, $write, $except, null);
|
||||
|
||||
if ($n === 0) {
|
||||
throw new \RuntimeException('Process timed out waiting for execution loop');
|
||||
}
|
||||
|
||||
if ($n === false) {
|
||||
$err = \error_get_last();
|
||||
$errMessage = \is_array($err) ? ($err['message'] ?? null) : null;
|
||||
|
||||
// If there's no error message, or it's an interrupted system call, just retry
|
||||
if ($errMessage === null || \stripos($errMessage, 'interrupted system call') !== false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new \RuntimeException(\sprintf('Error waiting for execution loop: %s', $errMessage));
|
||||
}
|
||||
} while ($n < 1);
|
||||
|
||||
$content = \stream_get_contents($down);
|
||||
\fclose($down);
|
||||
|
||||
// Wait for child to exit and get its exit status
|
||||
\pcntl_waitpid($pid, $status);
|
||||
|
||||
// Restore default SIGINT handler
|
||||
if ($sigintHandlerInstalled) {
|
||||
\pcntl_signal(\SIGINT, \SIG_DFL);
|
||||
}
|
||||
|
||||
// If child didn't send data, use the actual process exit status
|
||||
$exitCode = \pcntl_wexitstatus($status);
|
||||
if ($content) {
|
||||
$data = @\unserialize($content);
|
||||
if (\is_array($data) && isset($data['exitCode'], $data['scopeVars'])) {
|
||||
$exitCode = $data['exitCode'];
|
||||
$shell->setScopeVariables($data['scopeVars']);
|
||||
}
|
||||
}
|
||||
|
||||
throw new BreakException('Exiting main thread', $exitCode);
|
||||
}
|
||||
|
||||
// This is the child process. It's going to do all the work.
|
||||
if (!@\cli_set_process_title('psysh (loop)')) {
|
||||
// Fall back to `setproctitle` if that wasn't succesful.
|
||||
if (\function_exists('setproctitle')) {
|
||||
@\setproctitle('psysh (loop)');
|
||||
}
|
||||
}
|
||||
|
||||
// We won't be needing this one.
|
||||
\fclose($down);
|
||||
|
||||
// Save this; we'll need to close it in `afterRun`
|
||||
$this->up = $up;
|
||||
|
||||
// Save original stty state so we can restore on exit
|
||||
if (@\posix_isatty(\STDIN)) {
|
||||
$this->originalStty = @\shell_exec('stty -g 2>/dev/null');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Install SIGINT handler before executing user code.
|
||||
*/
|
||||
public function onExecute(Shell $shell, string $code)
|
||||
{
|
||||
// Only handle SIGINT in the child process
|
||||
if (isset($this->up)) {
|
||||
// Ensure signal processing is enabled so Ctrl-C can interrupt execution
|
||||
if (@\posix_isatty(\STDIN)) {
|
||||
@\shell_exec('stty isig 2>/dev/null');
|
||||
$this->restoreStty = true;
|
||||
}
|
||||
|
||||
\pcntl_async_signals(true);
|
||||
|
||||
// Install SIGINT handler that throws exception during execution
|
||||
\pcntl_signal(\SIGINT, function () {
|
||||
throw new InterruptException('Ctrl+C');
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a savegame at the start of each loop iteration.
|
||||
*
|
||||
* @param Shell $shell
|
||||
*/
|
||||
public function beforeLoop(Shell $shell)
|
||||
{
|
||||
$this->createSavegame();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up old savegames at the end of each loop iteration.
|
||||
*
|
||||
* Restores terminal state and clears stdin if execution was interrupted.
|
||||
*/
|
||||
public function afterLoop(Shell $shell)
|
||||
{
|
||||
// Only handle cleanup in child process
|
||||
if (isset($this->up)) {
|
||||
// Restore default SIGINT handler after execution
|
||||
if (!$this->sigintHandlerInstalled) {
|
||||
\pcntl_signal(\SIGINT, \SIG_DFL);
|
||||
}
|
||||
|
||||
// Restore terminal to raw mode after execution
|
||||
// This prevents Ctrl-C at the prompt from generating SIGINT
|
||||
if ($this->restoreStty) {
|
||||
@\shell_exec('stty -isig 2>/dev/null');
|
||||
$this->restoreStty = false;
|
||||
}
|
||||
}
|
||||
|
||||
// if there's an old savegame hanging around, let's kill it.
|
||||
if (isset($this->savegame)) {
|
||||
\posix_kill($this->savegame, \SIGKILL);
|
||||
\pcntl_signal_dispatch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* After the REPL session ends, send the scope variables back up to the main
|
||||
* thread (if this is a child thread).
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function afterRun(Shell $shell, int $exitCode = 0)
|
||||
{
|
||||
// We're a child thread. Send the scope variables and exit code back up to the main thread.
|
||||
if (isset($this->up)) {
|
||||
$data = $this->serializeReturn($exitCode, $shell->getScopeVariables(false));
|
||||
|
||||
// Suppress errors in case the pipe is broken (e.g., if parent was interrupted)
|
||||
@\fwrite($this->up, $data);
|
||||
@\fclose($this->up);
|
||||
|
||||
// Restore original terminal state before exiting.
|
||||
//
|
||||
// We set `stty isig` during execution, so Ctrl-C can interrupt, and
|
||||
// `stty -isig` after, so readline can handle it at the prompt.
|
||||
// Let's put things back the way we found them.
|
||||
if ($this->originalStty !== null) {
|
||||
@\shell_exec('stty '.\escapeshellarg(\trim($this->originalStty)).' 2>/dev/null');
|
||||
}
|
||||
|
||||
\posix_kill(\posix_getpid(), \SIGKILL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a savegame fork.
|
||||
*
|
||||
* The savegame contains the current execution state, and can be resumed in
|
||||
* the event that the worker dies unexpectedly (for example, by encountering
|
||||
* a PHP fatal error).
|
||||
*/
|
||||
private function createSavegame()
|
||||
{
|
||||
// the current process will become the savegame
|
||||
$this->savegame = \posix_getpid();
|
||||
|
||||
$pid = \pcntl_fork();
|
||||
if ($pid < 0) {
|
||||
throw new \RuntimeException('Unable to create savegame fork');
|
||||
} elseif ($pid > 0) {
|
||||
// we're the savegame now... let's wait and see what happens
|
||||
\pcntl_waitpid($pid, $status);
|
||||
|
||||
// worker exited cleanly, let's bail
|
||||
if (!\pcntl_wexitstatus($status)) {
|
||||
\posix_kill(\posix_getpid(), \SIGKILL);
|
||||
}
|
||||
|
||||
// worker didn't exit cleanly, we'll need to have another go
|
||||
// @phan-suppress-next-line PhanPossiblyInfiniteRecursionSameParams - recursion exits via posix_kill above
|
||||
$this->createSavegame();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear stdin buffer after interruption, in case SIGINT left the stream in a bad state.
|
||||
*/
|
||||
private function clearStdinBuffer(): void
|
||||
{
|
||||
if (!\defined('STDIN') || !\is_resource(\STDIN)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the stream is still usable
|
||||
$meta = @\stream_get_meta_data(\STDIN);
|
||||
if (!$meta || ($meta['eof'] ?? false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Drain any buffered input, suppressing I/O errors
|
||||
@\stream_set_blocking(\STDIN, false);
|
||||
while (@\fgetc(\STDIN) !== false) {
|
||||
}
|
||||
@\stream_set_blocking(\STDIN, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize exit code and scope variables for transmission to parent process.
|
||||
*
|
||||
* A naïve serialization will run into issues if there is a Closure or
|
||||
* SimpleXMLElement (among other things) in scope when exiting the execution
|
||||
* loop. We'll just ignore these unserializable classes, and serialize what
|
||||
* we can.
|
||||
*
|
||||
* @param int $exitCode Exit code from the child process
|
||||
* @param array $scopeVars Scope variables to serialize
|
||||
*
|
||||
* @return string Serialized data array containing exitCode and scopeVars
|
||||
*/
|
||||
private function serializeReturn(int $exitCode, array $scopeVars): string
|
||||
{
|
||||
$serializable = [];
|
||||
|
||||
foreach ($scopeVars as $key => $value) {
|
||||
// No need to return magic variables
|
||||
if (Context::isSpecialVariableName($key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Resources and Closures don't error, but they don't serialize well either.
|
||||
if (\is_resource($value) || $value instanceof \Closure) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (\PHP_VERSION_ID >= 80100 && $value instanceof \UnitEnum) {
|
||||
// Enums defined in the REPL session can't be unserialized.
|
||||
$ref = new \ReflectionObject($value);
|
||||
if (\strpos($ref->getFileName(), ": eval()'d code") !== false) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@\serialize($value);
|
||||
$serializable[$key] = $value;
|
||||
} catch (\Throwable $e) {
|
||||
// we'll just ignore this one...
|
||||
}
|
||||
}
|
||||
|
||||
return @\serialize([
|
||||
'exitCode' => $exitCode,
|
||||
'scopeVars' => $serializable,
|
||||
]);
|
||||
}
|
||||
}
|
||||
149
vendor/psy/psysh/src/ExecutionLoop/RunkitReloader.php
vendored
Normal file
149
vendor/psy/psysh/src/ExecutionLoop/RunkitReloader.php
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use PhpParser\Parser;
|
||||
use Psy\ConfigPaths;
|
||||
use Psy\Exception\ParseErrorException;
|
||||
use Psy\OutputAware;
|
||||
use Psy\ParserFactory;
|
||||
use Psy\Shell;
|
||||
use Psy\Util\Docblock;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* A runkit-based code reloader, which is pretty much magic.
|
||||
*
|
||||
* @todo Remove RunkitReloader once we drop support for PHP 7.x :(
|
||||
*/
|
||||
class RunkitReloader extends AbstractListener implements OutputAware
|
||||
{
|
||||
private Parser $parser;
|
||||
private ?OutputInterface $output = null;
|
||||
private array $timestamps = [];
|
||||
|
||||
/**
|
||||
* Only enabled if Runkit is installed.
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
// runkit_import was removed in runkit7-4.0.0a1
|
||||
return \extension_loaded('runkit') || \extension_loaded('runkit7') && \function_exists('runkit_import');
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Runkit Reloader.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->parser = (new ParserFactory())->createParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): void
|
||||
{
|
||||
$this->output = $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload code on input.
|
||||
*/
|
||||
public function onInput(Shell $shell, string $input)
|
||||
{
|
||||
$this->reload($shell);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look through included files and update anything with a new timestamp.
|
||||
*/
|
||||
private function reload(Shell $shell)
|
||||
{
|
||||
\clearstatcache();
|
||||
$modified = [];
|
||||
|
||||
foreach (\get_included_files() as $file) {
|
||||
$timestamp = \filemtime($file);
|
||||
|
||||
if (!isset($this->timestamps[$file])) {
|
||||
$this->timestamps[$file] = $timestamp;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->timestamps[$file] === $timestamp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->lintFile($file)) {
|
||||
$msg = \sprintf('Modified file "%s" could not be reloaded', $file);
|
||||
$shell->writeException(new ParseErrorException($msg));
|
||||
continue;
|
||||
}
|
||||
|
||||
$modified[] = $file;
|
||||
$this->timestamps[$file] = $timestamp;
|
||||
}
|
||||
|
||||
if (\count($modified) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear magic method/property cache since docblocks may have changed
|
||||
Docblock::clearMagicCache();
|
||||
|
||||
// Notify user about reload attempts
|
||||
if ($this->output) {
|
||||
if (\count($modified) === 1) {
|
||||
$this->output->writeln(\sprintf('<whisper>Reloading %s</whisper>', ConfigPaths::prettyPath($modified[0])));
|
||||
} else {
|
||||
$this->output->writeln(\sprintf('<whisper>Reloading %d files</whisper>', \count($modified)));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($modified as $file) {
|
||||
$flags = (
|
||||
RUNKIT_IMPORT_FUNCTIONS |
|
||||
RUNKIT_IMPORT_CLASSES |
|
||||
RUNKIT_IMPORT_CLASS_METHODS |
|
||||
RUNKIT_IMPORT_CLASS_CONSTS |
|
||||
RUNKIT_IMPORT_CLASS_PROPS |
|
||||
RUNKIT_IMPORT_OVERRIDE
|
||||
);
|
||||
|
||||
// these two const cannot be used with RUNKIT_IMPORT_OVERRIDE in runkit7
|
||||
if (\extension_loaded('runkit7')) {
|
||||
$flags &= ~RUNKIT_IMPORT_CLASS_PROPS & ~RUNKIT_IMPORT_CLASS_STATIC_PROPS;
|
||||
runkit7_import($file, $flags);
|
||||
} else {
|
||||
runkit_import($file, $flags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file has valid PHP syntax.
|
||||
*/
|
||||
private function lintFile(string $file): bool
|
||||
{
|
||||
// first try to parse it
|
||||
try {
|
||||
$this->parser->parse(\file_get_contents($file));
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
126
vendor/psy/psysh/src/ExecutionLoop/SignalHandler.php
vendored
Normal file
126
vendor/psy/psysh/src/ExecutionLoop/SignalHandler.php
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use Psy\Exception\InterruptException;
|
||||
use Psy\Shell;
|
||||
use Psy\Util\DependencyChecker;
|
||||
|
||||
/**
|
||||
* A signal handler for interrupting execution with Ctrl-C, used when process forking is disabled.
|
||||
*/
|
||||
class SignalHandler extends AbstractListener
|
||||
{
|
||||
private bool $sigintHandlerInstalled = false;
|
||||
private bool $restoreStty = false;
|
||||
private bool $wasInterrupted = false;
|
||||
private ?string $originalStty = null;
|
||||
|
||||
public const PCNTL_FUNCTIONS = [
|
||||
'pcntl_signal',
|
||||
'pcntl_async_signals',
|
||||
];
|
||||
|
||||
public const POSIX_FUNCTIONS = [
|
||||
'posix_isatty',
|
||||
];
|
||||
|
||||
/**
|
||||
* Signal handler is supported if pcntl and posix extensions are available.
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return DependencyChecker::functionsAvailable(self::PCNTL_FUNCTIONS)
|
||||
&& DependencyChecker::functionsAvailable(self::POSIX_FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save original stty state before the REPL starts.
|
||||
*/
|
||||
public function beforeRun(Shell $shell)
|
||||
{
|
||||
if (@\posix_isatty(\STDIN)) {
|
||||
$this->originalStty = @\shell_exec('stty -g 2>/dev/null');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Install SIGINT handler before executing user code.
|
||||
*/
|
||||
public function onExecute(Shell $shell, string $code)
|
||||
{
|
||||
$this->wasInterrupted = false;
|
||||
|
||||
// Ensure signal processing is enabled so Ctrl-C can interrupt execution
|
||||
if (@\posix_isatty(\STDIN)) {
|
||||
@\shell_exec('stty isig 2>/dev/null');
|
||||
$this->restoreStty = true;
|
||||
}
|
||||
|
||||
\pcntl_async_signals(true);
|
||||
|
||||
// Install SIGINT handler that throws exception during execution
|
||||
$interrupted = &$this->wasInterrupted;
|
||||
$this->sigintHandlerInstalled = \pcntl_signal(\SIGINT, function () use (&$interrupted) {
|
||||
$interrupted = true;
|
||||
throw new InterruptException('Ctrl+C');
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called at the end of each loop.
|
||||
*
|
||||
* Restores terminal state and clears stdin if execution was interrupted.
|
||||
*/
|
||||
public function afterLoop(Shell $shell)
|
||||
{
|
||||
// Restore default SIGINT handler after execution
|
||||
if ($this->sigintHandlerInstalled) {
|
||||
\pcntl_signal(\SIGINT, \SIG_DFL);
|
||||
$this->sigintHandlerInstalled = false;
|
||||
}
|
||||
|
||||
// Restore terminal to raw mode after execution
|
||||
// This prevents Ctrl-C at the prompt from generating SIGINT
|
||||
if ($this->restoreStty) {
|
||||
@\shell_exec('stty -isig 2>/dev/null');
|
||||
$this->restoreStty = false;
|
||||
}
|
||||
|
||||
// Clear any pending input from the interrupted stdin stream
|
||||
// The SIGINT may have left the stream in a bad state
|
||||
if ($this->wasInterrupted && \defined('STDIN') && \is_resource(\STDIN)) {
|
||||
// Check if the stream is still usable
|
||||
$meta = @\stream_get_meta_data(\STDIN);
|
||||
if ($meta && !($meta['eof'] ?? false)) {
|
||||
// Drain any buffered input, suppressing I/O errors
|
||||
@\stream_set_blocking(\STDIN, false);
|
||||
while (@\fgetc(\STDIN) !== false) {
|
||||
}
|
||||
@\stream_set_blocking(\STDIN, true);
|
||||
}
|
||||
$this->wasInterrupted = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore original terminal state when the REPL exits.
|
||||
*/
|
||||
public function afterRun(Shell $shell, int $exitCode = 0)
|
||||
{
|
||||
if ($this->originalStty !== null) {
|
||||
@\shell_exec('stty '.\escapeshellarg(\trim($this->originalStty)).' 2>/dev/null');
|
||||
}
|
||||
}
|
||||
}
|
||||
305
vendor/psy/psysh/src/ExecutionLoop/UopzReloader.php
vendored
Normal file
305
vendor/psy/psysh/src/ExecutionLoop/UopzReloader.php
vendored
Normal file
@@ -0,0 +1,305 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\Parser;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use Psy\ConfigPaths;
|
||||
use Psy\Exception\ParseErrorException;
|
||||
use Psy\OutputAware;
|
||||
use Psy\ParserFactory;
|
||||
use Psy\Shell;
|
||||
use Psy\Util\DependencyChecker;
|
||||
use Psy\Util\Docblock;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* A uopz-based code reloader for modern PHP.
|
||||
*
|
||||
* This reloader uses the uopz extension to dynamically reload modified files
|
||||
* without restarting the REPL session. It parses changed files and uses uopz
|
||||
* functions to override methods, functions, and constants.
|
||||
*
|
||||
* Reload flow:
|
||||
* 1. On each input, check included files for timestamp changes
|
||||
* 2. Parse modified files and reload safe elements (methods, unconditional functions)
|
||||
* 3. Skip unsafe elements (conditional functions/constants) and track in skippedFiles
|
||||
* 4. When `yolo` command enables force-reload, re-process skipped files with
|
||||
* safety checks bypassed
|
||||
*
|
||||
* Known limitations:
|
||||
* - Cannot add/remove class properties
|
||||
* - Cannot change class inheritance or interfaces
|
||||
* - Cannot change method signatures (parameter types/counts)
|
||||
*
|
||||
* However, it can:
|
||||
* - Reload method implementations (including private/protected)
|
||||
* - Reload function implementations
|
||||
* - Reload class and global constants
|
||||
* - Add new methods and functions
|
||||
*/
|
||||
class UopzReloader extends AbstractListener implements OutputAware
|
||||
{
|
||||
private Parser $parser;
|
||||
private PrettyPrinter\Standard $printer;
|
||||
private ?OutputInterface $output = null;
|
||||
private ?Shell $shell = null;
|
||||
|
||||
/** @var array<string, int> File path => last processed timestamp */
|
||||
private array $timestamps = [];
|
||||
|
||||
/**
|
||||
* File paths with skipped elements, awaiting force-reload via yolo.
|
||||
*
|
||||
* @var array<string, int> File path => last processed timestamp
|
||||
*/
|
||||
private array $skippedFiles = [];
|
||||
|
||||
/** @var bool Whether to bypass safety warnings (set by yolo command) */
|
||||
private bool $forceReload = false;
|
||||
|
||||
/**
|
||||
* Only enabled if uopz extension is installed with required functions.
|
||||
*
|
||||
* Requires uopz 5.0+ which provides uopz_set_return() and uopz_redefine().
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return \extension_loaded('uopz') && DependencyChecker::functionsAvailable([
|
||||
'uopz_set_return',
|
||||
'uopz_redefine',
|
||||
'uopz_unset_return',
|
||||
'uopz_undefine',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Uopz Reloader.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->parser = (new ParserFactory())->createParser();
|
||||
$this->printer = new PrettyPrinter\Standard();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): void
|
||||
{
|
||||
$this->output = $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable force-reload mode.
|
||||
*
|
||||
* When enabled, safety checks are bypassed and any pending skipped files
|
||||
* are immediately re-processed.
|
||||
*/
|
||||
public function setForceReload(bool $force)
|
||||
{
|
||||
$this->forceReload = $force;
|
||||
|
||||
// Re-process any skipped files now that force-reload is enabled
|
||||
if ($force && !empty($this->skippedFiles) && $this->shell !== null) {
|
||||
$this->reloadSkippedFiles();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-process files that were previously skipped.
|
||||
*/
|
||||
private function reloadSkippedFiles(): void
|
||||
{
|
||||
$files = $this->skippedFiles;
|
||||
$this->skippedFiles = [];
|
||||
|
||||
if (\count($files) === 1) {
|
||||
$this->writeInfo(\sprintf('YOLO: Force-reloading %s', ConfigPaths::prettyPath(\array_key_first($files))));
|
||||
} else {
|
||||
$this->writeInfo(\sprintf('YOLO: Force-reloading %d files', \count($files)));
|
||||
}
|
||||
|
||||
foreach ($files as $file => $timestamp) {
|
||||
$this->reloadFile($file);
|
||||
$this->timestamps[$file] = $timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload code on input.
|
||||
*/
|
||||
public function onInput(Shell $shell, string $input)
|
||||
{
|
||||
$this->shell = $shell;
|
||||
$this->reload();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look through included files and update anything with a new timestamp.
|
||||
*/
|
||||
private function reload(): void
|
||||
{
|
||||
\clearstatcache();
|
||||
$modified = [];
|
||||
|
||||
foreach (\get_included_files() as $file) {
|
||||
// Skip files that no longer exist
|
||||
if (!\file_exists($file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$timestamp = \filemtime($file);
|
||||
|
||||
if (!isset($this->timestamps[$file])) {
|
||||
$this->timestamps[$file] = $timestamp;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->timestamps[$file] === $timestamp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->lintFile($file)) {
|
||||
$this->writeError(\sprintf('Modified file "%s" has syntax errors and cannot be reloaded', ConfigPaths::prettyPath($file)));
|
||||
continue;
|
||||
}
|
||||
|
||||
$modified[$file] = $timestamp;
|
||||
}
|
||||
|
||||
if (\count($modified) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear magic method/property cache since docblocks may have changed
|
||||
Docblock::clearMagicCache();
|
||||
|
||||
// Notify user about reload attempts
|
||||
if ($this->forceReload) {
|
||||
if (\count($modified) === 1) {
|
||||
$this->writeInfo(\sprintf('YOLO: Force-reloading %s', ConfigPaths::prettyPath(\array_key_first($modified))));
|
||||
} else {
|
||||
$this->writeInfo(\sprintf('YOLO: Force-reloading %d files', \count($modified)));
|
||||
}
|
||||
} else {
|
||||
if (\count($modified) === 1) {
|
||||
$this->writeInfo(\sprintf('Reloading %s', ConfigPaths::prettyPath(\array_key_first($modified))));
|
||||
} else {
|
||||
$this->writeInfo(\sprintf('Reloading %d files', \count($modified)));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($modified as $file => $timestamp) {
|
||||
$hadSkips = $this->reloadFile($file);
|
||||
$this->timestamps[$file] = $timestamp;
|
||||
if ($hadSkips) {
|
||||
// Track for later force-reload via yolo
|
||||
$this->skippedFiles[$file] = $timestamp;
|
||||
} else {
|
||||
unset($this->skippedFiles[$file]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload a single file by parsing it and applying uopz overrides.
|
||||
*
|
||||
* @return bool True if any elements were skipped (need yolo to force)
|
||||
*/
|
||||
private function reloadFile(string $file): bool
|
||||
{
|
||||
try {
|
||||
$code = \file_get_contents($file);
|
||||
$ast = $this->parser->parse($code);
|
||||
|
||||
if ($ast === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$traverser = new NodeTraverser();
|
||||
$reloader = new UopzReloaderVisitor($this->printer, $this->forceReload);
|
||||
$traverser->addVisitor($reloader);
|
||||
$traverser->traverse($ast);
|
||||
|
||||
// Check if there were any warnings about limitations
|
||||
if ($reloader->hasWarnings()) {
|
||||
foreach ($reloader->getWarnings() as $warning) {
|
||||
$this->writeWarning($warning);
|
||||
}
|
||||
}
|
||||
|
||||
return $reloader->hasSkips();
|
||||
} catch (\Throwable $e) {
|
||||
$this->writeError(\sprintf('Failed to reload %s: %s', ConfigPaths::prettyPath($file), $e->getMessage()));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an info message.
|
||||
*/
|
||||
private function writeInfo(string $message): void
|
||||
{
|
||||
if ($this->output) {
|
||||
$this->output->writeln(\sprintf('<whisper>%s</whisper>', $message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a warning message.
|
||||
*/
|
||||
private function writeWarning(string $message): void
|
||||
{
|
||||
if ($this->output) {
|
||||
$this->output->writeln(\sprintf('<comment>Warning: %s</comment>', $message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an error message using shell exception handling.
|
||||
*/
|
||||
private function writeError(string $message): void
|
||||
{
|
||||
if ($this->shell) {
|
||||
try {
|
||||
$this->shell->writeException(new ParseErrorException($message));
|
||||
|
||||
return;
|
||||
} catch (\Throwable $e) {
|
||||
// Shell not fully initialized, fall back to output
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->output) {
|
||||
$this->output->writeln(\sprintf('<error>Error: %s</error>', $message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file has valid PHP syntax.
|
||||
*/
|
||||
private function lintFile(string $file): bool
|
||||
{
|
||||
try {
|
||||
$this->parser->parse(\file_get_contents($file));
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
646
vendor/psy/psysh/src/ExecutionLoop/UopzReloaderVisitor.php
vendored
Normal file
646
vendor/psy/psysh/src/ExecutionLoop/UopzReloaderVisitor.php
vendored
Normal file
@@ -0,0 +1,646 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Psy Shell.
|
||||
*
|
||||
* (c) 2012-2026 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Psy\ExecutionLoop;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* AST visitor that reloads code definitions using uopz.
|
||||
*
|
||||
* Traverses the parsed AST and uses uopz to reload:
|
||||
* - Class methods (via uopz_set_return with closure)
|
||||
* - Functions (via uopz_set_return)
|
||||
* - Class and global constants (via uopz_redefine)
|
||||
*
|
||||
* Safety checks:
|
||||
* - Conditional code (functions/constants inside if blocks) is skipped by default
|
||||
* because reloading may not match runtime conditions
|
||||
* - Static variables in functions/methods trigger a warning (state will reset)
|
||||
* - Structural changes (new properties, inheritance) cannot be applied
|
||||
*
|
||||
* When force-reload is enabled (via `yolo` command), safety checks are bypassed
|
||||
* and the code is reloaded anyway.
|
||||
*/
|
||||
class UopzReloaderVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
private PrettyPrinter\Standard $printer;
|
||||
|
||||
/** @var bool Whether to bypass safety warnings */
|
||||
private bool $forceReload;
|
||||
|
||||
private string $namespace = '';
|
||||
private ?string $currentClass = null;
|
||||
private ?string $currentFunction = null;
|
||||
|
||||
/** @var string[] Warning messages generated during traversal */
|
||||
private array $warnings = [];
|
||||
|
||||
/** @var bool Whether any elements were skipped (not force-reloaded) */
|
||||
private bool $hasSkips = false;
|
||||
|
||||
/** @var int Nesting depth inside conditional/control structures */
|
||||
private int $conditionalDepth = 0;
|
||||
|
||||
/**
|
||||
* @param bool $forceReload Whether to bypass safety warnings
|
||||
*/
|
||||
public function __construct(PrettyPrinter\Standard $printer, bool $forceReload = false)
|
||||
{
|
||||
$this->printer = $printer;
|
||||
$this->forceReload = $forceReload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any warnings were generated during reloading.
|
||||
*/
|
||||
public function hasWarnings(): bool
|
||||
{
|
||||
return \count($this->warnings) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all warnings generated during reloading.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getWarnings(): array
|
||||
{
|
||||
return $this->warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any elements were skipped during reloading.
|
||||
*/
|
||||
public function hasSkips(): bool
|
||||
{
|
||||
return $this->hasSkips;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a warning message.
|
||||
*/
|
||||
private function addWarning(string $message): void
|
||||
{
|
||||
$this->warnings[] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function enterNode(Node $node)
|
||||
{
|
||||
// Track namespace
|
||||
if ($node instanceof Stmt\Namespace_) {
|
||||
$this->namespace = $node->name ? $node->name->toString() : '';
|
||||
}
|
||||
|
||||
// Track current class and check for limitations
|
||||
if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Interface_ || $node instanceof Stmt\Trait_) {
|
||||
$name = $node->name ? $node->name->toString() : null;
|
||||
$this->currentClass = $name ? $this->getFullyQualifiedName($name) : null;
|
||||
|
||||
if ($this->currentClass) {
|
||||
$this->checkClassLimitations($this->currentClass, $node);
|
||||
}
|
||||
}
|
||||
|
||||
// Track when we enter conditional/control structures at global scope
|
||||
if ($this->currentClass === null && $this->currentFunction === null && $this->isControlStructure($node)) {
|
||||
$this->conditionalDepth++;
|
||||
}
|
||||
|
||||
// Detect side effects at global/namespace scope (but not if we're already tracking it as conditional)
|
||||
if ($this->currentClass === null && $this->currentFunction === null && $this->conditionalDepth === 0) {
|
||||
$this->checkForSideEffects($node);
|
||||
}
|
||||
|
||||
// Reload class methods
|
||||
if ($node instanceof Stmt\ClassMethod && $this->currentClass) {
|
||||
$this->reloadMethod($this->currentClass, $node);
|
||||
}
|
||||
|
||||
// Reload functions (skip if inside conditional, unless force mode)
|
||||
if ($node instanceof Stmt\Function_) {
|
||||
// Track that we're entering a function
|
||||
$this->currentFunction = $node->name->toString();
|
||||
|
||||
if ($this->conditionalDepth > 0 && $this->currentClass === null) {
|
||||
$funcName = $node->name->toString();
|
||||
$snippet = \sprintf('if (...) { function %s() ... }', $funcName);
|
||||
|
||||
if ($this->forceReload) {
|
||||
$this->addWarning(\sprintf('YOLO: Force-reloaded %s', $snippet));
|
||||
$this->reloadFunction($node);
|
||||
} else {
|
||||
$this->addWarning(\sprintf('Skipped conditional: %s (use `yolo` to force)', $snippet));
|
||||
$this->hasSkips = true;
|
||||
}
|
||||
} else {
|
||||
$this->reloadFunction($node);
|
||||
}
|
||||
}
|
||||
|
||||
// Reload constants
|
||||
if ($node instanceof Stmt\ClassConst && $this->currentClass) {
|
||||
$this->reloadClassConstants($this->currentClass, $node);
|
||||
}
|
||||
|
||||
if ($node instanceof Stmt\Const_) {
|
||||
if ($this->conditionalDepth > 0 && $this->currentClass === null) {
|
||||
$constNode = $node->consts[0] ?? null;
|
||||
$constName = $constNode ? $constNode->name->toString() : 'CONST';
|
||||
$snippet = \sprintf('if (...) { const %s = ...; }', $constName);
|
||||
|
||||
if ($this->forceReload) {
|
||||
$this->addWarning(\sprintf('YOLO: Force-reloaded %s', $snippet));
|
||||
$this->reloadGlobalConstants($node);
|
||||
} else {
|
||||
$this->addWarning(\sprintf('Skipped conditional: %s (use `yolo` to force)', $snippet));
|
||||
$this->hasSkips = true;
|
||||
}
|
||||
} else {
|
||||
$this->reloadGlobalConstants($node);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function leaveNode(Node $node)
|
||||
{
|
||||
// Clear current class when leaving class/interface/trait
|
||||
if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Interface_ || $node instanceof Stmt\Trait_) {
|
||||
$this->currentClass = null;
|
||||
}
|
||||
|
||||
// Clear current function when leaving function
|
||||
if ($node instanceof Stmt\Function_) {
|
||||
$this->currentFunction = null;
|
||||
}
|
||||
|
||||
// Track when we leave conditional/control structures at global scope
|
||||
if ($this->currentClass === null && $this->currentFunction === null && $this->isControlStructure($node)) {
|
||||
$this->conditionalDepth--;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload a class method using uopz_set_return.
|
||||
*/
|
||||
private function reloadMethod(string $className, Stmt\ClassMethod $method): void
|
||||
{
|
||||
$methodName = $method->name->toString();
|
||||
|
||||
// Skip abstract methods
|
||||
if ($method->isAbstract()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for static variables in method body
|
||||
if ($this->hasStaticVariables($method->stmts)) {
|
||||
$snippet = \sprintf('%s::%s() { static $var = ...; }', $className, $methodName);
|
||||
$this->addWarning(\sprintf('Static vars will reset: %s', $snippet));
|
||||
}
|
||||
|
||||
$closure = $this->createClosure($method->params, $method->stmts, $method->returnType);
|
||||
if ($closure !== null) {
|
||||
try {
|
||||
\uopz_set_return($className, $methodName, $closure, true);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addWarning(\sprintf('Failed to reload %s::%s(): %s', $className, $methodName, $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload a function using uopz_set_return.
|
||||
*/
|
||||
private function reloadFunction(Stmt\Function_ $function): void
|
||||
{
|
||||
$functionName = $this->getFullyQualifiedName($function->name->toString());
|
||||
|
||||
// New function; just define it via eval
|
||||
if (!\function_exists($functionName)) {
|
||||
try {
|
||||
$code = '';
|
||||
if ($this->namespace !== '') {
|
||||
$code .= 'namespace '.$this->namespace.'; ';
|
||||
}
|
||||
$code .= $this->printer->prettyPrint([$function]);
|
||||
eval($code);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addWarning(\sprintf('Failed to add %s(): %s', $functionName, $e->getMessage()));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Existing function; check for static variables (state will reset on reload)
|
||||
if ($this->hasStaticVariables($function->stmts)) {
|
||||
$snippet = \sprintf('%s() { static $var = ...; }', $functionName);
|
||||
$this->addWarning(\sprintf('Static vars will reset: %s', $snippet));
|
||||
}
|
||||
|
||||
// Use uopz to override existing function
|
||||
$closure = $this->createClosure($function->params, $function->stmts, $function->returnType);
|
||||
if ($closure !== null) {
|
||||
try {
|
||||
\uopz_set_return($functionName, $closure, true);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addWarning(\sprintf('Failed to reload %s(): %s', $functionName, $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a closure from parameters and statements.
|
||||
*
|
||||
* @param Node\Param[] $params
|
||||
* @param Stmt[]|null $stmts
|
||||
* @param Node|null $returnType
|
||||
*
|
||||
* @return \Closure|null
|
||||
*/
|
||||
private function createClosure(array $params, ?array $stmts, ?Node $returnType = null): ?\Closure
|
||||
{
|
||||
$paramStrs = [];
|
||||
foreach ($params as $param) {
|
||||
$paramStr = '';
|
||||
|
||||
if ($param->type) {
|
||||
$paramStr .= $this->printer->prettyPrint([$param->type]).' ';
|
||||
}
|
||||
|
||||
if ($param->variadic) {
|
||||
$paramStr .= '...';
|
||||
}
|
||||
|
||||
if ($param->byRef) {
|
||||
$paramStr .= '&';
|
||||
}
|
||||
|
||||
$paramStr .= '$'.$param->var->name;
|
||||
|
||||
if ($param->default) {
|
||||
$paramStr .= ' = '.$this->printer->prettyPrintExpr($param->default);
|
||||
}
|
||||
|
||||
$paramStrs[] = $paramStr;
|
||||
}
|
||||
|
||||
$paramList = \implode(', ', $paramStrs);
|
||||
|
||||
$returnTypeStr = '';
|
||||
if ($returnType !== null) {
|
||||
$returnTypeStr = ': '.$this->printer->prettyPrint([$returnType]);
|
||||
}
|
||||
|
||||
$body = '';
|
||||
if ($stmts) {
|
||||
$bodyStmts = [];
|
||||
foreach ($stmts as $stmt) {
|
||||
$bodyStmts[] = $this->printer->prettyPrint([$stmt]);
|
||||
}
|
||||
$body = \implode("\n", $bodyStmts);
|
||||
}
|
||||
|
||||
$closureCode = \sprintf("return function(%s)%s {\n%s\n};", $paramList, $returnTypeStr, $body);
|
||||
|
||||
try {
|
||||
return eval($closureCode);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload class constants using uopz_redefine.
|
||||
*/
|
||||
private function reloadClassConstants(string $className, Stmt\ClassConst $const): void
|
||||
{
|
||||
foreach ($const->consts as $constNode) {
|
||||
$constName = $constNode->name->toString();
|
||||
$value = $this->evaluateConstValue($constNode->value);
|
||||
|
||||
try {
|
||||
\uopz_redefine($className, $constName, $value);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addWarning(\sprintf('Failed to reload %s::%s: %s', $className, $constName, $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload global constants using uopz_redefine.
|
||||
*/
|
||||
private function reloadGlobalConstants(Stmt\Const_ $const): void
|
||||
{
|
||||
foreach ($const->consts as $constNode) {
|
||||
$constName = $this->getFullyQualifiedName($constNode->name->toString());
|
||||
$value = $this->evaluateConstValue($constNode->value);
|
||||
|
||||
try {
|
||||
\uopz_redefine($constName, $value);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addWarning(\sprintf('Failed to reload %s: %s', $constName, $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a constant value from AST node.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function evaluateConstValue(Expr $expr)
|
||||
{
|
||||
// For simple scalar values, we can evaluate directly
|
||||
try {
|
||||
$code = '<?php return '.$this->printer->prettyPrintExpr($expr).';';
|
||||
|
||||
return eval(\substr($code, 6));
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a fully-qualified name (class, function, constant, etc).
|
||||
*/
|
||||
private function getFullyQualifiedName(string $name): string
|
||||
{
|
||||
if ($this->namespace && \strpos($name, '\\') !== 0) {
|
||||
return $this->namespace.'\\'.$name;
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a node is a control structure.
|
||||
*/
|
||||
private function isControlStructure(Node $node): bool
|
||||
{
|
||||
return $node instanceof Stmt\If_ ||
|
||||
$node instanceof Stmt\Switch_ ||
|
||||
$node instanceof Stmt\For_ ||
|
||||
$node instanceof Stmt\Foreach_ ||
|
||||
$node instanceof Stmt\While_ ||
|
||||
$node instanceof Stmt\Do_ ||
|
||||
$node instanceof Stmt\TryCatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if statements contain static variable declarations.
|
||||
*
|
||||
* @param Stmt[]|null $stmts
|
||||
*/
|
||||
private function hasStaticVariables(?array $stmts): bool
|
||||
{
|
||||
if ($stmts === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($stmts as $stmt) {
|
||||
// Direct static declaration
|
||||
if ($stmt instanceof Stmt\Static_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Recursively check nested structures (if/for/while/etc)
|
||||
if ($stmt instanceof Stmt\If_) {
|
||||
if ($this->hasStaticVariables($stmt->stmts)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($stmt->elseifs as $elseif) {
|
||||
if ($this->hasStaticVariables($elseif->stmts)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ($stmt->else && $this->hasStaticVariables($stmt->else->stmts)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($stmt instanceof Stmt\For_ ||
|
||||
$stmt instanceof Stmt\Foreach_ ||
|
||||
$stmt instanceof Stmt\While_ ||
|
||||
$stmt instanceof Stmt\Do_) {
|
||||
if ($this->hasStaticVariables($stmt->stmts)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($stmt instanceof Stmt\Switch_) {
|
||||
foreach ($stmt->cases as $case) {
|
||||
if ($this->hasStaticVariables($case->stmts)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($stmt instanceof Stmt\TryCatch) {
|
||||
if ($this->hasStaticVariables($stmt->stmts)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($stmt->catches as $catch) {
|
||||
if ($this->hasStaticVariables($catch->stmts)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ($stmt->finally && $this->hasStaticVariables($stmt->finally->stmts)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for side effects that won't be re-executed on reload.
|
||||
*
|
||||
* Detects top-level code that has side effects (function calls, variable
|
||||
* assignments, etc.) which will not be re-run when the file is reloaded.
|
||||
*/
|
||||
private function checkForSideEffects(Node $node): void
|
||||
{
|
||||
// Skip declarations (these are handled by uopz)
|
||||
if ($node instanceof Stmt\Class_ ||
|
||||
$node instanceof Stmt\Interface_ ||
|
||||
$node instanceof Stmt\Trait_ ||
|
||||
$node instanceof Stmt\Function_ ||
|
||||
$node instanceof Stmt\Const_ ||
|
||||
$node instanceof Stmt\Namespace_ ||
|
||||
$node instanceof Stmt\Use_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only check statements, not expressions (to avoid duplicate warnings)
|
||||
// Expression statements contain the actual expression
|
||||
if ($node instanceof Stmt\Expression) {
|
||||
$expr = $node->expr;
|
||||
$snippet = $this->printer->prettyPrintExpr($expr);
|
||||
|
||||
// Truncate long snippets
|
||||
if (\strlen($snippet) > 50) {
|
||||
$snippet = \substr($snippet, 0, 47).'...';
|
||||
}
|
||||
|
||||
$this->addWarning(\sprintf('Not re-run: %s', $snippet));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Echo/print statements
|
||||
if ($node instanceof Stmt\Echo_) {
|
||||
$firstExpr = $node->exprs[0] ?? null;
|
||||
$snippet = $firstExpr !== null
|
||||
? 'echo '.$this->printer->prettyPrintExpr($firstExpr)
|
||||
: 'echo ...';
|
||||
if (\strlen($snippet) > 50) {
|
||||
$snippet = \substr($snippet, 0, 47).'...';
|
||||
}
|
||||
$this->addWarning(\sprintf('Not re-run: %s', $snippet));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Global variable declarations
|
||||
if ($node instanceof Stmt\Global_) {
|
||||
$varNames = [];
|
||||
foreach ($node->vars as $var) {
|
||||
if ($var instanceof Expr\Variable) {
|
||||
$varNames[] = '$'.$var->name;
|
||||
}
|
||||
}
|
||||
$snippet = 'global '.\implode(', ', $varNames);
|
||||
$this->addWarning(\sprintf('Not re-run: %s', $snippet));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Static variable declarations (inside functions are OK, but top-level would be unusual)
|
||||
if ($node instanceof Stmt\Static_) {
|
||||
$varNames = [];
|
||||
foreach ($node->vars as $var) {
|
||||
$varNames[] = '$'.$var->var->name;
|
||||
}
|
||||
$snippet = 'static '.\implode(', ', $varNames);
|
||||
$this->addWarning(\sprintf('Not re-run: %s', $snippet));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If/switch/for/while/etc control structures at top level
|
||||
if ($this->isControlStructure($node)) {
|
||||
$type = 'if';
|
||||
if ($node instanceof Stmt\Switch_) {
|
||||
$type = 'switch';
|
||||
} elseif ($node instanceof Stmt\For_) {
|
||||
$type = 'for';
|
||||
} elseif ($node instanceof Stmt\Foreach_) {
|
||||
$type = 'foreach';
|
||||
} elseif ($node instanceof Stmt\While_) {
|
||||
$type = 'while';
|
||||
} elseif ($node instanceof Stmt\Do_) {
|
||||
$type = 'do-while';
|
||||
} elseif ($node instanceof Stmt\TryCatch) {
|
||||
$type = 'try-catch';
|
||||
}
|
||||
|
||||
$this->addWarning(\sprintf('Not re-run: %s (...) { ... }', $type));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for known limitations when reloading a class.
|
||||
*/
|
||||
private function checkClassLimitations(string $className, Node $node): void
|
||||
{
|
||||
// Check if class already exists
|
||||
if (!\class_exists($className, false) && !\interface_exists($className, false) && !\trait_exists($className, false)) {
|
||||
// New class/interface/trait - uopz cannot add these
|
||||
$type = $node instanceof Stmt\Interface_ ? 'interface' : ($node instanceof Stmt\Trait_ ? 'trait' : 'class');
|
||||
$this->addWarning(\sprintf('Cannot add %s %s', $type, $className));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// For existing classes, check for structural changes
|
||||
if (!($node instanceof Stmt\Class_)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for new properties (cannot be added)
|
||||
foreach ($node->stmts as $stmt) {
|
||||
if ($stmt instanceof Stmt\Property) {
|
||||
foreach ($stmt->props as $prop) {
|
||||
$propName = $prop->name->toString();
|
||||
if (!\property_exists($className, $propName)) {
|
||||
$visibility = $stmt->isPublic() ? 'public' : ($stmt->isProtected() ? 'protected' : 'private');
|
||||
$static = $stmt->isStatic() ? 'static ' : '';
|
||||
$this->addWarning(\sprintf('Cannot add %s$%s', $static.$visibility.' ', $propName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for new methods (will try to add but may fail silently)
|
||||
if ($stmt instanceof Stmt\ClassMethod) {
|
||||
$methodName = $stmt->name->toString();
|
||||
if (!\method_exists($className, $methodName)) {
|
||||
$this->addWarning(\sprintf('Cannot add %s::%s()', $className, $methodName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for inheritance changes
|
||||
if ($node->extends) {
|
||||
$newParent = $node->extends->toString();
|
||||
$reflection = new ReflectionClass($className);
|
||||
$currentParent = $reflection->getParentClass();
|
||||
|
||||
if ($currentParent && $currentParent->getName() !== $this->getFullyQualifiedName($newParent)) {
|
||||
$this->addWarning(\sprintf('Cannot change parent of %s', $className));
|
||||
}
|
||||
}
|
||||
|
||||
// Check for interface changes
|
||||
if ($node->implements) {
|
||||
$newInterfaces = \array_map(function ($interface) {
|
||||
return $this->getFullyQualifiedName($interface->toString());
|
||||
}, $node->implements);
|
||||
|
||||
$reflection = new ReflectionClass($className);
|
||||
$currentInterfaces = $reflection->getInterfaceNames();
|
||||
|
||||
$added = \array_diff($newInterfaces, $currentInterfaces);
|
||||
$removed = \array_diff($currentInterfaces, $newInterfaces);
|
||||
|
||||
if (\count($added) > 0 || \count($removed) > 0) {
|
||||
$this->addWarning(\sprintf('Cannot change interfaces of %s', $className));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user