Skip to content

Commit 846d0ca

Browse files
committed
Rename the term "template" to "renderer"
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 8439977 commit 846d0ca

File tree

13 files changed

+103
-96
lines changed

13 files changed

+103
-96
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ $newFile = __DIR__ . '/example/new_file.txt';
4949
$old = 'This is the old string.';
5050
$new = 'And this is the new one.';
5151

52-
// template class name: Unified, Context, Json, Inline, SideBySide
53-
$template = 'Unified';
52+
// renderer class name: Unified, Context, Json, Inline, SideBySide
53+
$rendererName = 'Unified';
5454

5555
// the Diff class options
5656
$differOptions = [
@@ -62,35 +62,35 @@ $differOptions = [
6262
'ignoreWhitespace' => false,
6363
];
6464

65-
// the template class options
66-
$templateOptions = [
65+
// the renderer class options
66+
$rendererOptions = [
6767
// how detailed the rendered HTML in-line diff is? (none, line, word, char)
6868
'detailLevel' => 'line',
69-
// template language: eng, cht, chs, jpn, ...
69+
// renderer language: eng, cht, chs, jpn, ...
7070
// or an array which has the same keys with a language file
7171
'language' => 'eng',
72-
// show a separator between different diff hunks in HTML templates
72+
// show a separator between different diff hunks in HTML renderers
7373
'separateBlock' => true,
7474
// the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
7575
// but if you want to visualize them in the backend with "&nbsp;", you can set this to true
7676
'spacesToNbsp' => false,
77-
// HTML template tab width (negative = do not convert into spaces)
77+
// HTML renderer tab width (negative = do not convert into spaces)
7878
'tabSize' => 4,
7979
// internally, ops (tags) are all int type but this is not good for human reading.
8080
// set this to "true" to convert them into string form before outputting.
8181
'outputTagAsString' => true,
8282
];
8383

8484
// one-line simply compare two files
85-
$result = DiffHelper::calculateFiles($oldFile, $newFile, $differOptions, $templateOptions);
85+
$result = DiffHelper::calculateFiles($oldFile, $newFile, $rendererName, $differOptions, $rendererOptions);
8686
// one-line simply compare two strings
87-
$result = DiffHelper::calculate($old, $new, $template, $differOptions, $templateOptions);
87+
$result = DiffHelper::calculate($old, $new, $rendererName, $differOptions, $rendererOptions);
8888
// or even shorter if you are happy with default options
89-
$result = DiffHelper::calculate($old, $new, $template);
89+
$result = DiffHelper::calculate($old, $new, $rendererName);
9090

9191
// custom usage
9292
$differ = new Differ(explode("\n", $old), explode("\n", $new), $differOptions);
93-
$renderer = RendererFactory::make($template, $templateOptions); // or your own renderers
93+
$renderer = RendererFactory::make($rendererName, $rendererOptions); // or your own renderers
9494
$result = $renderer->render($differ);
9595
```
9696

UPGRADING/UPGRADING_v6.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ there is no breaking change for you so you do not have to do anything.
1212
- The `Diff` class has been renamed to `Differ`.
1313
It's easy to adapt to this by changing the class name.
1414

15+
- The term `template` has been renamed to `renderer`. Some examples are:
16+
17+
- Method `DiffHelper::getRenderersInfo()`
18+
- Method `DiffHelper::getAvailableRenderers()`
19+
- Constant `RendererConstant::RENDERER_TYPES`
20+
- Constant `AbstractRenderer::IS_TEXT_RENDERER`
21+
1522
- Now a `Renderer` has `render()` API, but a `Differ` does not.
1623
But if you use those classes by yourself, it should be written like below.
1724

@@ -20,7 +27,7 @@ there is no breaking change for you so you do not have to do anything.
2027
use Jfcherng\Diff\Factory\RendererFactory;
2128

2229
$differ = new Differ(explode("\n", $old), explode("\n", $new), $diffOptions);
23-
$renderer = RendererFactory::make($template, $templateOptions);
30+
$renderer = RendererFactory::make($renderer, $rendererOptions);
2431
$result = $renderer->render($differ); // <-- this has been changed
2532
```
2633

example/demo.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131
'ignoreWhitespace' => false,
3232
];
3333

34-
// options for template class
35-
$templateOptions = [
34+
// options for renderer class
35+
$rendererOptions = [
3636
// how detailed the rendered HTML is? (line, word, char)
3737
'detailLevel' => 'line',
38-
// template language: eng, cht, chs, jpn, ...
38+
// renderer language: eng, cht, chs, jpn, ...
3939
// or an array which has the same keys with a language file
4040
'language' => 'eng',
41-
// show a separator between different diff hunks in HTML templates
41+
// show a separator between different diff hunks in HTML renderers
4242
'separateBlock' => true,
4343
// the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
4444
// but if you want to visualize them in the backend with "&nbsp;", you can set this to true
4545
'spacesToNbsp' => false,
46-
// HTML template tab width (negative = do not convert into spaces)
46+
// HTML renderer tab width (negative = do not convert into spaces)
4747
'tabSize' => 4,
4848
// internally, ops (tags) are all int type but this is not good for human reading.
4949
// set this to "true" to convert them into string form before outputting.
@@ -61,7 +61,7 @@
6161
$newFile,
6262
'Inline',
6363
$diffOptions,
64-
['detailLevel' => 'none'] + $templateOptions
64+
['detailLevel' => 'none'] + $rendererOptions
6565
);
6666

6767
echo $result;
@@ -77,7 +77,7 @@
7777
$newFile,
7878
'Inline',
7979
$diffOptions,
80-
['detailLevel' => 'line'] + $templateOptions
80+
['detailLevel' => 'line'] + $rendererOptions
8181
);
8282

8383
echo $result;
@@ -93,7 +93,7 @@
9393
$newFile,
9494
'Inline',
9595
$diffOptions,
96-
['detailLevel' => 'word'] + $templateOptions
96+
['detailLevel' => 'word'] + $rendererOptions
9797
);
9898

