diff --git a/composer.json b/composer.json index 0dcd2cc..70f9c10 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,16 @@ "name": "kpicaza" } ], + "repositories": [ + { + "type": "github", + "url": "git@github.com:WyriHaximus-labs/filesystem.git" + } + ], "require": { "php": ">=8.1", "monolog/monolog": "^2.3", + "react/filesystem": "rewrite-dev", "webmozart/assert": "^1.10" }, "require-dev": { diff --git a/src/ReactFilesystemHandler.php b/src/ReactFilesystemHandler.php new file mode 100644 index 0000000..a58f04f --- /dev/null +++ b/src/ReactFilesystemHandler.php @@ -0,0 +1,69 @@ +loop = $loop; + $this->logPath = $logPath; + $filesystem = Factory::create(); + /** @psalm-suppress TooManyTemplateParams */ + $this->filesystem = $filesystem->detect($this->logPath) + ->then(static function (NotExistInterface $node): PromiseInterface { + /** @psalm-suppress TooManyTemplateParams */ + return $node->createFile(); + }); + + parent::__construct($debug, $bubble); + } + + /** + * @param FormattedRecord $record + */ + protected function write(array $record): void + { + $this->loop->futureTick(function () use ($record) { + $this->filesystem + ->then(static function (FileInterface $file) use ($record): PromiseInterface { + $formatted = $record['formatted']; + Assert::string($formatted); + /** @psalm-suppress TooManyTemplateParams */ + return $file->putContents($formatted, FILE_APPEND); + }); + }); + } + + public function close(): void + { + if ($this->filesystem instanceof ExtendedPromiseInterface) { + $this->filesystem->done(); + } + } +} diff --git a/test/ReactFileSystemHandlerTest.php b/test/ReactFileSystemHandlerTest.php new file mode 100644 index 0000000..ad5f714 --- /dev/null +++ b/test/ReactFileSystemHandlerTest.php @@ -0,0 +1,32 @@ +pushHandler($handler); + $logger->debug('Hola Mundo'); + $loop->run(); + $log = file_get_contents(self::LOG_PATH); + self::assertStringContainsString('Hola Mundo', $log); + } +}