22
33namespace WP2Static ;
44
5+ use PHLAK \Splat \Anchors ;
6+ use PHLAK \Splat \Pattern ;
57use WP2Static \CoreOptions ;
8+ use WP2Static \FileIgnorePattern ;
69use WP2Static \SiteInfo ;
710
811class FileFiltering {
912
1013 /**
11- * @var array<string >
12- * File and directory names to ignore
14+ * @var array<FileIgnorePattern >
15+ * Files and directories to ignore
1316 */
14- private $ filenames_to_ignore ;
15-
16- /**
17- * @var array<string>
18- * File extensions to ignore
19- */
20- private $ file_extensions_to_ignore ;
17+ private $ patterns_to_ignore ;
2118
2219 public function __construct () {
20+ $ this ->patterns_to_ignore = [];
21+
2322 $ filenames_to_ignore = CoreOptions::getLineDelimitedBlobValue ( 'filenamesToIgnore ' );
2423
25- $ this -> filenames_to_ignore =
24+ $ filenames_to_ignore =
2625 apply_filters (
2726 'wp2static_filenames_to_ignore ' ,
2827 $ filenames_to_ignore
2928 );
3029
30+ foreach ( $ filenames_to_ignore as $ filename ) {
31+ $ this ->patterns_to_ignore [] = new FileIgnorePattern ( $ filename );
32+ }
33+
3134 $ file_extensions_to_ignore = CoreOptions::getLineDelimitedBlobValue (
3235 'fileExtensionsToIgnore '
3336 );
3437
35- $ this -> file_extensions_to_ignore =
38+ $ file_extensions_to_ignore =
3639 apply_filters (
3740 'wp2static_file_extensions_to_ignore ' ,
3841 $ file_extensions_to_ignore
3942 );
43+
44+ foreach ( $ file_extensions_to_ignore as $ extension ) {
45+ $ this ->patterns_to_ignore [] = new FileIgnorePattern (
46+ "** $ extension "
47+ );
48+ }
4049 }
4150
4251 /**
@@ -49,6 +58,8 @@ public function __construct() {
4958 public function crawlableFiles (
5059 string $ directory ,
5160 ) : \Iterator {
61+ $ abs_base_dir = ( new \SplFileInfo ( $ directory ) )->getPathname ();
62+
5263 $ dir_iter = new \RecursiveDirectoryIterator (
5364 $ directory ,
5465 \RecursiveDirectoryIterator::SKIP_DOTS ,
@@ -59,32 +70,14 @@ public function crawlableFiles(
5970 // blocked directories.
6071 $ filter_iter = new \RecursiveCallbackFilterIterator (
6172 $ dir_iter ,
62- function ( $ current , $ key , $ iterator ) {
63- $ filename = $ current ->getFilename ();
64-
73+ function ( $ current , $ key , $ iterator ) use ( $ abs_base_dir ) {
6574 // Filter out both directories and files
66- foreach ( $ this ->filenames_to_ignore as $ filename_to_ignore ) {
67- if ( $ filename === $ filename_to_ignore ) {
75+ foreach ( $ this ->patterns_to_ignore as $ pattern ) {
76+ if ( $ pattern -> matches ( $ abs_base_dir , $ current ) ) {
6877 return false ;
6978 }
7079 }
7180
72- // Filter only files
73- if ( $ current ->isFile () ) {
74- /*
75- Prepare the file extension list for regex:
76- - Add prepending (escaped) \ for a literal . at the start of
77- the file extension
78- - Add $ at the end to match end of string
79- - Add i modifier for case insensitivity
80- */
81- foreach ( $ this ->file_extensions_to_ignore as $ extension ) {
82- if ( preg_match ( "/ \\{$ extension }$/i " , $ filename ) ) {
83- return false ;
84- }
85- }
86- }
87-
8881 return true ;
8982 }
9083 );
@@ -118,33 +111,14 @@ public function getListOfLocalFilesByDir(
118111 }
119112
120113 /**
121- * Ensure a given filepath has an allowed filename and extension.
122- *
123- * @param string $file_name
124- * @return bool True if the given file does not have a disallowed filename
125- * or extension.
114+ * @param string $path
115+ * @return bool True if the given path does not match an ignore pattern
126116 */
127117 public function pathLooksCrawlable (
128- string $ file_name ,
118+ string $ path ,
129119 ) : bool {
130- $ filename_matches = 0 ;
131-
132- str_ireplace ( $ this ->filenames_to_ignore , '' , $ file_name , $ filename_matches );
133-
134- // If we found matches we don't need to go any further
135- if ( $ filename_matches ) {
136- return false ;
137- }
138-
139- /*
140- Prepare the file extension list for regex:
141- - Add prepending (escaped) \ for a literal . at the start of
142- the file extension
143- - Add $ at the end to match end of string
144- - Add i modifier for case insensitivity
145- */
146- foreach ( $ this ->file_extensions_to_ignore as $ extension ) {
147- if ( preg_match ( "/ \\{$ extension }$/i " , $ file_name ) ) {
120+ foreach ( $ this ->patterns_to_ignore as $ pattern ) {
121+ if ( $ pattern ->matchesPath ( $ path ) ) {
148122 return false ;
149123 }
150124 }
0 commit comments