Skip to content

Commit 2553620

Browse files
committed
Add DiffHelper::calculateFiles()
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent a4f07c0 commit 2553620

File tree

3 files changed

+72
-32
lines changed

3 files changed

+72
-32
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ use Jfcherng\Diff\Diff;
4343
use Jfcherng\Diff\DiffHelper;
4444
use Jfcherng\Diff\Factory\RendererFactory;
4545

46+
$oldFile = __DIR__ . '/example/old_file.txt';
47+
$newFile = __DIR__ . '/example/new_file.txt';
48+
4649
$old = 'This is the old string.';
4750
$new = 'And this is the new one.';
4851

@@ -78,7 +81,9 @@ $templateOptions = [
7881
'outputTagAsString' => true,
7982
];
8083

81-
// one-line simple usage
84+
// one-line simply compare two files
85+
$result = DiffHelper::calculateFiles($oldFile, $newFile, $diffOptions, $templateOptions);
86+
// one-line simply compare two strings
8287
$result = DiffHelper::calculate($old, $new, $template, $diffOptions, $templateOptions);
8388
// or even shorter if you are happy with default options
8489
$result = DiffHelper::calculate($old, $new, $template);

example/demo.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use Jfcherng\Diff\DiffHelper;
1414

1515
// include two sample files for comparison
16-
$old_file = \file_get_contents(__DIR__ . '/old_file.txt');
17-
$new_file = \file_get_contents(__DIR__ . '/new_file.txt');
16+
$oldFilePath = __DIR__ . '/old_file.txt';
17+
$newFilePath = __DIR__ . '/new_file.txt';
18+
$oldFile = \file_get_contents($oldFilePath);
19+
$newFile = \file_get_contents($newFilePath);
1820

1921
// options for Diff class
2022
$diffOptions = [
@@ -52,8 +54,8 @@
5254

5355
// demo the no-inline-detail diff
5456
$result = DiffHelper::calculate(
55-
$old_file,
56-
$new_file,
57+
$oldFile,
58+
$newFile,
5759
'Inline',
5860
$diffOptions,
5961
['detailLevel' => 'none'] + $templateOptions
@@ -68,8 +70,8 @@
6870

6971
// demo the word-level diff
7072
$result = DiffHelper::calculate(
71-
$old_file,
72-
$new_file,
73+
$oldFile,
74+
$newFile,
7375
'Inline',
7476
$diffOptions,
7577
['detailLevel' => 'line'] + $templateOptions
@@ -84,8 +86,8 @@
8486

8587
// demo the word-level diff
8688
$result = DiffHelper::calculate(
87-
$old_file,
88-
$new_file,
89+
$oldFile,
90+
$newFile,
8991
'Inline',
9092
$diffOptions,
9193
['detailLevel' => 'word'] + $templateOptions
@@ -100,8 +102,8 @@
100102

101103
// demo the character-level diff
102104
$result = DiffHelper::calculate(
103-
$old_file,
104-
$new_file,
105+
$oldFile,
106+
$newFile,
105107
'Inline',
106108
$diffOptions,
107109
['detailLevel' => 'char'] + $templateOptions
@@ -115,9 +117,9 @@
115117
<?php
116118

117119
// generate a side by side diff
118-
$result = DiffHelper::calculate(
119-
$old_file,
120-
$new_file,
120+
$result = DiffHelper::calculateFiles(
121+
$oldFilePath,
122+
$newFilePath,
121123
'SideBySide',
122124
$diffOptions,
123125
$templateOptions
@@ -131,9 +133,9 @@
131133
<?php
132134

133135
// generate an inline diff
134-
$result = DiffHelper::calculate(
135-
$old_file,
136-
$new_file,
136+
$result = DiffHelper::calculateFiles(
137+
$oldFilePath,
138+
$newFilePath,
137139
'Inline',
138140
$diffOptions,
139141
$templateOptions
@@ -147,9 +149,9 @@
147149
<pre><?php
148150

149151
// generate a unified diff
150-
$result = DiffHelper::calculate(
151-
$old_file,
152-
$new_file,
152+
$result = DiffHelper::calculateFiles(
153+
$oldFilePath,
154+
$newFilePath,
153155
'Unified',
154156
$diffOptions,
155157
$templateOptions
@@ -163,9 +165,9 @@
163165
<pre><?php
164166

165167
// generate a context diff
166-
$result = DiffHelper::calculate(
167-
$old_file,
168-
$new_file,
168+
$result = DiffHelper::calculateFiles(
169+
$oldFilePath,
170+
$newFilePath,
169171
'Context',
170172
$diffOptions,
171173
$templateOptions
@@ -179,9 +181,9 @@
179181
<pre><?php
180182

181183
// generate a JSON diff
182-
$result = DiffHelper::calculate(
183-
$old_file,
184-
$new_file,
184+
$result = DiffHelper::calculateFiles(
185+
$oldFilePath,
186+
$newFilePath,
185187
'Json',
186188
$diffOptions,
187189
['outputTagAsString' => true] + $templateOptions

src/DiffHelper.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public static function getAvailableTemplates(): array
7474
}
7575

7676
/**
77-
* All-in-one static method to calculate the diff.
77+
* All-in-one static method to calculate the diff between two strings (or arrays of strings).
7878
*
7979
* @param string|string[] $old the old string (or array of lines)
8080
* @param string|string[] $new the new string (or array of lines)
8181
* @param string $template the template name
8282
* @param array $diffOptions the options for Diff object
8383
* @param array $templateOptions the options for template object
8484
*
85-
* @return string the difference
85+
* @return string the rendered differences
8686
*/
8787
public static function calculate(
8888
$old,
@@ -98,9 +98,42 @@ public static function calculate(
9898
return Diff::getInstance()
9999
->setOldNew($old, $new)
100100
->setOptions($diffOptions)
101-
->render(
102-
RendererFactory::getInstance($template)
103-
->setOptions($templateOptions)
104-
);
101+
->render(RendererFactory::getInstance($template)->setOptions($templateOptions));
102+
}
103+
104+
/**
105+
* All-in-one static method to calculate the diff between two files.
106+
*
107+
* @param string $old the path of the old file
108+
* @param string $new the path of the new file
109+
* @param string $template the template name
110+
* @param array $diffOptions the options for Diff object
111+
* @param array $templateOptions the options for template object
112+
*
113+
* @throws \LogicException path is a directory
114+
* @throws \RuntimeException path cannot be opened
115+
*
116+
* @return string the rendered differences
117+
*/
118+
public static function calculateFiles(
119+
string $old,
120+
string $new,
121+
string $template = 'Unified',
122+
array $diffOptions = [],
123+
array $templateOptions = []
124+
): string {
125+
// we want to leave the line-ending problem to static::calculate()
126+
// so do not set SplFileObject::DROP_NEW_LINE flag
127+
// otherwise, we will lose \r if the line-ending is \r\n
128+
$oldFile = new \SplFileObject($old, 'r');
129+
$newFile = new \SplFileObject($new, 'r');
130+
131+
return static::calculate(
132+
$oldFile->fread($oldFile->getSize()),
133+
$newFile->fread($newFile->getSize()),
134+
$template,
135+
$diffOptions,
136+
$templateOptions
137+
);
105138
}
106139
}

0 commit comments

Comments
 (0)