Ferreteria/v0.6/clade/IO/Aspect/Connx/@code/cur/2025/10/22

From WoozleCodes
Jump to navigation Jump to search
2025/10/21 2025/10/22 2025/10/23
interface iConnx extends BaseIface {
    // LIFECYCLE
    function IsOpen() : bool;
    function Open() : ActionIface;
    function Shut() : ActionIface;
}

trait tConnx {
    use BaseTrait;

    abstract protected function ActualOpen() : ActionIface;
    abstract protected function ActualShut() : ActionIface;

    private int $nOpens = 0;
    public function IsOpen() : bool { return $this->nOpens > 0; }
    private $oAct;  // 2025-04-12 For now, this only saves the status of the ActualOpen() operation, not ActualShut().
    public function ItWent() : ActionIface { return $this->oAct; }
    public function Open() : ActionIface {
        $nOpens = $this->nOpens++;
        #echo get_called_class()." OPENS: [$nOpens]".CRLF;
        if ($nOpens === 0) {
            #echo ' - actual open:'.CRLF;
            $oAct = $this->ActualOpen();
            $this->oAct = $oAct;
            #echo ' - - $oAct class: '.get_class($oAct).CRLF;
        } else {
            #echo ' - opening existing $oAct'.CRLF;
            // Report results of actual open, which happened earlier.
            $oAct = $this->oAct;
        }
        return $oAct;
    }
    public function Shut() : ActionIface {
        $nOpens = --$this->nOpens;
        if ($nOpens == 0) {
            $oAct = $this->ActualShut();
        } else {
            $oAct = new ActionClass;
            $oAct->SetNoOp();
            // Only the final/real Shut() actually does anything.
        }
        return $oAct;
    }
}
abstract class caConnx extends BaseClass implements SelfIface {
    use tConnx;
}