ajouter out::

This commit is contained in:
Jephté Clain 2024-04-05 09:44:20 +04:00
parent 6c7cef322d
commit dccd89d4a3
7 changed files with 64 additions and 35 deletions

View File

@ -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([

View File

@ -1,19 +0,0 @@
<?php
namespace nur\sery;
use RuntimeException;
/**
* Class DataException: exception générique concernant l'accès à des données
*/
class DataException extends RuntimeException {
static final function no_more_data(): self {
return new self("no more data");
}
static final function ensure_not_eof($data, bool $throw=true, $eof=false) {
if (!$throw) return null;
elseif ($data !== $eof) return $data;
else throw self::no_more_data();
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace nur\sery;
use RuntimeException;
/**
* Class NoMoreDataException: indiquer que plus aucune donnée n'est disponible
*/
class NoMoreDataException extends RuntimeException {
}

View File

@ -6,13 +6,9 @@ namespace nur\sery\os;
* flux
*/
class EOFException extends IOException {
static final function no_more_data(): self {
return new self("no more data");
}
static final function ensure_not_eof($data, bool $throw=true, $eof=false) {
if (!$throw) return null;
elseif ($data !== $eof) return $data;
else throw self::no_more_data();
else throw new self();
}
}

View File

@ -1,11 +1,12 @@
<?php
namespace nur\sery\os\file;
use nur\sery\str;
use nur\sery\ValueException;
use nur\sery\NoMoreDataException;
use nur\sery\os\EOFException;
use nur\sery\os\IOException;
use nur\sery\php\iter\AbstractIterator;
use nur\sery\str;
use nur\sery\ValueException;
/**
* Class Stream: lecture/écriture générique dans un flux
@ -149,7 +150,9 @@ class Stream extends AbstractIterator implements IReader, IWriter {
*/
function fgets(?int $length=null): string {
$fd = $this->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);
}
#############################################################################

33
src/output/out.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace nur\sery\output;
use nur\sery\output\std\StdOutput;
/**
* Class out: affichage sur la sortie standard
*/
class out {
/** @var StdOutput */
protected static $out;
/** reparamétrer l'instance */
static function reset($output=null, ?array $params=null): StdOutput {
if (self::$out === null) {
return self::$out = new StdOutput($output, $params);
}
if ($output !== null) $params["output"] = $output;
self::$out->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); }
}

View File

@ -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();