|
1 | 1 | <?php |
| 2 | + |
| 3 | +namespace JasonBradley\SpeedyDiff; |
| 4 | + |
| 5 | +require_once './Exceptions/DiffException.php'; |
| 6 | + |
| 7 | +use JasonBradley\SpeedyDiff\Exceptions\DiffException; |
| 8 | + |
2 | 9 | /** |
3 | 10 | * @author Jason Bradley |
4 | 11 | * |
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(); |
6 | 19 | */ |
7 | 20 | class SpeedyDiff |
8 | 21 | { |
| 22 | + /** @var string $file1 **/ |
9 | 23 | protected $file1; |
| 24 | + |
| 25 | + /** @var string $file2 **/ |
10 | 26 | 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 | | - } |
21 | 27 |
|
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 | + { |
23 | 40 | $this->file1 = $file1; |
24 | 41 | $this->file2 = $file2; |
25 | | - |
| 42 | + |
| 43 | + $this->validateFiles(); |
| 44 | + |
26 | 45 | $this->runDiff(); |
27 | 46 | } |
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) |
30 | 72 | { |
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.'); |
34 | 75 | } |
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)); |
48 | 87 | } |
49 | 88 | } |
50 | | - |
51 | | - fclose($tmp_diff_handle); |
52 | | - fclose($diff_file_handle); |
53 | | - |
| 89 | + |
| 90 | + fclose($tmpDiffFileHandle); |
| 91 | + fclose($diffFileHandle); |
| 92 | + |
54 | 93 | //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.'); |
64 | 100 | } |
65 | 101 | } |
66 | | - |
| 102 | + |
| 103 | + /** |
| 104 | + * Run the diff command and generate the diff file from the output. |
| 105 | + * |
| 106 | + * @throws \JasonBradley\SpeedyDiff\Exceptions\DiffException |
| 107 | + */ |
67 | 108 | protected function runDiff() |
68 | 109 | { |
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 | + |
73 | 114 | 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) == '') { |
86 | 122 | return; |
87 | 123 | } |
88 | | - |
89 | | - $this->has_difference = true; |
90 | | - |
91 | | - $this->createDiffFile($tmp_file); |
| 124 | + |
| 125 | + $this->hasDifference = true; |
| 126 | + |
| 127 | + $this->createDiffFile($tmpFile); |
92 | 128 | } |
93 | | - |
| 129 | + |
94 | 130 | /** |
95 | | - * Get the file created from the diff |
96 | | - * |
| 131 | + * Get the file created from the diff. |
| 132 | + * |
97 | 133 | * @return String|Boolean |
98 | 134 | */ |
99 | 135 | public function getDiffFile() |
100 | 136 | { |
101 | | - return (is_file($this->diff_file)) ? $this->diff_file : false; |
| 137 | + return (is_file($this->diffFile)) ? $this->diffFile : false; |
102 | 138 | } |
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 | + */ |
104 | 157 | public function getHasDifference() |
105 | 158 | { |
106 | | - return $this->has_difference; |
| 159 | + return $this->hasDifference; |
107 | 160 | } |
108 | 161 | } |
0 commit comments