This commit is contained in:
Jephté Clain 2025-06-19 17:42:18 +04:00
parent 6bcd8d4cf6
commit 0945a86763
2 changed files with 27 additions and 14 deletions

View File

@ -122,20 +122,6 @@ abstract class AbstractBuilder extends TempStream implements IBuilder {
return $this->index;
}
abstract protected function _sendContentType(): void;
protected bool $sentHeaders = false;
function sendHeaders(): void {
if ($this->sentHeaders) return;
$this->_sendContentType();
$output = $this->output;
if ($output !== null) {
http::download_as(path::filename($output));
}
$this->sentHeaders = true;
}
protected function _build(?iterable $rows=null): void {
$this->writeAll($rows);
$this->writeHeaders();
@ -158,6 +144,20 @@ abstract class AbstractBuilder extends TempStream implements IBuilder {
return $ok;
}
abstract protected function _sendContentType(): void;
protected bool $sentHeaders = false;
function sendHeaders(): void {
if ($this->sentHeaders) return;
$this->_sendContentType();
$output = $this->output;
if ($output !== null) {
http::download_as(path::filename($output));
}
$this->sentHeaders = true;
}
function sendFile(?iterable $rows=null): int {
if (!$this->built) {
$this->_build($rows);

View File

@ -1,6 +1,8 @@
<?php
namespace nulib\file\tab;
use nulib\file\IWriter;
interface IBuilder extends \nulib\file\IReader {
function writeHeaders(?array $headers=null): void;
@ -13,7 +15,18 @@ interface IBuilder extends \nulib\file\IReader {
*/
function getCount(): int;
/** préparer le fichier (sans l'envoyer) */
function build(?iterable $rows=null, bool $close=true): bool;
/** enregistrer le fichier préparé avec {@link build()} */
function copyTo(IWriter $dest, bool $closeWriter=false, bool $closeReader=true): void;
/** envoyer les en-têtes */
function sendHeaders(): void;
/**
* envoyer le fichier pour téléchargement (la méthode {@link sendHeaders()}
* est automatiquement appelée le cas échéant
*/
function sendFile(?iterable $rows=null): int;
}