interface iCmdLine extends BaseIface {
// SETUP
static function FromVoid() : SelfIface;
static function FromString(string $s) : SelfIface;
static function FromArray(array $ar) : SelfIface;
// ACCESS: read
function AsString() : string;
function AsArray() : array;
// ACCESS: write
function WithArray(array $a);
function EmbedWithin(SelfIface $oCmd);
// MODE
function DoWait(?bool $b=NULL) : bool;
// OBJECTS
function Hints() : HintsIface;
function QOLecturer() : QStreamIface;
function QOListener() : QStreamIface;
}
class cCmdLine extends BaseClass implements SelfIface {
// ++ SETUP ++ //
public static function FromVoid() : SelfIface { return new static; }
public static function FromString(string $s) : SelfIface {
$oThis = new static;
$oThis->QString()->SetIt($s);
return $oThis;
}
public static function FromArray(array $a) : SelfIface {
$oThis = new static;
$oThis->WithArray($a);
return $oThis;
}
// -- SETUP -- //
// ++ ACCESS: read ++ //
public function Spawn() : SelfIface {
$oNew = new static;
$oNew->qsCmd = QStrClass::FromSame($this->QString());
$oNew->qaCmd = QArrClass::FromSame($this->QArray());
return $oNew;
}
public function AsString() : string {
return $this->QString()->HasIt() ? $this->QString()->GetIt() : $this->ToString();
}
public function AsArray() : array {
return $this->QArray()->HasIt() ? $this->QArray()->GetIt() : $this->ToArray();
}
// ++ ACCESS: display ++ //
public function VIEW_Inline() : string {
$q = $this->QString();
$sStr = $q->HasIt() ? ('string=['.$q->GetIt().']') : '[string n/a]';
$q = $this->QArray();
$sArr = $q->HasIt() ? ('array N='.count($q->GetIt())) : '[array n/a]';
$sOID = $this->ObjectID();
return "(oid$sOID) $sStr / $sArr";
}
// ++ ACCESS: write ++ //
// ACTION: Embed the current CmdLine within a longer one, consisting of a prefix ($oPfx) and optional suffix ($oSfx).
// RULE: Array elements are either strings (I think) or CmdLine objects.
public function EmbedWithin(SelfIface $oPfx, ?SelfIface $oSfx=NULL) {
$arPfx = $oPfx->AsArray();
$oOld = $this->Spawn(); // copy old contents to embed
#$this->AmHereShort('PFX: '.$oPfx->VIEW_Inline().' / THIS: '.$this->VIEW_Inline().' / OLD: '.$oOld->VIEW_Inline());
if (is_object($oSfx)) {
$arSfx = $oSfx->AsArray();
$ar = array_merge($arPfx,[$oOld],$arSfx);
} else {
$ar = array_merge($arPfx,[$oOld]);
}
$this->QArray()->SetIt($ar);
$this->QString()->ZapIt();
}
// ++ MODE ++ //
// PURPOSE: signals whether the Runner code should wait for the process to end (buffering received data) or return right after executing
private $bWait = TRUE; // default
public function DoWait(?bool $b=NULL) : bool { return is_bool($b) ? ($this->bWait = $b) : $this->bWait; }
// -- MODE -- //
// ++ ACCESS ++ //
public function WithArray(array $a) { $this->QArray()->SetIt($a); $this->QString()->ZapIt(); }
public function WithString(string $s) { $this->QString()->SetIt($s); $this->QArray()->ZapIt(); }
// ++ ACCESS (internal): storage ++ //
private $qsCmd=NULL;
protected function QString() : QStrIface { return $this->qsCmd ?? ($this->qsCmd = QStrClass::AsNew()); }
private $qaCmd=NULL;
protected function QArray() : QArrIface { return $this->qaCmd ?? ($this->qaCmd = QArrClass::AsNew()); }
// ++ ACCESS (internal): conversion ++ //
// PURPOSE: Command is stored as an array -- convert to string.
protected function ToString() : string {
$ar = $this->QArray()->GetIt();
$s = NULL;
foreach ($ar as $vPiece) {
if (is_string($s)) $s .= ' ';
if (is_string($vPiece)) {
$s .= $vPiece;
} else {
$s .= escapeshellarg($vPiece->AsString());
}
}
return $s;
}
// PURPOSE: Command is stored as a string -- convert to array.
protected function ToArray() : array {
$s = $this->QString()->GetIt();
$oParser = new ParserClass($s);
$oGene = $oParser->Parse();
return $oGene->AsStringArray();
}
// -- ACCESS -- //
// ++ OBJECTS ++ //
private $oaHints = NULL;
public function Hints() : HintsIface { return $this->oaHints ?? ($this->oaHints = new HintsClass); }
private $qoLect = NULL;
public function QOLecturer() : QStreamIface { return $this->qoLect ?? ($this->qoLect = QStreamClass::AsNew()); }
private $qoLstn = NULL;
public function QOListener() : QStreamIface { return $this->qoLstn ?? ($this->qoLstn = QStreamClass::AsNew()); }
private $qoErrs = NULL;
public function QOErrorBin() : QStreamIface { return $this->qoErrs ?? ($this->qoErrs = QStreamClass::AsNew()); }
// -- OBJECTS -- //
}