55namespace TwigCS \Config ;
66
77use Exception ;
8- use Symfony \Component \Finder \Finder ;
98
109/**
1110 * TwigCS configuration data.
@@ -15,74 +14,41 @@ class Config
1514 /**
1615 * @var array
1716 */
18- public static $ defaultConfig = [
19- 'exclude ' => [],
20- 'pattern ' => '*.twig ' ,
21- 'paths ' => [],
22- 'workingDirectory ' => '' ,
23- ];
17+ protected $ paths = [];
2418
2519 /**
26- * @var array
20+ * @param array $paths
2721 */
28- protected $ config = [];
29-
30- /**
31- * @param array $config
32- */
33- public function __construct (array $ config = [])
22+ public function __construct (array $ paths = [])
3423 {
35- $ this ->config = array_merge ( $ this :: $ defaultConfig , $ config ) ;
24+ $ this ->paths = $ paths ;
3625 }
3726
3827 /**
39- * @return Finder
28+ * @return array
4029 *
4130 * @throws Exception
4231 */
43- public function findFiles (): Finder
32+ public function findFiles (): array
4433 {
45- $ paths = $ this ->get ('paths ' );
46- $ exclude = $ this ->get ('exclude ' );
47- $ workingDir = $ this ->get ('workingDirectory ' );
48-
49- // Build the finder.
50- $ files = Finder::create ()
51- ->in ($ workingDir )
52- ->name ($ this ->config ['pattern ' ])
53- ->files ();
54-
55- // Include all matching paths.
56- foreach ($ paths as $ path ) {
57- // Trim absolute path
58- if (substr ($ path , 0 , strlen ($ workingDir )) === $ workingDir ) {
59- $ path = ltrim (substr ($ path , strlen ($ workingDir )), '/ ' );
34+ $ files = [];
35+ foreach ($ this ->paths as $ path ) {
36+ if (is_dir ($ path )) {
37+ $ flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS ;
38+ $ directoryIterator = new \RecursiveDirectoryIterator ($ path , $ flags );
39+ } else {
40+ $ directoryIterator = new \RecursiveArrayIterator ([$ path ]);
6041 }
6142
62- $ files -> path ( $ path );
63- }
43+ $ filter = new TwigFileFilter ( $ directoryIterator );
44+ $ iterator = new \ RecursiveIteratorIterator ( $ filter );
6445
65- // Exclude all matching paths.
66- if ($ exclude ) {
67- $ files ->exclude ($ exclude );
46+ /** @var \SplFileInfo $file */
47+ foreach ($ iterator as $ file ) {
48+ $ files [] = $ file ->getRealPath ();
49+ }
6850 }
6951
7052 return $ files ;
7153 }
72-
73- /**
74- * @param string $key
75- *
76- * @return mixed
77- *
78- * @throws Exception
79- */
80- public function get (string $ key )
81- {
82- if (!isset ($ this ->config [$ key ])) {
83- throw new Exception (sprintf ('Configuration key "%s" does not exist ' , $ key ));
84- }
85-
86- return $ this ->config [$ key ];
87- }
8854}
0 commit comments