Skip to content

Instantly share code, notes, and snippets.

@garveen
Created January 7, 2015 10:53
Show Gist options
  • Save garveen/04c7a28a45b651d58ad0 to your computer and use it in GitHub Desktop.
Save garveen/04c7a28a45b651d58ad0 to your computer and use it in GitHub Desktop.
<?php
$source = './a';
$desc = './b';
$words = array(
'and' => 1,
'__LINE__' => 1,
'class' => 1,
'die' => 1,
'empty' => 1,
'endswitch' => 1,
'for' => 1,
'include' => 1,
'print' => 1,
'switch' => 1,
'__FUNCTION__' => 1,
'interface' => 1,
'protected' => 1,
'throw' => 1,
'or' => 1,
'array' => 1,
'const' => 1,
'do' => 1,
'enddeclare' => 1,
'endwhile' => 1,
'foreach' => 1,
'include_once' => 1,
'require' => 1,
'unset' => 1,
'__CLASS__' => 1,
'implements' => 1,
'abstract' => 1,
'xor' => 1,
'as' => 1,
'continue' => 1,
'echo' => 1,
'endfor' => 1,
'eval' => 1,
'function' => 1,
'isset' => 1,
'require_once' => 1,
'use' => 1,
'__METHOD__' => 1,
'extends' => 1,
'clone' => 1,
'this' => 1,
'__FILE__' => 1,
'break' => 1,
'declare' => 1,
'else' => 1,
'endforeach' => 1,
'exit' => 1,
'global' => 1,
'list' => 1,
'return' => 1,
'var' => 1,
'final' => 1,
'public' => 1,
'try' => 1,
'exception' => 1,
'case' => 1,
'default' => 1,
'elseif' => 1,
'endif' => 1,
'extends' => 1,
'if' => 1,
'new' => 1,
'static' => 1,
'while' => 1,
'php_user_filter' => 1,
'private' => 1,
'catch' => 1,
'self' => 1,
'parent' => 1,
'_GET' => 1,
'_POST' => 1,
'_REQUEST' => 1,
);
function walk($pattern){
$files = glob($pattern);
foreach(array_reverse(glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT)) as $dir){
$files = array_merge(walk($dir.'/'.basename($pattern)), $files);
}
return $files;
}
$files = walk($source . '/*.php');
$function_hash = array();
$variable_hash = array();
$output = '';
foreach ($files as $filename) {
$tokens = token_get_all(file_get_contents($filename));
foreach ($tokens as $token) {
if (is_array($token)) {
if (defined($token[1]) || isset($words[$token[1]]) || strtoupper($token[1]) == $token[1]) {
$output .= $token[1];
} elseif ($token[0] == T_STRING) {
if (!function_exists($token[1])) {
if (!isset($function_hash[$token[1]])) {
$function_hash[$token[1]] = count($function_hash);
}
$output .= "_{$function_hash[$token[1]]}";
} else {
$output .= $token[1];
}
} elseif ($token[0] == T_VARIABLE) {
$word = ltrim($token[1], '$');
if(!isset($words[$word])) {
if (!isset($variable_hash[$word])) {
$variable_hash[$word] = count($variable_hash);
}
$output .= "\$_{$variable_hash[$word]}";
} else {
$output .= $token[1];
}
} else {
$output .= $token[1];
}
} else {
$output .= $token;
}
}
$newname = $desc . substr($filename, strlen($source));
if(!is_dir($dir = dirname($newname))) {
mkdir($dir, 0755, true);
}
file_put_contents($newname, $output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment