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

@@ -27,11 +27,6 @@ abstract class AbstractConverter implements ConverterContract
*/
protected string $outputPath;
/**
* @var string[]
*/
protected array $vars = [];
/**
* @var Environment|null
*/
@@ -72,9 +67,10 @@ abstract class AbstractConverter implements ConverterContract
*/
protected function setVariables(): static
{
if (isset($this->collection?->variable)) {
empty($this->env) && $this->env = new Environment($this->collection?->variable);
if (!empty($this->collection?->variable)) {
foreach ($this->collection->variable as $var) {
$this->vars["{{{$var->key}}}"] = $var->value;
$this->env[$var->key] = $var->value;
}
}
return $this;
@@ -176,21 +172,18 @@ abstract class AbstractConverter implements ConverterContract
*/
protected function interpolate(string $content): string
{
if (empty($this->vars) && !$this->env?->hasVars()) {
if (!$this->env?->hasVars()) {
return $content;
}
$matches = [];
if (preg_match_all('/\{\{[a-zA-Z][a-zA-Z0-9]*}}/m', $content, $matches, PREG_PATTERN_ORDER) > 0) {
if (preg_match_all('/\{\{.*}}/m', $content, $matches, PREG_PATTERN_ORDER) > 0) {
foreach ($matches[0] as $key => $var) {
if (str_contains($content, $var)) {
$content = str_replace($var, $this->vars[$var] ?? $this->env[$var] ?? $var, $content);
$content = str_replace($var, $this->env[$var] ?: $var, $content);
unset($matches[0][$key]);
}
}
}
// if (!empty($matches[0])) {
// fwrite(STDERR, sprintf(' No values found: %s%s', implode(', ', $matches[0]), PHP_EOL));
// }
return $content;
}
}