eee
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
use Kiri\Server\ServerInterface;
|
||||
use Swoole\Process;
|
||||
use Kiri\Server\Processes\AbstractProcess;
|
||||
use function GuzzleHttp\Psr7\uri_for;
|
||||
|
||||
class HotReload extends AbstractProcess
|
||||
{
|
||||
|
||||
|
||||
protected mixed $pipe;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_coroutine = false;
|
||||
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $enable_queue = false;
|
||||
|
||||
|
||||
protected bool $reloading = false;
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'custom.process';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function onSigterm(): void
|
||||
{
|
||||
// TODO: Implement onSigterm() method.
|
||||
fclose(inotify_init());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ?Process $process
|
||||
*/
|
||||
public function process(Process|null $process): void
|
||||
{
|
||||
$this->pipe = inotify_init();
|
||||
$this->readDirectory(APP_PATH . '/app/*');
|
||||
$this->readDirectory(APP_PATH . '/routes/*');
|
||||
|
||||
while (!$this->isStop()) {
|
||||
$read = inotify_read($this->pipe);
|
||||
|
||||
foreach ($read as $item) {
|
||||
|
||||
$this->reWatch($item['name']);
|
||||
|
||||
}
|
||||
|
||||
$this->reload();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function reload(): void
|
||||
{
|
||||
$this->reloading = true;
|
||||
|
||||
di(ServerInterface::class)->reload();
|
||||
|
||||
$this->reloading = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $directory
|
||||
* @return void
|
||||
*/
|
||||
public function readFile(string $directory): void
|
||||
{
|
||||
if (str_ends_with($directory, '.php') === true) {
|
||||
inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $directory
|
||||
* @return void
|
||||
*/
|
||||
public function reWatch(string $directory): void
|
||||
{
|
||||
if (str_ends_with($directory, '.php') === true) {
|
||||
inotify_rm_watch($this->pipe, $directory);
|
||||
|
||||
inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $directory
|
||||
* @return void
|
||||
*/
|
||||
public function readDirectory(string $directory): void
|
||||
{
|
||||
foreach (glob($directory . '/*') as $data) {
|
||||
if (str_ends_with($data, '.php') === false) {
|
||||
continue;
|
||||
}
|
||||
if (is_dir($data)) {
|
||||
$this->readFile($data . DIRECTORY_SEPARATOR . '*');
|
||||
} else {
|
||||
$this->readFile($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user