1
1
<?php
2
2
3
- namespace SymfonyTools \CodeBlockChecker \Service ;
3
+ namespace SymfonyTools \CodeBlockChecker \Service \ CodeRunner ;
4
4
5
5
use Doctrine \RST \Nodes \CodeNode ;
6
6
use Symfony \Component \Filesystem \Filesystem ;
13
13
*
14
14
* @author Tobias Nyholm <[email protected] >
15
15
*/
16
- class CodeNodeRunner
16
+ class ConfigurationRunner
17
17
{
18
18
/**
19
19
* @param list<CodeNode> $nodes
20
20
*/
21
- public function runNodes (array $ nodes , string $ applicationDirectory ): IssueCollection
21
+ public function run (array $ nodes , IssueCollection $ issues , string $ applicationDirectory ): void
22
22
{
23
- $ issues = new IssueCollection ();
24
23
foreach ($ nodes as $ node ) {
25
24
$ this ->processNode ($ node , $ issues , $ applicationDirectory );
26
25
}
27
-
28
- return $ issues ;
29
26
}
30
27
31
28
private function processNode (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory ): void
32
29
{
33
- $ file = $ this ->getFile ($ node );
30
+ $ explodedNode = explode ("\n" , $ node ->getValue ());
31
+ $ file = $ this ->getFile ($ node , $ explodedNode );
32
+
34
33
if ('config/packages/ ' !== substr ($ file , 0 , 16 )) {
35
34
return ;
36
35
}
@@ -45,13 +44,13 @@ private function processNode(CodeNode $node, IssueCollection $issues, string $ap
45
44
}
46
45
47
46
// Write config
48
- file_put_contents ($ fullPath , $ this ->getNodeContents ($ node ));
47
+ file_put_contents ($ fullPath , $ this ->getNodeContents ($ node, $ explodedNode ));
49
48
50
49
// Clear cache
51
50
$ filesystem ->remove ($ applicationDirectory .'/var/cache ' );
52
51
53
52
// Warmup and log errors
54
- $ this ->warmupCache ($ node , $ issues , $ applicationDirectory );
53
+ $ this ->warmupCache ($ node , $ issues , $ applicationDirectory, count ( $ explodedNode ) - 1 );
55
54
} finally {
56
55
// Remove added file and restore original
57
56
$ filesystem ->remove ($ fullPath );
@@ -62,20 +61,19 @@ private function processNode(CodeNode $node, IssueCollection $issues, string $ap
62
61
}
63
62
}
64
63
65
- private function warmupCache (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory ): void
64
+ private function warmupCache (CodeNode $ node , IssueCollection $ issues , string $ applicationDirectory, int $ numberOfLines ): void
66
65
{
67
66
$ process = new Process (['php ' , 'bin/console ' , 'cache:warmup ' , '--env ' , 'dev ' ], $ applicationDirectory );
68
67
$ process ->run ();
69
68
if ($ process ->isSuccessful ()) {
70
69
return ;
71
70
}
72
71
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 ));
74
73
}
75
74
76
- private function getFile (CodeNode $ node ): string
75
+ private function getFile (CodeNode $ node, array $ contents ): string
77
76
{
78
- $ contents = explode ("\n" , $ node ->getValue ());
79
77
$ regex = match ($ node ->getLanguage ()) {
80
78
'php ' => '|^// ?([a-z1-9A-Z_\-/]+\.php)$| ' ,
81
79
'yaml ' => '|^# ?([a-z1-9A-Z_\-/]+\.yaml)$| ' ,
@@ -90,20 +88,17 @@ private function getFile(CodeNode $node): string
90
88
return $ matches [1 ];
91
89
}
92
90
93
- private function getNodeContents (CodeNode $ node ): string
91
+ private function getNodeContents (CodeNode $ node, array $ contents ): string
94
92
{
95
93
$ language = $ node ->getLanguage ();
96
94
if ('php ' === $ language ) {
97
95
return '<?php ' ."\n" .$ node ->getValue ();
98
96
}
99
97
100
98
if ('xml ' === $ language ) {
101
- $ contents = explode ("\n" , $ node ->getValue ());
102
99
unset($ contents [0 ]);
103
-
104
- return implode ("\n" , $ contents );
105
100
}
106
101
107
- return $ node -> getValue ( );
102
+ return implode ( "\n" , $ contents );
108
103
}
109
104
}
0 commit comments