Introducing --var and some improvements in variables interpolation

- now you can override any env variable, see README to find out how --var works
- improvements around env and vars storage
- some tiny ram optimizations in Processor (not sure if it useful nor necessary, actually)
This commit is contained in:
2023-09-10 15:28:09 +08:00
parent 8ab615c062
commit 0b4317f56a
4 changed files with 89 additions and 45 deletions

View File

@@ -17,7 +17,7 @@ class Environment implements \ArrayAccess
public function __construct(protected object $env)
{
foreach ($env->values as $var) {
$this->vars["{{{$var->key}}}"] = $var->value;
$this->vars[static::formatKey($var->key)] = $var->value;
}
}
@@ -36,7 +36,7 @@ class Environment implements \ArrayAccess
*/
public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->vars);
return array_key_exists(static::formatKey($offset), $this->vars);
}
/**
@@ -44,7 +44,7 @@ class Environment implements \ArrayAccess
*/
public function offsetGet(mixed $offset): mixed
{
return $this->vars[$offset];
return $this->vars[static::formatKey($offset)] ?? null;
}
/**
@@ -52,7 +52,7 @@ class Environment implements \ArrayAccess
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->vars[$offset] = $value;
$this->vars[static::formatKey($offset)] = $value;
}
/**
@@ -60,6 +60,17 @@ class Environment implements \ArrayAccess
*/
public function offsetUnset(mixed $offset): void
{
unset($this->vars[$offset]);
unset($this->vars[static::formatKey($offset)]);
}
/**
* Returns correct variable {{name}}
*
* @param string $key
* @return string
*/
public static function formatKey(string $key): string
{
return sprintf('{{%s}}', str_replace(['{', '}'], '', $key));
}
}