Ferreteria/v0.6/clade/IO/Aspect/Connx/run/Command

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | IO‎ | Aspect‎ | Connx‎ | run
Jump to navigation Jump to search
clade: IO\Aspect\Connx\run\Command
Clade Family
StandardBase Command Settable
Clade Aliases
Alias Clade
Base* [c,i] StandardBase
QArr* [c,i] Arr
QStr* [c,i] Str
Parser* [c,i] Chunker
SelfIface Command
QSession* [c,i] Session
QStream* [c,i] Stream
Subpages

Code

interface iCommand extends BaseIface {
    // ACCESS: read
    function AsArray() : array;
    function AsString() : string;
    function HasIt() : bool;
    // ACCESS: write
    function WithArray(array $a);
    function EmbedWithin(SelfIface $oCmd);
    // MODE
    #function ExecMode(?ModeEnum $e=NULL) : ModeEnum;
    #function DoWait(?bool $b=NULL) : bool;
    #function IsOneShot(?bool $b=NULL) : bool;
    // OBJECTS
    function QOSession()  : QSessionIface;
    /* 2026-04-04 moving these to Session
    function QOLecturer() : QStreamIface;
    function QOListener() : QStreamIface;
    function QOErrorBin() : QStreamIface;
    */
}
class cCommand 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 AsArray() : array {
        return $this->QArray()->HasIt() ? $this->QArray()->GetIt() : $this->ToArray();
    }
    public function AsString() : string {
        return $this->QString()->HasIt() ? $this->QString()->GetIt() : $this->ToString();
    }
    public function HasIt() : bool { return $this->QArray()->HasAny() || $this->QString()->HasIt(); }

    // ++ 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.
    // 2026-04-01 If we escape Command-objects included in the array, that causes problems for simple appending.
    //   When we need to put quotes around a command-string because it's being passed as an argument, use EmbedWithin().
    //   (If that turns out to be insufficient in some cases, then those cases need to be documented and a new solution worked out.)
    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());
                $s .= $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 $qoSess = NULL;
    public function QOSession() : QSessionIface { return $this->qoSess ?? ($this->qoSess = QSessionClass::AsNew()); }

    /* 2026-04-04 moving these to Session
    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 -- //
    // ++ DIAGS ++ //

    public function DEBUG_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";
    }
    public function DEBUG_AsBlock() : string {
        $oScrn = self::Screen();

        $q = $this->QString();
        $ftStr = $q->HasIt() ? ('string=['.$oScrn->GreenIt($q->GetIt()).']') : $oScrn->ErrorIt('[string n/a]');

        $q = $this->QArray();
        $ftArr = $q->HasIt() ? ('array N='.$oScrn->GreenIt(count($q->GetIt()))) : $oScrn->ErrorIt('[array n/a]');

        $sOID = $this->ObjectID();
        #echo $this->QOListener()->GetIt()->ReflectThis()->Report();

        return "oid$sOID:"
          .CRLF." - COMMAND STRING: [$ftStr]"
          .CRLF." - COMMAND ARRAY:  $ftArr"
          .CRLF." - STREAMS:"
          .CRLF." -- Lecture: ".$this->QOLecturer()->DEBUG_Inline()
          .CRLF." -- Listen: ".$this->QOListener()->DEBUG_Inline()
          .CRLF." -- Errors: ".$this->QOErrorBin()->DEBUG_Inline()
          .CRLF." - SESSION: ".$this->QOSession()->DEBUG_Inline()
          ;
    }

    // -- DIAGS -- //
}