5
5
namespace a9f \FractorXml ;
6
6
7
7
use a9f \Fractor \Application \Contract \FileProcessor ;
8
- use a9f \Fractor \Application \RuleSkipper ;
9
8
use a9f \Fractor \Application \ValueObject \File ;
10
9
use a9f \Fractor \Exception \ShouldNotHappenException ;
10
+ use a9f \Fractor \Rules \RulesProvider ;
11
11
use a9f \FractorXml \Contract \XmlFractor ;
12
12
use a9f \FractorXml \ValueObjectFactory \DomDocumentFactory ;
13
13
use DOMDocument ;
14
- use Webmozart \Assert \Assert ;
15
- use function Safe \file_get_contents ;
16
14
17
15
final readonly class XmlFileProcessor implements FileProcessor
18
16
{
19
17
/**
20
- * @param XmlFractor[] $rules
18
+ * @param RulesProvider< XmlFractor> $rulesProvider
21
19
*/
22
20
public function __construct (
23
- private iterable $ rules ,
24
21
private DomDocumentFactory $ domDocumentFactory ,
25
- private RuleSkipper $ ruleSkipper
22
+ private RulesProvider $ rulesProvider
26
23
) {
27
- Assert::allIsInstanceOf ($ this ->rules , XmlFractor::class);
28
24
}
29
25
30
26
public function canHandle (File $ file ): bool
@@ -35,17 +31,12 @@ public function canHandle(File $file): bool
35
31
public function handle (File $ file ): void
36
32
{
37
33
$ document = $ this ->domDocumentFactory ->create ();
38
- $ originalXml = file_get_contents ($ file ->getFilePath ());
39
- $ document ->loadXML ($ originalXml );
34
+ $ document ->loadXML ($ file ->getOriginalContent ());
40
35
41
36
// This is a hacky trick to keep format and create a nice diff later
42
37
$ file ->changeOriginalContent ($ this ->saveXml ($ document ));
43
38
44
- $ applicableRulesForFile = array_filter (
45
- // for a large number of rules, this might be resource hungry; try to find a better alternative here
46
- iterator_to_array ($ this ->rules ),
47
- fn (XmlFractor $ rule ) => $ this ->ruleSkipper ->shouldSkip ($ rule ::class, $ file ->getFilePath ()) === false
48
- );
39
+ $ applicableRulesForFile = $ this ->rulesProvider ->getApplicableRules ($ file );
49
40
50
41
$ iterator = new DomDocumentIterator ($ applicableRulesForFile );
51
42
$ iterator ->traverseDocument ($ file , $ document );
0 commit comments