11<?php
22
3- namespace SymfonyTools \CodeBlockChecker \Service ;
3+ namespace SymfonyTools \CodeBlockChecker \Service \ CodeRunner ;
44
55use Doctrine \RST \Nodes \CodeNode ;
66use Symfony \Component \Filesystem \Filesystem ;
1313 *
1414 * @author Tobias Nyholm <tobias.nyholm@gmail.com>
1515 */
16- class CodeNodeRunner
16+ class ConfigurationRunner
1717{
1818 /**
1919 * @param list<CodeNode> $nodes
2020 */
21- public function runNodes (array $ nodes , string $ applicationDirectory ): IssueCollection
21+ public function run (array $ nodes , IssueCollection $ issues , string $ applicationDirectory ): void
2222 {
23- $ issues = new IssueCollection ();
2423 foreach ($ nodes as $ node ) {
2524 $ this ->processNode ($ node , $ issues , $ applicationDirectory );
2625 }
27-
28- return $ issues ;
2926 }
3027
3128 private function processNode (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory ): void
3229 {
33- $ file = $ this ->getFile ($ node );
30+ $ explodedNode = explode ("\n" , $ node ->getValue ());
31+ $ file = $ this ->getFile ($ node , $ explodedNode );
32+
3433 if ('config/packages/ ' !== substr ($ file , 0 , 16 )) {
3534 return ;
3635 }
@@ -45,13 +44,13 @@ private function processNode(CodeNode $node, IssueCollection $issues, string $ap
4544 }
4645
4746 // Write config
48- file_put_contents ($ fullPath , $ this ->getNodeContents ($ node ));
47+ file_put_contents ($ fullPath , $ this ->getNodeContents ($ node, $ explodedNode ));
4948
5049 // Clear cache
5150 $ filesystem ->remove ($ applicationDirectory .'/var/cache ' );
5251
5352 // Warmup and log errors
54- $ this ->warmupCache ($ node , $ issues , $ applicationDirectory );
53+ $ this ->warmupCache ($ node , $ issues , $ applicationDirectory, count ( $ explodedNode ) - 1 );
5554 } finally {
5655 // Remove added file and restore original
5756 $ filesystem ->remove ($ fullPath );
@@ -62,20 +61,19 @@ private function processNode(CodeNode $node, IssueCollection $issues, string $ap
6261 }
6362 }
6463
65- private function warmupCache (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory ): void
64+ private function warmupCache (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory, int $ numberOfLines ): void
6665 {
6766 $ process = new Process (['php ' , 'bin/console ' , 'cache:warmup ' , '--env ' , 'dev ' ], $ applicationDirectory );
6867 $ process ->run ();
6968 if ($ process ->isSuccessful ()) {
7069 return ;
7170 }
7271
73- $ issues ->addIssue (new Issue ($ node , trim ($ process ->getErrorOutput ()), 'Cache Warmup ' , $ node ->getEnvironment ()->getCurrentFileName (), count ( explode ( "\n" , $ node -> getValue ())) - 1 ));
72+ $ issues ->addIssue (new Issue ($ node , trim ($ process ->getErrorOutput ()), 'Cache Warmup ' , $ node ->getEnvironment ()->getCurrentFileName (), $ numberOfLines ));
7473 }
7574
76- private function getFile (CodeNode $ node ): string
75+ private function getFile (CodeNode $ node, array $ contents ): string
7776 {
78- $ contents = explode ("\n" , $ node ->getValue ());
7977 $ regex = match ($ node ->getLanguage ()) {
8078 'php ' => '|^// ?([a-z1-9A-Z_\-/]+\.php)$| ' ,
8179 'yaml ' => '|^# ?([a-z1-9A-Z_\-/]+\.yaml)$| ' ,
@@ -90,20 +88,17 @@ private function getFile(CodeNode $node): string
9088 return $ matches [1 ];
9189 }
9290
93- private function getNodeContents (CodeNode $ node ): string
91+ private function getNodeContents (CodeNode $ node, array $ contents ): string
9492 {
9593 $ language = $ node ->getLanguage ();
9694 if ('php ' === $ language ) {
9795 return '<?php ' ."\n" .$ node ->getValue ();
9896 }
9997
10098 if ('xml ' === $ language ) {
101- $ contents = explode ("\n" , $ node ->getValue ());
10299 unset($ contents [0 ]);
103-
104- return implode ("\n" , $ contents );
105100 }
106101
107- return $ node -> getValue ( );
102+ return implode ( "\n" , $ contents );
108103 }
109104}
0 commit comments