php-spark/plugins/com.noccy.pdo.shell/Shell/ShellState.php

138 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace SparkPlug\Com\Noccy\Pdo\Shell\Shell;
use Attribute;
use SparkPlug\Com\Noccy\Pdo\PdoResource;
use Spark\Commands\Command;
use Spark\SparkApplication;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ShellState
{
private array $vars = [];
private array $options = [];
private SessionStorage $sessionStorage;
private PersistentStorage $persistentStorage;
public function __construct()
{
$this->sessionStorage = new SessionStorage();
$this->persistentStorage = new PersistentStorage();
}
public function getSessionStorage(): SessionStorage
{
return $this->sessionStorage;
}
public function getPersistentStorage(): PersistentStorage
{
return $this->persistentStorage;
}
public function set(string $varname, $value)
{
$this->vars[$varname] = $value;
}
public function get(string $varname, $default=null): mixed
{
return $this->vars[$varname] ?? $default;
}
public function expand(string $input): string
{
$expanded = preg_replace_callback('/(%\{(.+?)\})/', function ($m) {
$k = $m[1];
if (str_contains($k, ':')) {
[$type, $k] = explode(":", $k, 2);
} else {
$type = "var";
}
switch ($type) {
case 'var':
return $this->get($k, null);
case 'env':
return getenv($k);
}
}, $input);
return $expanded;
}
}
class SessionStorage implements StorageInterface
{
protected $data = [];
public function set(string $k, $value)
{
$this->data[$k] = $value;
}
public function get(string $k, $default=null): mixed
{
return $this->data[$k] ?? $default;
}
public function has(string $k): bool
{
return array_key_exists($k, $this->data);
}
public function delete(string $k): void
{
unset($this->data[$k]);
}
}
class PersistentStorage implements StorageInterface
{
protected $data = [];
public function set(string $k, $value)
{
$this->data[$k] = $value;
}
public function get(string $k, $default=null): mixed
{
return $this->data[$k] ?? $default;
}
public function has(string $k): bool
{
return array_key_exists($k, $this->data);
}
public function delete(string $k): void
{
unset($this->data[$k]);
}
}
interface StorageInterface
{
public function set(string $k, $value);
public function get(string $k, $default=null): mixed;
public function has(string $k): bool;
public function delete(string $k): void;
}