Skip to content

Commit

Permalink
Added support for setting function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Nov 25, 2021
1 parent 7787df1 commit 51ef587
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Config/ConfigFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function set($key, $value = null): ConfigFile
}

// special handling of function objects
if (get_class($target->value) === FuncCall::class) {
if (get_class($target->value) === FuncCall::class && !$value instanceof ConfigFunction) {
if ($target->value->name->parts[0] !== 'env' || !isset($target->value->args[0])) {
return $this;
}
Expand Down Expand Up @@ -174,17 +174,28 @@ protected function makeArrayItem(string $key, string $valueType, $value): ArrayI
*
* @param string $type
* @param mixed $value
* @return ConstFetch|LNumber|String_
* @return ConstFetch|LNumber|String_|FuncCall
*/
protected function makeAstNode(string $type, $value)
{
if ($value instanceof ConfigFunction) {
$type = 'function';
}

switch ($type) {
case 'string':
return new String_($value);
case 'boolean':
return new ConstFetch(new Name($value ? 'true' : 'false'));
case 'integer':
return new LNumber($value);
case 'function':
return new FuncCall(
new Name($value->getName()),
array_map(function ($arg) {
return new Arg($this->makeAstNode(gettype($arg), $arg));
}, $value->getArgs())
);
default:
throw new \RuntimeException('not implemented replacement type: ' . $type);
}
Expand Down Expand Up @@ -273,6 +284,11 @@ public function write(string $filePath = null): void
file_put_contents($filePath, $this->render());
}

public function function(string $name, array $args): ConfigFunction
{
return new ConfigFunction($name, $args);
}

/**
* Get the printed AST as php code
*
Expand Down

0 comments on commit 51ef587

Please sign in to comment.