Skip to content

Commit

Permalink
Optimize terminal layout
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Jan 25, 2025
1 parent da927b3 commit d3095a0
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 48 deletions.
38 changes: 11 additions & 27 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Octane/Bin/ripple-server.bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
};

Expand Down
16 changes: 5 additions & 11 deletions src/Octane/Bin/ripple-ware.bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
4 changes: 2 additions & 2 deletions src/Octane/Commands/RippleStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
52 changes: 52 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/**
* Copyright © 2024 cclilshy
* Email: jingnigg@gmail.com
*
* This software is licensed under the MIT License.
* For full license details, please visit: https://opensource.org/licenses/MIT
*
* By using this software, you agree to the terms of the license.
* Contributions, suggestions, and feedback are always welcome!
*/

namespace Laravel\Ripple;

use Throwable;

use function realpath;
use function rtrim;
use function str_starts_with;
use function strlen;
use function substr;

use const DIRECTORY_SEPARATOR;

class Util
{
/**
* @param $path1
* @param $path2
*
* @return string
*/
public static function getRelativePath($path1, $path2): string
{
try {
$path1 = rtrim(realpath($path1), DIRECTORY_SEPARATOR);
$path2 = rtrim(realpath($path2), DIRECTORY_SEPARATOR);

if (!$path1 || !$path2) {
return '';
}

if (str_starts_with($path1, $path2)) {
return substr($path1, strlen($path2) + 1);
}
} catch (Throwable $e) {
return '';
}

return '';
}
}
34 changes: 29 additions & 5 deletions src/Virtual/Virtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@

namespace Laravel\Ripple\Virtual;

use Illuminate\Support\Facades\Config;
use Ripple\Channel\Channel;
use Ripple\Proc\Session;
use Throwable;

use function base_path;
use function Co\proc;
use function file_get_contents;
use function getenv;
use function putenv;
use function uniqid;
use function getenv;

use const PHP_BINARY;
use const SIGINT;

class Virtual
{
Expand Down Expand Up @@ -52,17 +54,39 @@ public function launch(): Session
putenv('RIP_PROJECT_PATH=' . base_path());
putenv('RIP_BIN_WORKING_PATH=' . base_path());
putenv('RIP_VIRTUAL_ID=' . $this->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) {
}
}

/**
*
*/
Expand Down
12 changes: 10 additions & 2 deletions src/Virtual/server.bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -60,14 +63,19 @@
} 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();
$hotReloadWatch->onModify = $hotReload;
$hotReloadWatch->onTouch = $hotReload;
$hotReloadWatch->onRemove = $hotReload;
$hotReloadWatch->run();
if (\boolval(RIP_RELOAD)) {
$hotReloadWatch->run();
}

/*** Guardian part*/
async(function () use ($manager) {
Expand Down

0 comments on commit d3095a0

Please sign in to comment.