Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Add some test cases for basic comment support #4483

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/config-transformer/src/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
-----
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// Intro comment
$containerConfigurator->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
],
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Intro comment
parameters:
# Some nice comment
key: 'value' # inline comment
-----
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// Intro comment
$parameters = $containerConfigurator->parameters();

// Some nice comment
$parameters->set('key', 'value'); // inline comment
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Intro comment
services:
# Some nice comment
App\Service: ~ # inline comment
-----
<?php

declare(strict_types=1);

use App\Service;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// Intro comment
$services = $containerConfigurator->services();

// Some nice comment
$services->set(Service::class); // inline comment
};
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 !== []) {
Expand All @@ -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;
Expand Down