Skip to content

Commit f4a196b

Browse files
committed
Convert remaining array detectors to iterators
1 parent 4f362aa commit f4a196b

9 files changed

Lines changed: 42 additions & 76 deletions

src/DetectArchiveURLs.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ class DetectArchiveURLs {
1313
* https://foo.com/2020/05/
1414
* https://foo.com/2020/
1515
*
16-
* @return string[] list of archive URLs
16+
* @return \Iterator<array>
1717
*/
18-
public static function detect( bool $log = false ) : array {
18+
public static function detect( bool $log = false ) : \Iterator {
1919
if ( $log ) {
2020
WsLog::l( 'Detecting archive URLs' );
2121
}
2222

2323
global $wpdb;
2424

25-
$archive_urls = [];
26-
2725
$archive_urls_with_markup = '';
2826

2927
$yearly_archives = wp_get_archives(
@@ -59,6 +57,8 @@ public static function detect( bool $log = false ) : array {
5957
$url_matching_regex = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#';
6058
preg_match_all( $url_matching_regex, $archive_urls_with_markup, $matches );
6159

62-
return $matches[0];
60+
foreach ( $matches[0] as $url ) {
61+
yield [ 'url' => $url ];
62+
}
6363
}
6464
}

src/DetectAuthorPaginationURLs.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class DetectAuthorPaginationURLs {
77
/**
88
* Detect Author Pagination URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( string $wp_site_url, bool $log = false ) : array {
12+
public static function detect( string $wp_site_url, bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting author pagination URLs' );
1515
}
@@ -18,7 +18,6 @@ public static function detect( string $wp_site_url, bool $log = false ) : array
1818

1919
$public = true;
2020
$authors_urls = [];
21-
$urls_to_include = [];
2221
$users = get_users();
2322
$pagination_base = $wp_rewrite->pagination_base;
2423

@@ -55,11 +54,8 @@ public static function detect( string $wp_site_url, bool $log = false ) : array
5554
$total_pages = ceil( $total_posts / $default_posts_per_page );
5655

5756
for ( $page = 1; $page <= $total_pages; $page++ ) {
58-
$urls_to_include[] =
59-
"/{$author}{$pagination_base}/{$page}/";
57+
yield [ 'url' => "/{$author}{$pagination_base}/{$page}/" ];
6058
}
6159
}
62-
63-
return $urls_to_include;
6460
}
6561
}

src/DetectAuthorsURLs.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ class DetectAuthorsURLs {
77
/**
88
* Detect Authors URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( bool $log = false ) : array {
12+
public static function detect( bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting author URLs' );
1515
}
1616

1717
global $wp_rewrite, $wpdb;
1818

19-
$authors_urls = [];
2019
$users = get_users();
2120

2221
foreach ( $users as $author ) {
@@ -28,9 +27,7 @@ public static function detect( bool $log = false ) : array {
2827

2928
$permalink = trim( $author_link );
3029

31-
$authors_urls[] = $permalink;
30+
yield [ 'url' => $permalink ];
3231
}
33-
34-
return $authors_urls;
3532
}
3633
}

src/DetectCategoryPaginationURLs.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class DetectCategoryPaginationURLs {
77
/**
88
* Detect Category Pagination URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( bool $log = false ) : array {
12+
public static function detect( bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting category pagination URLs' );
1515
}
@@ -22,7 +22,6 @@ public static function detect( bool $log = false ) : array {
2222
$args = [ 'public' => true ];
2323

2424
$category_links = [];
25-
$urls_to_include = [];
2625
$taxonomies = get_taxonomies( $args, 'objects' );
2726
$pagination_base = $wp_rewrite->pagination_base;
2827
$default_posts_per_page = get_option( 'posts_per_page' );
@@ -55,11 +54,8 @@ public static function detect( bool $log = false ) : array {
5554
$total_pages = ceil( $total_posts / $default_posts_per_page );
5655

5756
for ( $page = 1; $page <= $total_pages; $page++ ) {
58-
$urls_to_include[] =
59-
"{$term}{$pagination_base}/{$page}/";
57+
yield [ 'url' => "{$term}{$pagination_base}/{$page}/" ];
6058
}
6159
}
62-
63-
return $urls_to_include;
6460
}
6561
}

src/DetectCategoryURLs.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class DetectCategoryURLs {
77
/**
88
* Detect Category URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( bool $log = false ) : array {
12+
public static function detect( bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting category URLs' );
1515
}
@@ -20,8 +20,6 @@ public static function detect( bool $log = false ) : array {
2020

2121
$taxonomies = get_taxonomies( $args, 'objects' );
2222

23-
$category_urls = [];
24-
2523
foreach ( $taxonomies as $taxonomy ) {
2624
/** @var list<\WP_Term> $terms */
2725
$terms = get_terms(
@@ -39,10 +37,8 @@ public static function detect( bool $log = false ) : array {
3937

4038
$permalink = trim( $term_link );
4139

42-
$category_urls[] = $permalink;
40+
yield [ 'url' => $permalink ];
4341
}
4442
}
45-
46-
return $category_urls;
4743
}
4844
}

src/DetectPageURLs.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ class DetectPageURLs {
77
/**
88
* Detect Page URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( bool $log = false ) : array {
12+
public static function detect( bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting page URLs' );
1515
}
1616

1717
global $wpdb;
1818

19-
$page_urls = [];
20-
2119
$page_ids = $wpdb->get_col(
2220
"SELECT ID
2321
FROM {$wpdb->posts}
@@ -32,9 +30,7 @@ public static function detect( bool $log = false ) : array {
3230
continue;
3331
}
3432

35-
$page_urls[] = $permalink;
33+
yield [ 'url' => $permalink ];
3634
}
37-
38-
return $page_urls;
3935
}
4036
}

src/DetectPostURLs.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ class DetectPostURLs {
77
/**
88
* Detect Post URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( bool $log = false ) : array {
12+
public static function detect( bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting post URLs' );
1515
}
1616

1717
global $wpdb;
1818

19-
$post_urls = [];
20-
2119
$post_ids = $wpdb->get_col(
2220
"SELECT ID
2321
FROM {$wpdb->posts}
@@ -36,9 +34,7 @@ public static function detect( bool $log = false ) : array {
3634
continue;
3735
}
3836

39-
$post_urls[] = $permalink;
37+
yield [ 'url' => $permalink ];
4038
}
41-
42-
return $post_urls;
4339
}
4440
}

src/DetectPostsPaginationURLs.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class DetectPostsPaginationURLs {
77
/**
88
* Detect Post pagination URLs
99
*
10-
* @return string[] list of URLs
10+
* @return \Iterator<array> list of URLs
1111
*/
12-
public static function detect( string $wp_site_url, bool $log = false ) : array {
12+
public static function detect( string $wp_site_url, bool $log = false ) : \Iterator {
1313
if ( $log ) {
1414
WsLog::l( 'Detecting post pagination URLs' );
1515
}
@@ -45,8 +45,6 @@ public static function detect( string $wp_site_url, bool $log = false ) : array
4545
$pagination_base = $wp_rewrite->pagination_base;
4646
$default_posts_per_page = get_option( 'posts_per_page' );
4747

48-
$urls_to_include = [];
49-
5048
foreach ( $post_types as $post_type ) {
5149
$query = "SELECT COUNT(*) FROM %s WHERE post_status = '%s'" .
5250
" AND post_type = '%s'";
@@ -106,14 +104,11 @@ public static function detect( string $wp_site_url, bool $log = false ) : array
106104
}
107105
}
108106

109-
$urls_to_include[] = "/{$post_archive_slug}{$pagination_base}/{$page}/";
107+
yield [ 'url' => "/{$post_archive_slug}{$pagination_base}/{$page}/" ];
110108
} else {
111-
$urls_to_include[] =
112-
"/{$plural_form}/{$pagination_base}/{$page}/";
109+
yield [ 'url' => "/{$plural_form}/{$pagination_base}/{$page}/" ];
113110
}
114111
}
115112
}
116-
117-
return $urls_to_include;
118113
}
119114
}

