@@ -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