Skip to content

Commit a76381e

Browse files
committed
Move logic to rewriteFileContents
- Change rewriteFileContents to PathInfo - Move logic around getting file contents and modifying PathInfo from processIter to rewriteFileContents
1 parent 8248489 commit a76381e

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/PostProcessor.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,8 @@ public function processIter(
9393
foreach ( $crawl_responses as $crawled ) {
9494
$content_type = $crawled->content_type;
9595
if ( $content_type && $this->processContentType( $content_type ) ) {
96-
if ( $crawled->body ) {
97-
$rewritten = $this->rewriteFileContents(
98-
$crawled->body,
99-
);
100-
if ( $rewritten !== $crawled->body ) {
101-
$crawled = $crawled->withBody( $rewritten );
102-
}
103-
++$this->processed;
104-
} elseif ( $crawled->filename ) {
105-
$file_contents = file_get_contents( $crawled->filename );
106-
$rewritten = $this->rewriteFileContents(
107-
$file_contents
108-
);
109-
if ( $rewritten !== $file_contents ) {
110-
$crawled = $crawled->withBody( $rewritten );
111-
}
96+
if ( $crawled->body || $crawled->filename ) {
97+
$crawled = $this->rewriteFileContents( $crawled );
11298
++$this->processed;
11399
}
114100
} else {
@@ -122,25 +108,31 @@ public function processIter(
122108
}
123109

124110
/**
125-
* Rewrite URLs in a string to destination_url
126-
*
127-
* @param string $file_contents
128-
* @return string
111+
* Rewrite strings in the body of a PathInfo
112+
* according to $this->config->replacement_patterns
129113
*/
130114
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 '';
115+
PathInfo $path_info
116+
): PathInfo {
117+
if ( $path_info->body !== null ) {
118+
$s = $path_info->body;
119+
} elseif ( $path_info->filename ) {
120+
$s = file_get_contents( $path_info->filename );
121+
if ( $s === false ) {
122+
throw WsLog::ex( 'Error reading file ' . $path_info->filename );
123+
}
136124
}
137125

138-
$rewritten_contents = strtr(
139-
$file_contents,
126+
$rewritten = strtr(
127+
$s,
140128
$this->config->replacement_patterns
141129
);
142130

143-
return $rewritten_contents;
131+
if ( $rewritten !== $s ) {
132+
return $path_info->withBody( $rewritten );
133+
}
134+
135+
return $path_info;
144136
}
145137

146138
public function complete(): void {

0 commit comments

Comments
 (0)