src/URLDetector.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ public static function detectURLsIter( bool $quiet = false ) : \Iterator {
4242

4343
$filtering = new FileFiltering();
4444

45-
$arrays_to_merge = [];
46-
47-
$arrays_to_merge[] = [
48-
'/',
49-
'/robots.txt',
50-
'/favicon.ico',
51-
'/sitemap.xml',
52-
];
53-
5445
$iterators_to_merge = [];
5546

47+
$iterators_to_merge[] = new \ArrayIterator( [
48+
[ 'url' => '/' ],
49+
[ 'url' => '/robots.txt' ],
50+
[ 'url' => '/favicon.ico' ],
51+
[ 'url' => '/sitemap.xml' ],
52+
] );
53+
5654
$detect_parent_theme = apply_filters( 'wp2static_detect_parent_theme', 1 );
5755

5856
if ( $detect_parent_theme ) {
@@ -97,11 +95,11 @@ public static function detectURLsIter( bool $quiet = false ) : \Iterator {
9795
}
9896

9997
if ( CoreOptions::getValue( 'detectPosts' ) ) {
100-
$arrays_to_merge[] = DetectPostURLs::detect( log: $log_steps );
98+
$iterators_to_merge[] = DetectPostURLs::detect( log: $log_steps );
10199
}
102100

103101
if ( CoreOptions::getValue( 'detectPages' ) ) {
104-
$arrays_to_merge[] = DetectPageURLs::detect( log: $log_steps );
102+
$iterators_to_merge[] = DetectPageURLs::detect( log: $log_steps );
105103
}
106104

107105
if ( CoreOptions::getValue( 'detectCustomPostTypes' ) ) {
@@ -111,46 +109,42 @@ public static function detectURLsIter( bool $quiet = false ) : \Iterator {
111109
$detect_posts_pagination = apply_filters( 'wp2static_detect_posts_pagination', 1 );
112110

113111
if ( $detect_posts_pagination ) {
114-
$arrays_to_merge[] = DetectPostsPaginationURLs::detect( SiteInfo::getURL( 'site' ), log: $log_steps );
112+
$iterators_to_merge[] = DetectPostsPaginationURLs::detect( SiteInfo::getURL( 'site' ), log: $log_steps );
115113
}
116114

117115
$detect_archives = apply_filters( 'wp2static_detect_archives', 1 );
118116

119117
if ( $detect_archives ) {
120-
$arrays_to_merge[] = DetectArchiveURLs::detect( log: $log_steps );
118+
$iterators_to_merge[] = DetectArchiveURLs::detect( log: $log_steps );
121119
}
122120

123121
$detect_categories = apply_filters( 'wp2static_detect_categories', 1 );
124122

125123
if ( $detect_categories ) {
126-
$arrays_to_merge[] = DetectCategoryURLs::detect( log: $log_steps );
124+
$iterators_to_merge[] = DetectCategoryURLs::detect( log: $log_steps );
127125
}
128126

129127
$detect_category_pagination = apply_filters( 'wp2static_detect_category_pagination', 1 );
130128

131129
if ( $detect_category_pagination ) {
132-
$arrays_to_merge[] = DetectCategoryPaginationURLs::detect( log: $log_steps );
130+
$iterators_to_merge[] = DetectCategoryPaginationURLs::detect( log: $log_steps );
133131
}
134132

135133
$detect_authors = apply_filters( 'wp2static_detect_authors', 1 );
136134

137135
if ( $detect_authors ) {
138-
$arrays_to_merge[] = DetectAuthorsURLs::detect( log: $log_steps );
136+
$iterators_to_merge[] = DetectAuthorsURLs::detect( log: $log_steps );
139137
}
140138

141139
$detect_authors_pagination = apply_filters( 'wp2static_detect_authors_pagination', 1 );
142140

143141
if ( $detect_authors_pagination ) {
144-
$arrays_to_merge[] = DetectAuthorPaginationURLs::detect( SiteInfo::getUrl( 'site' ), log: $log_steps );
142+
$iterators_to_merge[] = DetectAuthorPaginationURLs::detect( SiteInfo::getUrl( 'site' ), log: $log_steps );
145143
}
146144

147145
$home_url = SiteInfo::getUrl( 'home' );
148146
$unique_urls = [];
149147

150-
foreach ( $arrays_to_merge as $array ) {
151-
$iterators_to_merge[] = new \ArrayIterator( $array );
152-
}
153-
154148
$last_log_time = microtime( true );
155149

156150
foreach ( $iterators_to_merge as $iter ) {

0 commit comments

Comments
 (0)