Skip to content

Commit e476f9f

Browse files
committed
Replace SimpleWriter config with PostProcessConfig
- Remove vars and constructor from SimpleWriter - Add $config arg instead - Add an extra check that if there are no hosts_to_write, we can skip everything
1 parent 8e24399 commit e476f9f

File tree

2 files changed

+21
-46
lines changed

2 files changed

+21
-46
lines changed

src/PostProcessor.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
namespace StaticDeploy;
99

1010
class PostProcessor {
11+
public PostProcessConfig $config;
1112
private int $processed = 0;
1213
private int $skipped = 0;
1314

1415
/**
1516
* PostProcessor constructor
1617
*/
1718
public function __construct() {
19+
$this->config = new PostProcessConfig();
1820
}
1921

2022
public function processContentType( string $content_type ): bool {
@@ -83,20 +85,30 @@ public function processStaticSite(
8385
public function processIter(
8486
\Iterator $crawl_responses
8587
): \Iterator {
88+
if ( ! $this->config->hosts_to_rewrite ) {
89+
return $crawl_responses;
90+
}
91+
8692
$rewriter = new SimpleRewriter();
8793
$process = function ( $crawl_responses ) use ( $rewriter ) {
8894
foreach ( $crawl_responses as $crawled ) {
8995
$content_type = $crawled->content_type;
9096
if ( $content_type && $this->processContentType( $content_type ) ) {
9197
if ( $crawled->body ) {
92-
$rewritten = $rewriter->rewriteFileContents( $crawled->body );
98+
$rewritten = $rewriter->rewriteFileContents(
99+
$this->config,
100+
$crawled->body,
101+
);
93102
if ( $rewritten !== $crawled->body ) {
94103
$crawled = $crawled->withBody( $rewritten );
95104
}
96105
++$this->processed;
97106
} elseif ( $crawled->filename ) {
98107
$file_contents = file_get_contents( $crawled->filename );
99-
$rewritten = $rewriter->rewriteFileContents( $file_contents );
108+
$rewritten = $rewriter->rewriteFileContents(
109+
$this->config,
110+
$file_contents
111+
);
100112
if ( $rewritten !== $file_contents ) {
101113
$crawled = $crawled->withBody( $rewritten );
102114
}

src/SimpleRewriter.php

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,23 @@
99
namespace StaticDeploy;
1010

1111
class SimpleRewriter {
12-
13-
/**
14-
* @var string
15-
*/
16-
private $destination_url;
17-
18-
/**
19-
* @var array
20-
*/
21-
private $hosts_to_rewrite;
22-
23-
/**
24-
* @var string
25-
*/
26-
private $site_url;
27-
28-
/**
29-
* @var boolean
30-
*/
31-
private $skip_url_rewrite;
32-
33-
public function __construct() {
34-
$this->destination_url = apply_filters(
35-
Controller::getHookName( 'set_destination_url' ),
36-
Options::getValue( 'deploymentURL' )
37-
);
38-
$this->hosts_to_rewrite = Options::getLineDelimitedBlobValue( 'hostsToRewrite' );
39-
$this->site_url = apply_filters(
40-
Controller::getHookName( 'set_wordpress_site_url' ),
41-
untrailingslashit( SiteInfo::getUrl( 'site' ) )
42-
);
43-
$url_rewrite = (int) Options::getValue( 'skipURLRewrite' );
44-
$this->skip_url_rewrite = $url_rewrite === 1 ? true : false;
45-
}
46-
4712
/**
4813
* Rewrite URLs in a string to destination_url
4914
*
5015
* @param string $file_contents
5116
* @return string
5217
*/
53-
public function rewriteFileContents( string $file_contents ): string
54-
{
18+
public function rewriteFileContents(
19+
PostProcessConfig $config,
20+
string $file_contents,
21+
): string {
5522
// TODO: allow empty file saving here? Exception for style.css
5623
if ( ! $file_contents ) {
5724
return '';
5825
}
5926

60-
if ( $this->skip_url_rewrite ) {
61-
return $file_contents;
62-
}
63-
64-
$wordpress_site_url = untrailingslashit( $this->site_url );
65-
$destination_url = untrailingslashit( $this->destination_url );
27+
$wordpress_site_url = untrailingslashit( $config->site_url );
28+
$destination_url = untrailingslashit( $config->destination_url );
6629
$destination_url_c = addcslashes( $destination_url, '/' );
6730
$destination_url_rel = URLHelper::getProtocolRelativeURL( $destination_url );
6831
$destination_url_rel_c = addcslashes( $destination_url_rel, '/' );
@@ -75,7 +38,7 @@ public function rewriteFileContents( string $file_contents ): string
7538
addcslashes( URLHelper::getProtocolRelativeURL( $destination_url ), '/' ),
7639
];
7740

78-
foreach ( $this->hosts_to_rewrite as $host ) {
41+
foreach ( $config->hosts_to_rewrite as $host ) {
7942
if ( $host ) {
8043
$host_rel = URLHelper::getProtocolRelativeURL( 'http://' . $host );
8144
$host_rel_c = addcslashes( $host_rel, '/' );

0 commit comments

Comments
 (0)