9999
echo $result;
@@ -109,7 +109,7 @@
109109
$newFile,
110110
'Inline',
111111
$diffOptions,
112-
['detailLevel' => 'char'] + $templateOptions
112+
['detailLevel' => 'char'] + $rendererOptions
113113
);
114114

115115
echo $result;
@@ -125,7 +125,7 @@
125125
$newFilePath,
126126
'SideBySide',
127127
$diffOptions,
128-
$templateOptions
128+
$rendererOptions
129129
);
130130

131131
echo $result;
@@ -141,7 +141,7 @@
141141
$newFilePath,
142142
'Inline',
143143
$diffOptions,
144-
$templateOptions
144+
$rendererOptions
145145
);
146146

147147
echo $result;
@@ -157,7 +157,7 @@
157157
$newFilePath,
158158
'Unified',
159159
$diffOptions,
160-
$templateOptions
160+
$rendererOptions
161161
);
162162

163163
echo \htmlspecialchars($result);
@@ -173,7 +173,7 @@
173173
$newFilePath,
174174
'Context',
175175
$diffOptions,
176-
$templateOptions
176+
$rendererOptions
177177
);
178178

179179
echo \htmlspecialchars($result);
@@ -189,7 +189,7 @@
189189
$newFilePath,
190190
'Json',
191191
$diffOptions,
192-
['outputTagAsString' => true] + $templateOptions
192+
['outputTagAsString' => true] + $rendererOptions
193193
);
194194

195195
$beautified = \json_encode(

src/DiffHelper.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public static function getProjectDirectory(): string
2929
}
3030

3131
/**
32-
* Get the information about available templates.
32+
* Get the information about available renderers.
3333
*
3434
* @return array
3535
*/
36-
public static function getTemplatesInfo(): array
36+
public static function getRenderersInfo(): array
3737
{
3838
static $info;
3939

@@ -45,7 +45,7 @@ public static function getTemplatesInfo(): array
4545
static::getProjectDirectory(),
4646
'src',
4747
'Renderer',
48-
'{' . \implode(',', RendererConstant::TEMPLATE_TYPES) . '}',
48+
'{' . \implode(',', RendererConstant::RENDERER_TYPES) . '}',
4949
'*.php',
5050
]);
5151

