diff --git a/packages/config-transformer/src/ConfigLoader.php b/packages/config-transformer/src/ConfigLoader.php index 376fc2bf1d..427919beb2 100644 --- a/packages/config-transformer/src/ConfigLoader.php +++ b/packages/config-transformer/src/ConfigLoader.php @@ -39,6 +39,12 @@ final class ConfigLoader */ private const UNQUOTED_PARAMETER_REGEX = '#(\w+:\s+)(\%(.*?)%)(.*?)?$#m'; + /** + * @see https://regex101.com/r/I2wiC9/2 + * @var string + */ + private const COMMENT_REGEX = '#^(\s+)?\# (.*)#m'; + public function __construct( private IdAwareXmlFileLoaderFactory $idAwareXmlFileLoaderFactory, private SmartFileSystem $smartFileSystem, @@ -64,6 +70,13 @@ public function createAndLoadContainerBuilderFromFileInfo( static fn (array $match): string => $match[1] . '"' . $match[2] . ($match[4] ?? '') . '"' ); + // fake comment to keep them + $content = Strings::replace( + $content, + self::COMMENT_REGEX, + static fn ($match): string => $match[1] . '"%comment(' . $match[2] . ')%": null' + ); + if (in_array($smartFileInfo->getSuffix(), [Format::YML, Format::YAML], true)) { $content = Strings::replace( $content, diff --git a/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_packages.yaml b/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_packages.yaml new file mode 100644 index 0000000000..957c591ea7 --- /dev/null +++ b/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_packages.yaml @@ -0,0 +1,29 @@ +# Intro comment +security: + # Some nice comment 1 + encoders: true # inline comment + # Some nice comment 2 + other: true # inline comment + nested: + # Nested Config + key: true # inline comment +----- +extension('security', [ + // Some nice comment 1 + 'encoders' => true, // inline comment + // Some nice comment 2 + 'other' => true, // inline comment + 'nested' => [ + // Nested Config + 'key' => true, // inline comment + ], + ]); +}; diff --git a/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_parameters.yaml b/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_parameters.yaml new file mode 100644 index 0000000000..576f4d4c9a --- /dev/null +++ b/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_parameters.yaml @@ -0,0 +1,18 @@ +# Intro comment +parameters: + # Some nice comment + key: 'value' # inline comment +----- +parameters(); + + // Some nice comment + $parameters->set('key', 'value'); // inline comment +}; diff --git a/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_services.yaml b/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_services.yaml new file mode 100644 index 0000000000..69e7edf497 --- /dev/null +++ b/packages/config-transformer/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/comments_services.yaml @@ -0,0 +1,19 @@ +# Intro comment +services: + # Some nice comment + App\Service: ~ # inline comment +----- +services(); + + // Some nice comment + $services->set(Service::class); // inline comment +}; diff --git a/packages/php-config-printer/src/NodeFactory/ContainerConfiguratorReturnClosureFactory.php b/packages/php-config-printer/src/NodeFactory/ContainerConfiguratorReturnClosureFactory.php index 0019220aec..9a8e041d54 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;