From dccd89d4a3633b982689b43828708c68e1348e0d Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 5 Apr 2024 09:44:20 +0400 Subject: [PATCH] ajouter out:: --- nur_src/cli/Application.php | 2 +- src/DataException.php | 19 ------------------ src/NoMoreDataException.php | 10 ++++++++++ src/os/EOFException.php | 6 +----- src/os/file/Stream.php | 18 ++++++++++++----- src/output/out.php | 33 +++++++++++++++++++++++++++++++ src/php/iter/AbstractIterator.php | 11 ++++++----- 7 files changed, 64 insertions(+), 35 deletions(-) delete mode 100644 src/DataException.php create mode 100644 src/NoMoreDataException.php create mode 100644 src/output/out.php diff --git a/nur_src/cli/Application.php b/nur_src/cli/Application.php index 10aa950..f48919d 100644 --- a/nur_src/cli/Application.php +++ b/nur_src/cli/Application.php @@ -16,7 +16,7 @@ use nur\path; abstract class Application { protected static function _app_init(): void { config::set_fact(config::FACT_CLI_APP); - \nulib\output\msg::set_messenger_class(\nulib\output\std\StdMessenger::class); + \nur\sery\output\msg::set_messenger_class(\nur\sery\output\std\StdMessenger::class); msg::set_messenger_class(Console::class); msg::get()->setParametrableParams([ diff --git a/src/DataException.php b/src/DataException.php deleted file mode 100644 index ec4089e..0000000 --- a/src/DataException.php +++ /dev/null @@ -1,19 +0,0 @@ -getResource(); - return EOFException::ensure_not_eof(fgets($fd, $length), $this->throwOnError); + if ($length === null) $r = fgets($fd); + else $r = fgets($fd, $length); + return EOFException::ensure_not_eof($r, $this->throwOnError); } /** @throws IOException */ @@ -234,11 +237,16 @@ class Stream extends AbstractIterator implements IReader, IWriter { } protected function _next(&$key) { - return $this->fgets(); + try { + return $this->fgets(); + } catch (EOFException $e) { + throw new NoMoreDataException(); + } } protected function _teardown(): void { - $this->fseek(0); + $md = stream_get_meta_data($this->fd); + if ($md["seekable"]) $this->fseek(0); } ############################################################################# diff --git a/src/output/out.php b/src/output/out.php new file mode 100644 index 0000000..7430935 --- /dev/null +++ b/src/output/out.php @@ -0,0 +1,33 @@ +resetParams($params); + return self::$out; + } + + static function get(): StdOutput { + if (self::$out !== null) return self::$out; + else return self::reset(); + } + + static function write(...$values): void { self::get()->write(...$values); } + static function print(...$values): void { self::get()->print(...$values); } + + static function iwrite(int $indentLevel, ...$values): void { self::get()->iwrite($indentLevel, ...$values); } + static function iprint(int $indentLevel, ...$values): void { self::get()->iprint($indentLevel, ...$values); } +} diff --git a/src/php/iter/AbstractIterator.php b/src/php/iter/AbstractIterator.php index ef11844..b74e24e 100644 --- a/src/php/iter/AbstractIterator.php +++ b/src/php/iter/AbstractIterator.php @@ -3,7 +3,7 @@ namespace nur\sery\php\iter; use Exception; use Iterator; -use nur\sery\DataException; +use nur\sery\NoMoreDataException; use nur\sery\php\ICloseable; /** @@ -29,12 +29,13 @@ abstract class AbstractIterator implements Iterator, ICloseable { protected function beforeIter() {} /** - * retourner le prochain élément. lancer l'exception {@link DataException} pour - * indiquer que plus aucun élément n'est disponible + * retourner le prochain élément. + * lancer l'exception {@link NoMoreDataException} pour indiquer que plus aucun + * élément n'est disponible * * le cas échéant, initialiser $key * - * @throws DataException + * @throws NoMoreDataException */ abstract protected function _next(&$key); @@ -94,7 +95,7 @@ abstract class AbstractIterator implements Iterator, ICloseable { $this->valid = false; try { $item = $this->_next($key); - } catch (DataException $e) { + } catch (NoMoreDataException $e) { $this->beforeClose(); try { $this->_teardown();