diff --git a/packages/php-config-printer/src/NodeFactory/ContainerConfiguratorReturnClosureFactory.php b/packages/php-config-printer/src/NodeFactory/ContainerConfiguratorReturnClosureFactory.php index 0019220aecc..9a8e041d54a 100644 --- a/packages/php-config-printer/src/NodeFactory/ContainerConfiguratorReturnClosureFactory.php +++ b/packages/php-config-printer/src/NodeFactory/ContainerConfiguratorReturnClosureFactory.php @@ -5,6 +5,7 @@ namespace Symplify\PhpConfigPrinter\NodeFactory; use Nette\Utils\Json; +use PhpParser\Comment\Doc; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; @@ -60,7 +61,10 @@ public function createFromYamlArray( */ private function createClosureStmts(array $yamlData): array { - $yamlData = array_filter($yamlData); + $yamlData = array_filter($yamlData, function($v, $k) { + return $v || str_starts_with($k, '%comment('); + }, ARRAY_FILTER_USE_BOTH); + return $this->createStmtsFromCaseConverters($yamlData); } @@ -72,10 +76,24 @@ private function createStmtsFromCaseConverters(array $yamlData): array { $stmts = []; + $lastComments = []; + $rootLastComments = []; foreach ($yamlData as $key => $values) { + if (str_starts_with($key, '%comment(')) { + $rootLastComments[] = substr($key, 9, -2); + + continue; + } + $stmts = $this->createInitializeStmt($key, $stmts); foreach ($values as $nestedKey => $nestedValues) { + if (str_starts_with($nestedKey, '%comment(')) { + $lastComments[] = substr($nestedKey, 9, -2); + + continue; + } + $nestedNodes = $this->processNestedNodes($key, $nestedKey, $nestedValues); if ($nestedNodes !== []) { @@ -89,11 +107,26 @@ private function createStmtsFromCaseConverters(array $yamlData): array } $lastNode = end($stmts); + $node = $this->resolveExpressionWhenAtEnv($expression, $key, $lastNode); + if ($node !== null) { + foreach ($lastComments as $lastComment) { + $node->setDocComment(new Doc('// ' . $lastComment)); + } + $lastComment = []; + $stmts[] = $node; } } + + $firstNode = reset($stmts); + if ($firstNode) { + foreach ($rootLastComments as $lastComment) { + $firstNode->setDocComment(new Doc('// ' . $lastComment)); + } + $rootLastComments = []; + } } return $stmts;