Skip to content

Commit 2584484

Browse files
committed
Added namespace and composer support
1 parent d5d318c commit 2584484

File tree

7 files changed

+182
-72
lines changed

7 files changed

+182
-72
lines changed

Exceptions/DiffException.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace JasonBradley\SpeedyDiff\Exceptions;
4+
5+
use Exception;
6+
7+
class DiffException extends Exception
8+
{
9+
/**
10+
* @param string $message
11+
* @param int $code
12+
* @param Exception $previous
13+
*/
14+
public function __construct($message = '', $code = null, Exception $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
speedy-diff
22
===========
33

4-
Compare to files with diff and create a file from the difference.
4+
Compare to files with diff and create a file from the difference.
5+
6+
Usage:
7+
8+
$speedyDiff = new \JasonBradley\SpeedyDiff\SpeedyDiff($file1, $file2);
9+
10+
echo $speedyDiff->getDiffOutput();

SpeedyDiff.php

Lines changed: 124 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,161 @@
11
<?php
2+
3+
namespace JasonBradley\SpeedyDiff;
4+
5+
require_once './Exceptions/DiffException.php';
6+
7+
use JasonBradley\SpeedyDiff\Exceptions\DiffException;
8+
29
/**
310
* @author Jason Bradley
411
*
5-
* Creates a diff of two files from large files
12+
* Quickly creates a diff of two files from large files using
13+
* the Linux "diff" command.
14+
*
15+
* Usage:
16+
* $speedyDiff = new \JasonBradley\SpeedyDiff\SpeedyDiff($file1, $file2);
17+
*
18+
* echo $speedyDiff->getDiffOutput();
619
*/
720
class SpeedyDiff
821
{
22+
/** @var string $file1 **/
923
protected $file1;
24+
25+
/** @var string $file2 **/
1026
protected $file2;
11-
12-
protected $diff_file = '';
13-
protected $has_difference = false;
14-
15-
public function __construct($file1, $file2))
16-
{
17-
if (!is_file($file1) || !is_file($file2))
18-
{
19-
throw new Exception("Provide two files.");
20-
}
2127

22-
28+
/** @var string $diffFile **/
29+
protected $diffFile = '';
30+
31+
/** @var bool $hasDifference **/
32+
protected $hasDifference = false;
33+
34+
/**
35+
* @param string $file1 First File
36+
* @param string $file2 Second File
37+
*/
38+
public function __construct($file1, $file2)
39+
{
2340
$this->file1 = $file1;
2441
$this->file2 = $file2;
25-
42+
43+
$this->validateFiles();
44+
2645
$this->runDiff();
2746
}
28-
29-
protected function createDiffFile($tmp_file)
47+
48+
/**
49+
* Ensure we have two valid files.
50+
*
51+
* @throws \JasonBradley\SpeedyDiff\Exceptions\DiffException
52+
*/
53+
protected function validateFiles()
54+
{
55+
if (!is_file($this->file1)) {
56+
throw new DiffException('File #1 is not valid.');
57+
}
58+
59+
if (!is_file($this->file2)) {
60+
throw new DiffException('File #2 is not valid.');
61+
}
62+
}
63+
64+
/**
65+
* Generate the diff file.
66+
*
67+
* @param string $tmpFile File to write diff to
68+
*
69+
* @throws \JasonBradley\SpeedyDiff\Exceptions\DiffException
70+
*/
71+
protected function createDiffFile($tmpFile)
3072
{
31-
if (!is_file($tmp_file))
32-
{
33-
throw new Exception("No diff file provided.");
73+
if (!is_file($tmpFile)) {
74+
throw new DiffException('No diff file provided.');
3475
}
35-
36-
$this->diff_file = tempnam('/tmp', 'speeddifffinal');
37-
38-
$tmp_diff_handle = fopen($tmp_file, 'r');
39-
$diff_file_handle = fopen($this->diff_file, 'a');
40-
41-
while (($line = fgets($tmp_diff_handle)) !== false)
42-
{
43-
$first_char = substr($line, 0, 1);
44-
45-
if ($first_char == '>')
46-
{
47-
fwrite($diff_file_handle, substr($line, 2));
76+
77+
$this->diffFile = tempnam('/tmp', 'speeddifffinal');
78+
79+
$tmpDiffFileHandle = fopen($tmpFile, 'r');
80+
$diffFileHandle = fopen($this->diffFile, 'a');
81+
82+
while (($line = fgets($tmpDiffFileHandle)) !== false) {
83+
$firstChar = substr($line, 0, 1);
84+
85+
if ($firstChar == '>') {
86+
fwrite($diffFileHandle, substr($line, 2));
4887
}
4988
}
50-
51-
fclose($tmp_diff_handle);
52-
fclose($diff_file_handle);
53-
89+
90+
fclose($tmpDiffFileHandle);
91+
fclose($diffFileHandle);
92+
5493
//remove the tmp file
55-
unlink($tmp_file);
56-
57-
if (!is_file($this->diff_file))
58-
{
59-
throw new Exception("There was a problem writing the new diff file.");
60-
}
61-
else if (file_get_contents($this->diff_file) == '')
62-
{
63-
throw new Exception("Nothing was written to the new diff file.");
94+
unlink($tmpFile);
95+
96+
if (!is_file($this->diffFile)) {
97+
throw new DiffException('There was a problem writing the new diff file.');
98+
} elseif (file_get_contents($this->diffFile) == '') {
99+
throw new DiffException('Nothing was written to the new diff file.');
64100
}
65101
}
66-
102+
103+
/**
104+
* Run the diff command and generate the diff file from the output.
105+
*
106+
* @throws \JasonBradley\SpeedyDiff\Exceptions\DiffException
107+
*/
67108
protected function runDiff()
68109
{
69-
$tmp_file = tempnam('/tmp', 'speeddiff');
70-
$cmd = "diff {$this->file1} {$this->file2} --speed-large-files -b > $tmp_file";
71-
exec($cmd, $output, $return_var);
72-
110+
$tmpFile = tempnam('/tmp', 'speedydiff');
111+
$cmd = "diff {$this->file1} {$this->file2} --speed-large-files -b > $tmpFile";
112+
exec($cmd, $output, $returnVar);
113+
73114
unset($output);
74-
75-
if ($return_var > 1)
76-
{
77-
unlink($tmp_file);
78-
throw new Exception("There was an error when calling $cmd");
79-
}
80-
else if (!is_file($tmp_file))
81-
{
82-
throw new Exception("Unable to write to tmp file, $tmp_file");
83-
}
84-
else if (file_get_contents($tmp_file) == '')
85-
{
115+
116+
if ($returnVar > 1) {
117+
unlink($tmpFile);
118+
throw new DiffException("There was an error when calling $cmd");
119+
} elseif (!is_file($tmpFile)) {
120+
throw new DiffException("Unable to write to tmp file, $tmpFile");
121+
} elseif (file_get_contents($tmpFile) == '') {
86122
return;
87123
}
88-
89-
$this->has_difference = true;
90-
91-
$this->createDiffFile($tmp_file);
124+
125+
$this->hasDifference = true;
126+
127+
$this->createDiffFile($tmpFile);
92128
}
93-
129+
94130
/**
95-
* Get the file created from the diff
96-
*
131+
* Get the file created from the diff.
132+
*
97133
* @return String|Boolean
98134
*/
99135
public function getDiffFile()
100136
{
101-
return (is_file($this->diff_file)) ? $this->diff_file : false;
137+
return (is_file($this->diffFile)) ? $this->diffFile : false;
102138
}
103-
139+
140+
/**
141+
* Get the difference between the two input files1.
142+
*
143+
* @return string
144+
*/
145+
public function getDiffOutput()
146+
{
147+
if ($this->getDiffFile() !== false) {
148+
return file_get_contents($this->getDiffFile());
149+
}
150+
}
151+
152+
/**
153+
* Returns if there is a difference between the two files.
154+
*
155+
* @return bool
156+
*/
104157
public function getHasDifference()
105158
{
106-
return $this->has_difference;
159+
return $this->hasDifference;
107160
}
108161
}

TestDiffFiles/file1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1234
2+
2345

TestDiffFiles/file2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1234
2+
2346

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "jasonbradley/speedy-diff",
3+
"description": "Quickly generate a diff of two files.",
4+
"keywords": ["cli","command-line"],
5+
"homepage": "http://github.com/jasonbradley/speedy-diff",
6+
"type": "library",
7+
"license": "Apache-2.0",
8+
"authors": [
9+
{
10+
"name": "Jason Bradley",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3.0"
16+
}
17+
}

example.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require_once 'SpeedyDiff.php';
4+
5+
use JasonBradley\SpeedyDiff\SpeedyDiff;
6+
7+
$file1 = './TestDiffFiles/file1.txt';
8+
$file2 = './TestDiffFiles/file2.txt';
9+
10+
$speedDiff = new SpeedyDiff($file1, $file2);
11+
12+
echo $speedDiff->getDiffOutput();

0 commit comments

Comments
 (0)