modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									6441d82e0d
								
							
						
					
					
						commit
						b9dab7a67b
					
				@ -6,7 +6,7 @@ use nur\sery\os\IOException;
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Class FileReader: un fichier accédé en lecture
 | 
					 * Class FileReader: un fichier accédé en lecture
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class FileReader extends Stream {
 | 
					class FileReader extends _File {
 | 
				
			||||||
  const DEFAULT_MODE = "rb";
 | 
					  const DEFAULT_MODE = "rb";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function __construct($input, ?string $mode=null, bool $throwOnError=true, ?bool $allowLocking=null) {
 | 
					  function __construct($input, ?string $mode=null, bool $throwOnError=true, ?bool $allowLocking=null) {
 | 
				
			||||||
@ -17,25 +17,13 @@ class FileReader extends Stream {
 | 
				
			|||||||
      $fd = $input;
 | 
					      $fd = $input;
 | 
				
			||||||
      $close = false;
 | 
					      $close = false;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
 | 
					      $file = $input;
 | 
				
			||||||
      if ($mode === null) $mode = static::DEFAULT_MODE;
 | 
					      if ($mode === null) $mode = static::DEFAULT_MODE;
 | 
				
			||||||
      $this->file = $input;
 | 
					      $this->file = $file;
 | 
				
			||||||
      $this->mode = $mode;
 | 
					      $this->mode = $mode;
 | 
				
			||||||
      $fd = null;
 | 
					      $fd = null;
 | 
				
			||||||
      $close = true;
 | 
					      $close = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    parent::__construct($fd, $close, $throwOnError, $allowLocking);
 | 
					    parent::__construct($fd, $close, $throwOnError, $allowLocking);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  /** @var string */
 | 
					 | 
				
			||||||
  protected $file;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  /** @var string */
 | 
					 | 
				
			||||||
  protected $mode;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  function getResource() {
 | 
					 | 
				
			||||||
    if ($this->fd === null && $this->file !== null) {
 | 
					 | 
				
			||||||
      $this->fd = IOException::ensure_value(@fopen($this->file, $this->mode));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return parent::getResource();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -3,11 +3,12 @@ namespace nur\sery\os\file;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use nur\sery\os\IOException;
 | 
					use nur\sery\os\IOException;
 | 
				
			||||||
use nur\sery\os\sh;
 | 
					use nur\sery\os\sh;
 | 
				
			||||||
 | 
					use nur\sery\web\http;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Class FileWriter: un fichier accédé en lecture/écriture
 | 
					 * Class FileWriter: un fichier accédé en lecture/écriture
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class FileWriter extends Stream {
 | 
					class FileWriter extends _File {
 | 
				
			||||||
  const DEFAULT_MODE = "a+b";
 | 
					  const DEFAULT_MODE = "a+b";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function __construct($output, ?string $mode=null, bool $throwOnError=true, ?bool $allowLocking=null) {
 | 
					  function __construct($output, ?string $mode=null, bool $throwOnError=true, ?bool $allowLocking=null) {
 | 
				
			||||||
@ -18,26 +19,14 @@ class FileWriter extends Stream {
 | 
				
			|||||||
      $fd = $output;
 | 
					      $fd = $output;
 | 
				
			||||||
      $close = false;
 | 
					      $close = false;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
 | 
					      $file = $output;
 | 
				
			||||||
      if ($mode === null) $mode = static::DEFAULT_MODE;
 | 
					      if ($mode === null) $mode = static::DEFAULT_MODE;
 | 
				
			||||||
      $this->file = $output;
 | 
					      IOException::ensure_value(sh::mkdirof($file));
 | 
				
			||||||
 | 
					      $this->file = $file;
 | 
				
			||||||
      $this->mode = $mode;
 | 
					      $this->mode = $mode;
 | 
				
			||||||
      $fd = null;
 | 
					      $fd = null;
 | 
				
			||||||
      $close = true;
 | 
					      $close = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    parent::__construct($fd, $close, $throwOnError, $allowLocking);
 | 
					    parent::__construct($fd, $close, $throwOnError, $allowLocking);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  /** @var string */
 | 
					 | 
				
			||||||
  protected $file;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  /** @var string */
 | 
					 | 
				
			||||||
  protected $mode;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  function getResource() {
 | 
					 | 
				
			||||||
    if ($this->fd === null && $this->file !== null) {
 | 
					 | 
				
			||||||
      IOException::ensure_value(sh::mkdirof($this->file));
 | 
					 | 
				
			||||||
      $this->fd = IOException::ensure_value(@fopen($this->file, $this->mode));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return parent::getResource();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										13
									
								
								src/os/file/MemoryStream.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/os/file/MemoryStream.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace nur\sery\os\file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Class MemoryStream: un flux qui peut être lu ou écrit, et qui reste
 | 
				
			||||||
 | 
					 * uniquement en mémoire.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class MemoryStream extends Stream {
 | 
				
			||||||
 | 
					  function __construct(bool $throwOnError=true) {
 | 
				
			||||||
 | 
					    $fd = fopen("php://memory", "w+b");
 | 
				
			||||||
 | 
					    parent::__construct($fd, true, $throwOnError);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -108,6 +108,20 @@ class Stream extends AbstractIterator implements IReader, IWriter {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function copyTo(IWriter $dest, bool $closeWriter=false, bool $closeReader=true): void {
 | 
				
			||||||
 | 
					    $srcr = $this->getResource();
 | 
				
			||||||
 | 
					    $destr = $dest->getResource();
 | 
				
			||||||
 | 
					    if ($srcr !== null && $destr !== null) {
 | 
				
			||||||
 | 
					      while (!feof($srcr)) {
 | 
				
			||||||
 | 
					        fwrite($destr, fread($srcr, 8192));
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      $dest->fwrite($this->getContents(false));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if ($closeWriter) $dest->close();
 | 
				
			||||||
 | 
					    if ($closeReader) $this->close();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #############################################################################
 | 
					  #############################################################################
 | 
				
			||||||
  # Reader
 | 
					  # Reader
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -117,7 +131,13 @@ class Stream extends AbstractIterator implements IReader, IWriter {
 | 
				
			|||||||
    return IOException::ensure_value(fread($fd, $length), $this->throwOnError);
 | 
					    return IOException::ensure_value(fread($fd, $length), $this->throwOnError);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /** @throws IOException */
 | 
					  /**
 | 
				
			||||||
 | 
					   * lire la prochaine ligne. la ligne est retournée avec le caractère de fin
 | 
				
			||||||
 | 
					   * de ligne[\r]\n
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @throws EOFException si plus aucune ligne n'est disponible
 | 
				
			||||||
 | 
					   * @throws IOException si une erreur se produit
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
  function fgets(?int $length=null): string {
 | 
					  function fgets(?int $length=null): string {
 | 
				
			||||||
    $fd = $this->getResource();
 | 
					    $fd = $this->getResource();
 | 
				
			||||||
    return EOFException::ensure_not_eof(fgets($fd, $length), $this->throwOnError);
 | 
					    return EOFException::ensure_not_eof(fgets($fd, $length), $this->throwOnError);
 | 
				
			||||||
@ -129,10 +149,22 @@ class Stream extends AbstractIterator implements IReader, IWriter {
 | 
				
			|||||||
    return IOException::ensure_value(fpassthru($fd), $this->throwOnError);
 | 
					    return IOException::ensure_value(fpassthru($fd), $this->throwOnError);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * lire la prochaine ligne. la ligne est retournée *sans* le caractère de fin
 | 
				
			||||||
 | 
					   * de ligne [\r]\n
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
 | 
					   * @throws EOFException si plus aucune ligne n'est disponible
 | 
				
			||||||
 | 
					   * @throws IOException si une erreur se produit
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
  function readLine(): ?string {
 | 
					  function readLine(): ?string {
 | 
				
			||||||
    return str::strip_nl($this->fgets());
 | 
					    return str::strip_nl($this->fgets());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** lire et retourner toutes les lignes */
 | 
				
			||||||
 | 
					  function readLines(): array {
 | 
				
			||||||
 | 
					    return iterator_to_array($this);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * essayer de verrouiller le fichier en lecture. retourner true si l'opération
 | 
					   * essayer de verrouiller le fichier en lecture. retourner true si l'opération
 | 
				
			||||||
   * réussit. dans ce cas, il faut appeler {@link getReader()} avec l'argument
 | 
					   * réussit. dans ce cas, il faut appeler {@link getReader()} avec l'argument
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										17
									
								
								src/os/file/TempStream.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/os/file/TempStream.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace nur\sery\os\file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Class TempStream: un flux qui peut être lu ou écrit, et qui reste en mémoire,
 | 
				
			||||||
 | 
					 * jusqu'à ce que la taille des données atteint {@link self::MAX_MEMORY} et à
 | 
				
			||||||
 | 
					 * ce moment-là un fichier temporaire est automatiquement créé.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class TempStream extends Stream {
 | 
				
			||||||
 | 
					  const MAX_MEMORY = 2 * 1024 * 1024;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function __construct(?int $maxMemory=null, bool $throwOnError=true) {
 | 
				
			||||||
 | 
					    if ($maxMemory === null) $maxMemory = static::MAX_MEMORY;
 | 
				
			||||||
 | 
					    $fd = fopen("php://temp/maxmemory:$maxMemory", "w+b");
 | 
				
			||||||
 | 
					    parent::__construct($fd, true, $throwOnError);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -85,11 +85,4 @@ class TmpfileWriter extends FileWriter {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    if ($mode !== null || $owner !== null) clearstatcache(true, $file);
 | 
					    if ($mode !== null || $owner !== null) clearstatcache(true, $file);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  /** streamer le contenu du fichier en sortie */
 | 
					 | 
				
			||||||
  function readfile(?string $contentType=null, ?string $charset=null, ?string $filename=null, string $disposition=null): bool {
 | 
					 | 
				
			||||||
    if ($contentType !== null) http::content_type($contentType, $charset);
 | 
					 | 
				
			||||||
    if ($filename !== null) http::download_as($filename, $disposition);
 | 
					 | 
				
			||||||
    return readfile($this->file) !== false;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										31
									
								
								src/os/file/_File.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/os/file/_File.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace nur\sery\os\file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use nur\sery\os\IOException;
 | 
				
			||||||
 | 
					use nur\sery\web\http;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					abstract class _File extends Stream {
 | 
				
			||||||
 | 
					  function __construct($fd, bool $close, bool $throwOnError=true, ?bool $allowLocking=null) {
 | 
				
			||||||
 | 
					    parent::__construct($fd, $close, $throwOnError, $allowLocking);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** @var string */
 | 
				
			||||||
 | 
					  protected $file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** @var string */
 | 
				
			||||||
 | 
					  protected $mode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function getResource() {
 | 
				
			||||||
 | 
					    if ($this->fd === null && $this->file !== null) {
 | 
				
			||||||
 | 
					      $this->fd = IOException::ensure_value(@fopen($this->file, $this->mode));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return parent::getResource();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /** streamer le contenu du fichier en sortie */
 | 
				
			||||||
 | 
					  function readfile(?string $contentType=null, ?string $charset=null, ?string $filename=null, string $disposition=null): bool {
 | 
				
			||||||
 | 
					    if ($contentType !== null) http::content_type($contentType, $charset);
 | 
				
			||||||
 | 
					    if ($filename !== null) http::download_as($filename, $disposition);
 | 
				
			||||||
 | 
					    return readfile($this->file) !== false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user