Ferreteria/v0.6/clade/IO/Aspect/Connx/Buffer/File
Jump to navigation
Jump to search
| ||||||||||||||||||||
About
- Purpose: a Buffer which moves data between a stream and a file
- This is the direction-agnostic base class, necessarily abstract.
- Assumes display is updateable (CLI: yes, web: no).
History
Code
interface iFile extends BaseIface {}
abstract class caFile extends BaseClass {
// ++ CONFIG ++ //
abstract protected function ModesArray() : array;
// -- CONFIG -- //
// ++ SETUP ++ //
public function __construct(private FileIface $oData) {}
protected function DataFile() : FileIface { return $this->oData; }
// -- SETUP -- //
// ++ LIFECYCLE ++ //
protected function ActualOpen() : ActionIface {
$oData = $this->DataFile();
$oMode = new FModeClass($this->ModesArray());
$oDataIO = $oData->InOut();
$oDataIO->Mode($oMode);
#echo $oData->ReflectThis()->Report();
$oAct = $oDataIO->Open();
if (!$oAct->GetOkay()) {
self::GotToHere('File error detected here.');
echo self::Screen()->ErrorIt('File error').': '.$oAct->GetStatusText().CRLF;
}
$this->RewindAll(); // reset the contents
$this->StartClock(); // reset the time counter
return $oAct;
}
protected function ActualShut() : ActionIface {
$oDataIO = $this->DataFile()->InOut();
$oAct = $oDataIO->Shut();
$this->UpdateStatus();
echo CRLF;
return $oAct;
}
private $onWhenStart;
protected function StartClock() { $this->onWhenStart = new DateTime; }
protected function DurationString() : string {
$onNow = new DateTime;
$onDiff = $this->onWhenStart->diff($onNow);
return $onDiff->format('%h:%I:%S');
}
// -- LIFECYCLE -- //
// ++ UI ++ //
private $onFmt = NULL; protected function Formatter() : NumberFormatter {
return $this->onFmt ?? ($this->onFmt = new NumberFormatter('en_US',NumberFormatter::DECIMAL));
}
protected function UpdateStatus() {
static $nMaxWd = 0;
static $sInBytes = '0';
$nTot = $this->SpentByteCount();
$sTot = $this->Formatter()->format($nTot);
$nFil = $this->DataFile()->Size();
$ftRem = ''; // default
if (is_int($nFil) && ($nFil > 0)) {
$nRem = $nFil - $nTot;
if ($nRem > 0) {
$sRem = $this->Formatter()->format($nRem);
$ftRem = "rem: $sRem | ";
}
}
$nMem = memory_get_usage();
$sMem = $this->Formatter()->format($nMem);
$sTime = $this->DurationString();
#echo "\r ($sTime) $ftInBytes $sTot written (mem used: $sMem)";
echo "\r ($sTime) $sTot bytes ({$ftRem}mem: $sMem)"; // TODO: clear remainder of line
#echo CRLF.$this->DataFile()->Ident()->ReflectThis()->Report(); die();
}
// ACTION: calls UpdateStatus() if enough time has passed
protected function OnStatusChange() {
static $ntLast = 0.0;
$ntNow = microtime(TRUE);
if ($ntNow - $ntLast > 0.05) { // update display every 0.05 seconds
$ntLast = $ntNow;
$this->UpdateStatus();
// reset the timeout clock
$ok = set_time_limit(10); // NOTE: seems to always return FALSE; TODO: make this configurable
}
}
// -- UI -- //
}