diff --git a/src/Client.php b/src/Client.php index 11c43c0..74b460a 100755 --- a/src/Client.php +++ b/src/Client.php @@ -17,7 +17,6 @@ use Ripple\Channel\Channel; use Ripple\File\Lock; use Ripple\Utils\Output; -use Throwable; use function base_path; use function cli_set_process_title; @@ -27,6 +26,7 @@ use function Co\wait; use function config_path; use function file_exists; +use function fwrite; use function shell_exec; use function sprintf; use function storage_path; @@ -35,21 +35,22 @@ use const SIGINT; use const SIGQUIT; use const SIGTERM; +use const STDOUT; class Client { /*** @var \Laravel\Ripple\Inspector */ public readonly Inspector $inspector; - /*** @var \Laravel\Ripple\Virtual\Virtual */ - public readonly Virtual $virtual; - /*** @var \Ripple\Channel\Channel */ public readonly Channel $channel; /*** @var \Ripple\File\Lock */ public readonly Lock $lock; + /*** @var \Laravel\Ripple\Virtual\Virtual */ + public Virtual $virtual; + /*** @var bool */ public bool $owner = false; @@ -97,7 +98,7 @@ public function start(bool $daemon = false): void $this->lock->exclusion(false); $this->virtual = new Virtual(__DIR__ . '/Virtual/server.bin.php'); $this->virtual->launch(); - $this->virtual->session->onMessage = static fn (string $content) => Output::writeln($content); + $this->virtual->session->onMessage = static fn (string $content) => fwrite(STDOUT, $content); $this->virtual->session->onErrorMessage = static fn (string $content) => Output::error($content); $this->channel = channel(base_path(), true); @@ -107,37 +108,20 @@ public function start(bool $daemon = false): void $command = $this->channel->receive(); switch ($command) { case 'stop': - $this->virtual->channel->send('stop'); - try { - \Co\sleep(0.1); - if ($this->virtual->session->getStatus('running')) { - \Co\sleep(1); - $this->virtual->session->inputSignal(SIGINT); - } - } catch (Throwable) { - } + $this->virtual->stop(); exit(0); break; case 'reload': - $oldVirtual = $this->virtual; + fwrite(STDOUT, "\033c"); + $oldVirtual = $this->virtual; $virtual = new Virtual(__DIR__ . '/Virtual/server.bin.php'); $virtual->launch(); $this->virtual = $virtual; - $this->virtual->session->onMessage = static fn (string $content) => Output::writeln($content); + $this->virtual->session->onMessage = static fn (string $content) => fwrite(STDOUT, $content); $this->virtual->session->onErrorMessage = static fn (string $content) => Output::error($content); - $oldVirtual->channel->send('stop'); - - try { - \Co\sleep(0.1); - if ($oldVirtual->session->getStatus('running')) { - \Co\sleep(1); - $oldVirtual->session->inputSignal(SIGINT); - } - } catch (Throwable) { - } - + $oldVirtual->stop(); break; } } diff --git a/src/Octane/Bin/ripple-server.bin.php b/src/Octane/Bin/ripple-server.bin.php index 13a8f06..11baf07 100755 --- a/src/Octane/Bin/ripple-server.bin.php +++ b/src/Octane/Bin/ripple-server.bin.php @@ -18,6 +18,7 @@ use Laravel\Ripple\Factory; use Laravel\Ripple\HttpWorker; use Laravel\Ripple\Octane\RippleClient; +use Laravel\Ripple\Util; use Revolt\EventLoop\UnsupportedFeatureException; use Ripple\Http\Server\Request; use Ripple\Utils\Output; @@ -74,11 +75,14 @@ $includedFiles = \get_included_files(); $hotReload = static function (string $file) use ($manager, $includedFiles, $projectChannel) { if (\in_array($file, $includedFiles, true)) { + \fwrite(\STDOUT, "\033c"); $projectChannel->send('reload'); } else { $manager->reload(); $date = \date('Y-m-d H:i:s'); - \is_file($file) && Output::writeln("[{$date}] {$file} has been modified"); + \is_file($file) + && ($file = Util::getRelativePath($file, RIP_PROJECT_PATH)) + && Output::writeln("[{$date}] {$file} has been modified"); } }; diff --git a/src/Octane/Bin/ripple-ware.bin.php b/src/Octane/Bin/ripple-ware.bin.php index 86e0f38..6188c3d 100755 --- a/src/Octane/Bin/ripple-ware.bin.php +++ b/src/Octane/Bin/ripple-ware.bin.php @@ -51,29 +51,23 @@ $virtual = new Virtual($imagePath); $virtual->launch(); -$virtual->session->onMessage = static fn (string $content) => Output::writeln($content); +$virtual->session->onMessage = static fn (string $content) => \fwrite(\STDOUT, $content); $virtual->session->onErrorMessage = static fn (string $content) => Output::error($content); $virtualStop = static function () use (&$virtual) { - $virtual->channel->send('stop'); - try { - \Co\sleep(0.1); - if ($virtual->session->getStatus('running')) { - \Co\sleep(1); - $virtual->session->inputSignal(\SIGINT); - } - } catch (Throwable) { - } + $virtual->stop(); exit(0); }; $virtualReboot = static function () use (&$virtual, $imagePath) { + \fwrite(\STDOUT, "\033c"); + $oldVirtual = $virtual; $_virtual = new Virtual($imagePath); $_virtual->launch(); $oldVirtual->channel->send('stop'); $virtual = $_virtual; - $virtual->session->onMessage = static fn (string $content) => Output::writeln($content); + $virtual->session->onMessage = static fn (string $content) => \fwrite(\STDOUT, $content); $virtual->session->onErrorMessage = static fn (string $content) => Output::error($content); }; diff --git a/src/Octane/Commands/RippleStartCommand.php b/src/Octane/Commands/RippleStartCommand.php index 5596e4e..dce122e 100755 --- a/src/Octane/Commands/RippleStartCommand.php +++ b/src/Octane/Commands/RippleStartCommand.php @@ -88,8 +88,8 @@ public function handle(RippleServerProcessInspector $inspector): int 'RIP_PROJECT_PATH' => base_path(), 'RIP_BIN_WORKING_PATH' => $binPath, 'APP_BASE_PATH' => base_path(), - 'RIP_HOST' => $this->option('host') ?? '127.0.0.1', - 'RIP_PORT' => $this->option('port') ?? 8000, + 'RIP_HOST' => $this->getHost(), + 'RIP_PORT' => $this->getPort(), 'RIP_WORKERS' => $workers, 'RIP_RELOAD' => $watch ?? 0 ] diff --git a/src/Util.php b/src/Util.php new file mode 100644 index 0000000..f63cf0f --- /dev/null +++ b/src/Util.php @@ -0,0 +1,52 @@ +id); + putenv('RIP_RELOAD=' . Config::get('ripple.HTTP_RELOAD', '')); foreach (getenv() as $key => $value) { putenv("{$key}={$value}"); } - $session = proc(PHP_BINARY); - $session->write(file_get_contents($this->virtualPath)); - $session->inputEot(); + $launch = fn () => proc([PHP_BINARY, $this->virtualPath]); + + $session = $launch(); + $session->onClose = function () use ($launch, $session) { + unset($session->onClose); + $this->session = $launch(); + }; return $this->session = $session; } + /** + * @return void + */ + public function stop(): void + { + unset($this->session->onClose); + $this->channel->send('stop'); + try { + \Co\sleep(0.1); + if ($this->session->getStatus('running')) { + \Co\sleep(1); + $this->session->inputSignal(SIGINT); + } + } catch (Throwable) { + } + } + /** * */ diff --git a/src/Virtual/server.bin.php b/src/Virtual/server.bin.php index 953a8cf..962f59b 100755 --- a/src/Virtual/server.bin.php +++ b/src/Virtual/server.bin.php @@ -17,6 +17,7 @@ use Illuminate\Support\Facades\Config; use Laravel\Ripple\Factory; use Laravel\Ripple\HttpWorker; +use Laravel\Ripple\Util; use Revolt\EventLoop\UnsupportedFeatureException; use Ripple\Utils\Output; use Ripple\Worker\Manager; @@ -29,6 +30,8 @@ \cli_set_process_title('laravel-virtual'); \define("RIP_PROJECT_PATH", \realpath(\getenv('RIP_PROJECT_PATH'))); \define("RIP_VIRTUAL_ID", \getenv('RIP_VIRTUAL_ID')); +\define("RIP_RELOAD", \getenv('RIP_RELOAD')); + require RIP_PROJECT_PATH . '/vendor/autoload.php'; /*** loadDeferredProviders */ @@ -48,7 +51,7 @@ $application, \strval(Config::get('ripple.HTTP_LISTEN', 'http://127.0.0.1:8008')), \intval(Config::get('ripple.HTTP_WORKER_COUNT', 1)), - \boolval(Config::get('ripple.HTTP_RELOAD', true)) + \boolval(RIP_RELOAD) )); /*** Hot reload part */ @@ -60,7 +63,9 @@ } else { $manager->reload(); $date = \date('Y-m-d H:i:s'); - \is_file($file) && Output::writeln("[{$date}] {$file} has been modified"); + \is_file($file) + && ($file = Util::getRelativePath($file, RIP_PROJECT_PATH)) + && Output::writeln("[{$date}] {$file} has been modified"); } }; $hotReloadWatch = Factory::createMonitor(); @@ -68,6 +73,9 @@ $hotReloadWatch->onTouch = $hotReload; $hotReloadWatch->onRemove = $hotReload; $hotReloadWatch->run(); +if (\boolval(RIP_RELOAD)) { + $hotReloadWatch->run(); +} /*** Guardian part*/ async(function () use ($manager) {