@@ -58,7 +58,7 @@ function (string $file): string {
5858
\glob($glob, \GLOB_BRACE)
5959
);
6060

61-
$templates = \array_filter(
61+
$renderers = \array_filter(
6262
$fileNames,
6363
// only normal class files are wanted
6464
function (string $fileName): bool {
@@ -70,25 +70,25 @@ function (string $fileName): bool {
7070
);
7171

7272
$info = [];
73-
foreach ($templates as $template) {
74-
$info[$template] = RendererFactory::resolveTemplate($template)::INFO;
73+
foreach ($renderers as $renderer) {
74+
$info[$renderer] = RendererFactory::resolveRenderer($renderer)::INFO;
7575
}
7676

7777
return $info;
7878
}
7979

8080
/**
81-
* Get the available templates.
81+
* Get the available renderers.
8282
*
83-
* @return string[] the available templates
83+
* @return string[] the available renderers
8484
*/
85-
public static function getAvailableTemplates(): array
85+
public static function getAvailableRenderers(): array
8686
{
87-
return \array_keys(self::getTemplatesInfo());
87+
return \array_keys(self::getRenderersInfo());
8888
}
8989

9090
/**
91-
* Get the content of the CSS style sheet for HTML templates.
91+
* Get the content of the CSS style sheet for HTML renderers.
9292
*
9393
* @throws \LogicException path is a directory
9494
* @throws \RuntimeException path cannot be opened
@@ -115,25 +115,25 @@ public static function getStyleSheet(): string
115115
*
116116
* @param string|string[] $old the old string (or array of lines)
117117
* @param string|string[] $new the new string (or array of lines)
118-
* @param string $template the template name
118+
* @param string $renderer the renderer name
119119
* @param array $differOptions the options for Differ object
120-
* @param array $templateOptions the options for template object
120+
* @param array $rendererOptions the options for renderer object
121121
*
122122
* @return string the rendered differences
123123
*/
124124
public static function calculate(
125125
$old,
126126
$new,
127-
string $template = 'Unified',
127+
string $renderer = 'Unified',
128128
array $differOptions = [],
129-
array $templateOptions = []
129+
array $rendererOptions = []
130130
): string {
131131
// always convert into array form
132132
\is_string($old) && ($old = \explode("\n", $old));
133133
\is_string($new) && ($new = \explode("\n", $new));
134134

135-
return RendererFactory::getInstance($template)
136-
->setOptions($templateOptions)
135+
return RendererFactory::getInstance($renderer)
136+
->setOptions($rendererOptions)
137137
->render(
138138
Differ::getInstance()
139139
->setOldNew($old, $new)
@@ -146,9 +146,9 @@ public static function calculate(
146146
*
147147
* @param string $old the path of the old file
148148
* @param string $new the path of the new file
149-
* @param string $template the template name
149+
* @param string $renderer the renderer name
150150
* @param array $differOptions the options for Differ object
151-
* @param array $templateOptions the options for template object
151+
* @param array $rendererOptions the options for renderer object
152152
*
153153
* @throws \LogicException path is a directory
154154
* @throws \RuntimeException path cannot be opened
@@ -158,9 +158,9 @@ public static function calculate(
158158
public static function calculateFiles(
159159
string $old,
160160
string $new,
161-
string $template = 'Unified',
161+
string $renderer = 'Unified',
162162
array $differOptions = [],
163-
array $templateOptions = []
163+
array $rendererOptions = []
164164
): string {
165165
// we want to leave the line-ending problem to static::calculate()
166166
// so do not set SplFileObject::DROP_NEW_LINE flag
@@ -171,9 +171,9 @@ public static function calculateFiles(
171171
return static::calculate(
172172
$oldFile->fread($oldFile->getSize()),
173173
$newFile->fread($newFile->getSize()),
174-
$template,
174+
$renderer,
175175
$differOptions,
176-
$templateOptions
176+
$rendererOptions
177177
);
178178
}
179179
}

0 commit comments

Comments
 (0)