clrghouz/app/Classes/BBS/Control.php
2023-09-13 20:59:40 +10:00

101 lines
2.1 KiB
PHP

<?php
namespace App\Classes\BBS;
use App\Classes\BBS\Control\EditFrame;
use App\Classes\BBS\Control\Register;
use App\Classes\BBS\Control\Telnet;
abstract class Control
{
const prefix = 'App\Classes\Control\\';
// Has this control class finished with input
protected bool $complete = FALSE;
// The server object that is running this control class
protected Server $so;
/**
* What is the state of the server outside of this control.
* Should only contain
* + mode = Mode to follow outside of the control method
* + action = Action to run after leaving the control method
*
* @var array
*/
public array $state = [];
abstract public function handle(string $read): string;
public static function factory(string $name,Server $so,array $args=[])
{
switch ($name) {
case 'editframe':
return new EditFrame($so,$args);
case 'register':
return new Register($so);
case 'telnet':
return new Telnet($so);
default:
$c = (class_exists($name)) ? $name : self::prefix.$name;
$o = class_exists($c) ? new $c($so,$args) : NULL;
$so->log('debug',sprintf(($o ? 'Executing: %s' : 'Class doesnt exist: %s'),$c));
return $o;
}
}
public function __construct(Server $so,array $args=[])
{
$this->so = $so;
// Boot control, preparing anything before keyboard entry
$this->boot();
$this->so->log('info',sprintf('Initialised control %s',get_class($this)));
}
public function __get(string $key): mixed
{
switch ($key) {
case 'complete':
return $this->complete;
case 'name':
return get_class($this);
default:
throw new \Exception(sprintf('%s:! Unknown key: %s',static::LOGKEY,$key));
}
}
// Default boot method if a child class doesnt have one.
protected function boot()
{
$this->state['mode'] = FALSE;
}
/**
* Has control completed?
* @deprecated use $this->complete;
*/
public function complete()
{
return $this->complete;
}
/**
* If completing an Action frame, this will be called to submit the data.
*
* Ideally this should be overridden in a child class.
*/
public function process()
{
$this->complete = TRUE;
}
}