Skip to content

Commit 6226066

Browse files
committed
Move replacement patterns to PostProcessConfig
This should save a lot of time by generating the patterns just once and not on each file.
1 parent e476f9f commit 6226066

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

src/PostProcessConfig.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ final class PostProcessConfig {
66

77
public readonly ?string $destination_url;
88
public readonly ?array $hosts_to_rewrite;
9+
public readonly ?array $replacement_patterns;
910
public readonly ?string $site_url;
1011

1112
public function __construct() {
1213
if ( Options::getValue( 'skipURLRewrite' ) === '1' ) {
1314
$this->destination_url = null;
1415
$this->hosts_to_rewrite = null;
16+
$this->replacement_patterns = null;
1517
$this->site_url = null;
1618
return;
1719
}
@@ -25,6 +27,40 @@ public function __construct() {
2527
Controller::getHookName( 'set_wordpress_site_url' ),
2628
untrailingslashit( SiteInfo::getUrl( 'site' ) )
2729
);
30+
31+
$wordpress_site_url = untrailingslashit( $this->site_url );
32+
$destination_url = untrailingslashit( $this->destination_url );
33+
$destination_url_c = addcslashes( $destination_url, '/' );
34+
$destination_url_rel = URLHelper::getProtocolRelativeURL( $destination_url );
35+
$destination_url_rel_c = addcslashes( $destination_url_rel, '/' );
36+
37+
$replacement_patterns = [
38+
$wordpress_site_url => $destination_url,
39+
URLHelper::getProtocolRelativeURL( $wordpress_site_url ) =>
40+
URLHelper::getProtocolRelativeURL( $destination_url ),
41+
addcslashes( URLHelper::getProtocolRelativeURL( $wordpress_site_url ), '/' ) =>
42+
addcslashes( URLHelper::getProtocolRelativeURL( $destination_url ), '/' ),
43+
];
44+
45+
foreach ( $this->hosts_to_rewrite as $host ) {
46+
if ( $host ) {
47+
$host_rel = URLHelper::getProtocolRelativeURL( 'http://' . $host );
48+
$host_rel_c = addcslashes( $host_rel, '/' );
49+
50+
$replacement_patterns[ 'http:' . $host_rel ] = $destination_url;
51+
$replacement_patterns[ 'https:' . $host_rel ] = $destination_url;
52+
$replacement_patterns[ $host_rel ] = $destination_url_rel;
53+
$replacement_patterns[ 'http:' . $host_rel_c ] = $destination_url_c;
54+
$replacement_patterns[ 'https:' . $host_rel_c ] = $destination_url_c;
55+
$replacement_patterns[ addcslashes( $host_rel, '/' ) ] = $destination_url_rel_c;
56+
}
57+
}
58+
59+
$this->replacement_patterns = $replacement_patterns;
60+
61+
if ( STATIC_DEPLOY_DEBUG ) {
62+
WsLog::l( 'PostProcessConfig: ' . json_encode( $this->toArray() ) );
63+
}
2864
}
2965

3066
/**
@@ -42,6 +78,10 @@ public function toArray(): array
4278
$arr['hosts_to_rewrite'] = $this->hosts_to_rewrite;
4379
}
4480

81+
if ( $this->replacement_patterns ) {
82+
$arr['replacement_patterns'] = $this->replacement_patterns;
83+
}
84+
4585
if ( $this->site_url ) {
4686
$arr['site_url'] = $this->site_url;
4787
}

src/PostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function processStaticSite(
8585
public function processIter(
8686
\Iterator $crawl_responses
8787
): \Iterator {
88-
if ( ! $this->config->hosts_to_rewrite ) {
88+
if ( ! $this->config->replacement_patterns ) {
8989
return $crawl_responses;
9090
}
9191

src/SimpleRewriter.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,9 @@ public function rewriteFileContents(
2424
return '';
2525
}
2626

27-
$wordpress_site_url = untrailingslashit( $config->site_url );
28-
$destination_url = untrailingslashit( $config->destination_url );
29-
$destination_url_c = addcslashes( $destination_url, '/' );
30-
$destination_url_rel = URLHelper::getProtocolRelativeURL( $destination_url );
31-
$destination_url_rel_c = addcslashes( $destination_url_rel, '/' );
32-
33-
$replacement_patterns = [
34-
$wordpress_site_url => $destination_url,
35-
URLHelper::getProtocolRelativeURL( $wordpress_site_url ) =>
36-
URLHelper::getProtocolRelativeURL( $destination_url ),
37-
addcslashes( URLHelper::getProtocolRelativeURL( $wordpress_site_url ), '/' ) =>
38-
addcslashes( URLHelper::getProtocolRelativeURL( $destination_url ), '/' ),
39-
];
40-
41-
foreach ( $config->hosts_to_rewrite as $host ) {
42-
if ( $host ) {
43-
$host_rel = URLHelper::getProtocolRelativeURL( 'http://' . $host );
44-
$host_rel_c = addcslashes( $host_rel, '/' );
45-
46-
$replacement_patterns[ 'http:' . $host_rel ] = $destination_url;
47-
$replacement_patterns[ 'https:' . $host_rel ] = $destination_url;
48-
$replacement_patterns[ $host_rel ] = $destination_url_rel;
49-
$replacement_patterns[ 'http:' . $host_rel_c ] = $destination_url_c;
50-
$replacement_patterns[ 'https:' . $host_rel_c ] = $destination_url_c;
51-
$replacement_patterns[ addcslashes( $host_rel, '/' ) ] = $destination_url_rel_c;
52-
}
53-
}
54-
5527
$rewritten_contents = strtr(
5628
$file_contents,
57-
$replacement_patterns
29+
$config->replacement_patterns
5830
);
5931

6032
return $rewritten_contents;

0 commit comments

Comments
 (0)