Debian Codegraph @

vendor/jfcherng/php-diff/src/DiffHelper.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jfcherng\Diff;
6
7 use Jfcherng\Diff\Factory\RendererFactory;
8 use Jfcherng\Diff\Renderer\RendererConstant;
9
10 final class DiffHelper
11 {
12 /**
13 * The constructor.
14 */
15 private function __construct()
16 {
17 }
18
19 /**
20 * Get the absolute path of the project root directory.
21 */
22 public static function getProjectDirectory(): string
23 {
24 static $path;
25
26 return $path ??= realpath(__DIR__ . '/..');
27 }
28
29 /**
30 * Get the information about available renderers.
31 */
32 public static function getRenderersInfo(): array
33 {
34 static $info;
35
36 if (isset($info)) {
37 return $info;
38 }
39
40 $glob = implode(\DIRECTORY_SEPARATOR, [
41 self::getProjectDirectory(),
42 'src',
43 'Renderer',
44 '{' . implode(',', RendererConstant::RENDERER_TYPES) . '}',
45 '*.php',
46 ]);
47
48 $fileNames = array_map(
49 // get basename without file extension
50 static fn (string $file): string => pathinfo($file, \PATHINFO_FILENAME),
51 // paths of all Renderer files
52 glob($glob, \GLOB_BRACE),
53 );
54
55 $renderers = array_filter(
56 $fileNames,
57 // only normal class files are wanted
58 static fn (string $fileName): bool => (
59 substr($fileName, 0, 8) !== 'Abstract'
60 && substr($fileName, -9) !== 'Interface'
61 && substr($fileName, -5) !== 'Trait'
62 ),
63 );
64
65 $info = [];
66 foreach ($renderers as $renderer) {
67 $info[$renderer] = RendererFactory::resolveRenderer($renderer)::INFO;
68 }
69
70 return $info;
71 }
72
73 /**
74 * Get the available renderers.
75 *
76 * @return string[] the available renderers
77 */
78 public static function getAvailableRenderers(): array
79 {
80 return array_keys(self::getRenderersInfo());
81 }
82
83 /**
84 * Get the content of the CSS style sheet for HTML renderers.
85 *
86 * @throws \LogicException path is a directory
87 * @throws \RuntimeException path cannot be opened
88 */
89 public static function getStyleSheet(): string
90 {
91 static $fileContent;
92
93 if (isset($fileContent)) {
94 return $fileContent;
95 }
96
97 $filePath = self::getProjectDirectory() . '/example/diff-table.css';
98
99 $file = new \SplFileObject($filePath, 'r');
100
101 return $fileContent = $file->fread($file->getSize());
102 }
103
104 /**
105 * Gets the diff statistics such as inserted and deleted etc...
106 *
107 * @return array<string,float> the statistics
108 */
109 public static function getStatistics(): array
110 {
111 return Differ::getInstance()->getStatistics();
112 }
113
114 /**
115 * All-in-one static method to calculate the diff between two strings (or arrays of strings).
116 *
117 * @param string|string[] $old the old string (or array of lines)
118 * @param string|string[] $new the new string (or array of lines)
119 * @param string $renderer the renderer name
120 * @param array $differOptions the options for Differ object
121 * @param array $rendererOptions the options for renderer object
122 *
123 * @return string the rendered differences
124 */
125 public static function calculate(
126 $old,
127 $new,
128 string $renderer = 'Unified',
129 array $differOptions = [],
130 array $rendererOptions = []
131 ): string {
132 // always convert into array form
133 \is_string($old) && ($old = explode("\n", $old));
134 \is_string($new) && ($new = explode("\n", $new));
135
136 return RendererFactory::getInstance($renderer)
137 ->setOptions($rendererOptions)
138 ->render(
139 Differ::getInstance()
140 ->setOldNew($old, $new)
141 ->setOptions($differOptions),
142 )
143 ;
144 }
145
146 /**
147 * All-in-one static method to calculate the diff between two files.
148 *
149 * @param string $old the path of the old file
150 * @param string $new the path of the new file
151 * @param string $renderer the renderer name
152 * @param array $differOptions the options for Differ object
153 * @param array $rendererOptions the options for renderer object
154 *
155 * @throws \LogicException path is a directory
156 * @throws \RuntimeException path cannot be opened
157 *
158 * @return string the rendered differences
159 */
160 public static function calculateFiles(
161 string $old,
162 string $new,
163 string $renderer = 'Unified',
164 array $differOptions = [],
165 array $rendererOptions = []
166 ): string {
167 // we want to leave the line-ending problem to static::calculate()
168 // so do not set SplFileObject::DROP_NEW_LINE flag
169 // otherwise, we will lose \r if the line-ending is \r\n
170 $oldFile = new \SplFileObject($old, 'r');
171 $newFile = new \SplFileObject($new, 'r');
172
173 return self::calculate(
174 // fread() requires the length > 0 hence we plus 1 for empty files
175 $oldFile->fread($oldFile->getSize() + 1),
176 $newFile->fread($newFile->getSize() + 1),
177 $renderer,
178 $differOptions,
179 $rendererOptions,
180 );
181 }
182 }