Skip to content

Commit 8248489

Browse files
committed
Move rewriteFileContents to PostProcessor
1 parent 6226066 commit 8248489

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

src/PostProcessor.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,12 @@ public function processIter(
8989
return $crawl_responses;
9090
}
9191

92-
$rewriter = new SimpleRewriter();
93-
$process = function ( $crawl_responses ) use ( $rewriter ) {
92+
$process = function ( $crawl_responses ) {
9493
foreach ( $crawl_responses as $crawled ) {
9594
$content_type = $crawled->content_type;
9695
if ( $content_type && $this->processContentType( $content_type ) ) {
9796
if ( $crawled->body ) {
98-
$rewritten = $rewriter->rewriteFileContents(
99-
$this->config,
97+
$rewritten = $this->rewriteFileContents(
10098
$crawled->body,
10199
);
102100
if ( $rewritten !== $crawled->body ) {
@@ -105,8 +103,7 @@ public function processIter(
105103
++$this->processed;
106104
} elseif ( $crawled->filename ) {
107105
$file_contents = file_get_contents( $crawled->filename );
108-
$rewritten = $rewriter->rewriteFileContents(
109-
$this->config,
106+
$rewritten = $this->rewriteFileContents(
110107
$file_contents
111108
);
112109
if ( $rewritten !== $file_contents ) {
@@ -124,6 +121,28 @@ public function processIter(
124121
return $process( $crawl_responses );
125122
}
126123

124+
/**
125+
* Rewrite URLs in a string to destination_url
126+
*
127+
* @param string $file_contents
128+
* @return string
129+
*/
130+
public function rewriteFileContents(
131+
string $file_contents,
132+
): string {
133+
// TODO: allow empty file saving here? Exception for style.css
134+
if ( ! $file_contents ) {
135+
return '';
136+
}
137+
138+
$rewritten_contents = strtr(
139+
$file_contents,
140+
$this->config->replacement_patterns
141+
);
142+
143+
return $rewritten_contents;
144+
}
145+
127146
public function complete(): void {
128147
WsLog::l(
129148
"Post processing complete. $this->processed processed, $this->skipped skipped."

src/SimpleRewriter.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)