1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
<?php
define ('FD_LINE_NORMAL', 1);
define ('FD_LINE_ADDED', 2);
define ('FD_LINE_DELETED', 3);
define ('FD_INLINE_NORMAL', 4);
define ('FD_INLINE_ADDED', 5);
define ('FD_INLINE_DELETED', 6);
class filediff {
private $files = array();
private $line_format = array();
private $header;
private $footer;
private $uid;
public function __construct() {
$this->files['new'] = array();
$this->files['old'] = array();
$this->files['res'] = array();
$this->uid = md5(microtime());
$this->set_line_format('<tr><td>{line:'. $this->uid .'}</td><td>{value:'. $this->uid .'}</td></tr>', FD_LINE_NORMAL);
$this->set_line_format('<tr style="background-color:#99ffaa;"><td>{line:'. $this->uid .'}</td><td>{value:'. $this->uid .'}</td></tr>', FD_LINE_ADDED);
$this->set_line_format('<tr style="background-color:#ff9999;"><td>{line:'. $this->uid .'}</td><td>{value:'. $this->uid .'}</td></tr>', FD_LINE_DELETED);
$this->set_line_format('<span>{value:'. $this->uid .'}</span>', FD_INLINE_NORMAL);
$this->set_line_format('<span style="background-color:#44ff55;">{value:'. $this->uid .'}</span>', FD_INLINE_ADDED);
$this->set_line_format('<span style="background-color:#ff6666;">{value:'. $this->uid .'}</span>', FD_INLINE_DELETED);
$this->set_header('<table>');
$this->set_footer('</table>');
}
public function set_files($new, $old)
{
if (! file_exists($new) )
{
trigger_error('Le fichier '.$new.' n\'existe pas');
}
elseif (! file_exists($old) )
{
trigger_error('Le fichier '.$old.' n\'existe pas');
}
else
{
$this->files['new'] = array(
'name' => $new,
'content' => file($new)
);
$this->files['old'] = array(
'name' => $new,
'content' => file($old)
);
$this->files['res']['content'] = array();
}
}
public function set_destination($res)
{
$this->files['res']['name'][$res];
}
public function set_line_format($format, $type)
{
switch ($type)
{
case FD_LINE_NORMAL:
case FD_LINE_ADDED:
case FD_LINE_DELETED:
case FD_INLINE_NORMAL:
case FD_INLINE_ADDED:
case FD_INLINE_DELETED:
$this->line_format[$type] = str_replace(
array('{value}', '{line}'),
array('{value:'. $this->uid .'}','{line:'. $this->uid .'}'),
$format
);
break;
default:
echo 'error: set_line_format';
break;
}
}
public function set_header($header) { $this->header = $header; }
public function set_footer($footer) { $this->footer = $footer; }
public function display()
{
echo $this->header;
echo implode("\n", $this->files['res']['content']);
echo $this->footer;
}
public function execute($dest = null)
{
// On récupère le tableau des ajouts
$adds = array_diff($this->files['new']['content'], $this->files['old']['content']);
// On récupère le tableau des suppressions
$dels = array_diff($this->files['old']['content'], $this->files['new']['content']);
$i = 0;
// Tant que le nouveau fichier n'a pas été parcourru entièrement
foreach($this->files['new']['content'] as $k => $v)
{
// Si la ligne est présente dans le tableau des suppressions
if ( isset($dels[$i]) )
{
// On vérifie si c'est une modification inline
if (isset($adds[$k])) {
$res = $this->execute_inline( $dels[$i], $adds[$k], FD_INLINE_DELETED, FD_INLINE_NORMAL, $k);
}
else {
$res = $this->replace($this->line_format[FD_INLINE_DELETED], $dels[$i], $k);
}
$this->push_line($res,$k, FD_LINE_DELETED);
$i++;
}
// Sinon si c'est un ajout
if ( isset($adds[$k]) )
{
// On vérifie si c'est une modification inline
if ( isset($dels[$i-1]) ) {
$res = $this->execute_inline( $adds[$k], $dels[$i-1], FD_INLINE_ADDED, FD_INLINE_NORMAL, $k);
}
else {
$res = $this->replace($this->line_format[FD_INLINE_ADDED], $adds[$k], $k);
}
$this->push_line($res,$k, FD_LINE_ADDED);
}
else
{
$v = $this->replace($this->line_format[FD_INLINE_NORMAL], $v, $k);
$this->push_line($v,$k, FD_LINE_NORMAL);
$i++;
}
}
if (isset($dest)) {
$this->save_file($dest);
}
elseif (isset($this->files['res']['name'])) {
$this->save_file($this->files['res']['name']);
}
}
private function save_file($dest)
{
file_put_contents(
$dest,
array_merge(
(array)$this->header,
$this->files['res']['content'],
(array)$this->footer
)
);
}
private function replace ($src, $value = null, $line = null)
{
return str_replace(
array('{value:'. $this->uid .'}','{line:'. $this->uid .'}'),
array($value, $line),
$src
);
}
private function execute_inline($new, $old, $new_format, $old_format, $i)
{
$line_new = split(' ', $new);
$line_old = split(' ', $old);
$modif = array_diff($line_new, $line_old);
$res = ' ';
$n = $m = '';
// Tant que nous n'avons pas parcourru toutes les entités de la ligne
foreach($line_new as $j => $w)
{
// Vérifie si l'entité est présente dans le tableau des modifs
if ( isset($modif[$j]) )
{
// J'utilise ici un buffer pour stocker temporairement pour ne par morceler des données d'un
// meme type (exemple: INLINE_NORMAL) en plusieures morceaux
if ( strlen($n) )
{
$res .= $this->replace($this->line_format[$old_format], $n, $i);
$n = '';
}
$m .= $w . ' ';
}
else
{
// Buffer...
if ( strlen($m) )
{
$res .= $this->replace($this->line_format[$new_format], substr($m,0,-1), $i);
$m = '';
$n = ' ';
}
$n .= $w . ' ';
}
}
// On vide les buffers
if (strlen($m)) { $res .= $this->replace($this->line_format[$new_format], $m, $i); }
elseif (strlen($n)) { $res .= $this->replace($this->line_format[$old_format], $n, $i); }
return $res;
}
private function push_line($value, $i, $type)
{
$value = str_replace("\r\n", '', $value);
$this->files['res']['content'][] = $this->replace($this->line_format[$type], $value, $i+1);
}
}
?> |
Partager