55namespace Symplify \PhpConfigPrinter \NodeFactory ;
66
77use Nette \Utils \Json ;
8+ use PhpParser \Comment \Doc ;
89use PhpParser \Node \Arg ;
910use PhpParser \Node \Expr ;
1011use PhpParser \Node \Expr \Array_ ;
@@ -60,7 +61,10 @@ public function createFromYamlArray(
6061 */
6162 private function createClosureStmts (array $ yamlData ): array
6263 {
63- $ yamlData = array_filter ($ yamlData );
64+ $ yamlData = array_filter ($ yamlData , function ($ v , $ k ) {
65+ return $ v || str_starts_with ($ k , '%comment( ' );
66+ }, ARRAY_FILTER_USE_BOTH );
67+
6468 return $ this ->createStmtsFromCaseConverters ($ yamlData );
6569 }
6670
@@ -72,10 +76,24 @@ private function createStmtsFromCaseConverters(array $yamlData): array
7276 {
7377 $ stmts = [];
7478
79+ $ lastComments = [];
80+ $ rootLastComments = [];
7581 foreach ($ yamlData as $ key => $ values ) {
82+ if (str_starts_with ($ key , '%comment( ' )) {
83+ $ rootLastComments [] = substr ($ key , 9 , -2 );
84+
85+ continue ;
86+ }
87+
7688 $ stmts = $ this ->createInitializeStmt ($ key , $ stmts );
7789
7890 foreach ($ values as $ nestedKey => $ nestedValues ) {
91+ if (str_starts_with ($ nestedKey , '%comment( ' )) {
92+ $ lastComments [] = substr ($ nestedKey , 9 , -2 );
93+
94+ continue ;
95+ }
96+
7997 $ nestedNodes = $ this ->processNestedNodes ($ key , $ nestedKey , $ nestedValues );
8098
8199 if ($ nestedNodes !== []) {
@@ -89,11 +107,26 @@ private function createStmtsFromCaseConverters(array $yamlData): array
89107 }
90108
91109 $ lastNode = end ($ stmts );
110+
92111 $ node = $ this ->resolveExpressionWhenAtEnv ($ expression , $ key , $ lastNode );
112+
93113 if ($ node !== null ) {
114+ foreach ($ lastComments as $ lastComment ) {
115+ $ node ->setDocComment (new Doc ('// ' . $ lastComment ));
116+ }
117+ $ lastComment = [];
118+
94119 $ stmts [] = $ node ;
95120 }
96121 }
122+
123+ $ firstNode = reset ($ stmts );
124+ if ($ firstNode ) {
125+ foreach ($ rootLastComments as $ lastComment ) {
126+ $ firstNode ->setDocComment (new Doc ('// ' . $ lastComment ));
127+ }
128+ $ rootLastComments = [];
129+ }
97130 }
98131
99132 return $ stmts ;
0 commit comments