Skip to content

Instantly share code, notes, and snippets.

@jasonknight
Last active September 26, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonknight/4cf03e46fefcd0a4009af19fc862d10f to your computer and use it in GitHub Desktop.
Save jasonknight/4cf03e46fefcd0a4009af19fc862d10f to your computer and use it in GitHub Desktop.
<?php
namespace Codeable;
function map_with_keys($cb,$a) {
$na = [];
foreach ($a as $k=>$v) {
list($nk,$nv) = call_user_func_array($cb,[$k,$v]);
$na[$nk] = $nv;
}
return $na;
}
function union_of_files($a,$b,$res=[]) {
$cb = function ($k,$v) {
$md5 = md5_file($v);
return [$md5,$v];
};
$files_only = function ($f) {
if ( is_dir($f) || $f == '.' || $f == '..' ) {
return false;
}
return true;
};
$dirs_only = function ($f) {
if ( basename($f) == '.' || basename($f) == '..' ) {
return false;
}
if ( ! is_dir($f) ) {
return false;
}
return true;
};
$a_files = array_map(function ($p) use($a) {
return "$a/$p";}, scandir($a));
$a_files_only = map_with_keys($cb,array_filter($a_files,$files_only));
$b_files = array_map(function ($p) use($b) {
return "$b/$p";}, scandir($b));
$b_files_only = map_with_keys($cb,array_filter($b_files,$files_only));
foreach ( $b_files_only as $md5 => $path ) {
if ( array_key_exists($md5,$a_files_only) ) {
array_push($res,$path);
}
}
$a_dirs_only = array_filter(scandir($a),function ($p) use ($a,$dirs_only) {
if ( $dirs_only("$a/$p") ) {
return true;
}
return false;
});
$b_dirs_only = array_filter(scandir($b),function ($p) use ($b,$dirs_only) {
if ( $dirs_only("$b/$p") ) {
return true;
}
return false;
});
foreach ( $b_dirs_only as $path ) {
if ( in_array($path,$a_dirs_only) ) {
$res += union_of_files("$a/$path","$b/$path",$res);
}
}
return $res;
}
/*
$woo = __DIR__ . "/../woocommerce/templates";
$theme = __DIR__ . "/../../themes/test/templates";
$files = union_of_files($woo,$theme);
print_r($files);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment