Skip to content

Instantly share code, notes, and snippets.

@weltling
Created December 18, 2014 08:29
Show Gist options
  • Save weltling/8e8e0965bc471d254acc to your computer and use it in GitHub Desktop.
Save weltling/8e8e0965bc471d254acc to your computer and use it in GitHub Desktop.
Tool for basic removals of TSRMLS_* in PHP7
<?php
$path = !isset($argv[1]) ? "." : realpath($argv[1]);
if (!is_dir($path)) {
echo "need the source dir path\n";
exit(3);
}
$map = array(
" TSRMLS_CC)" => ")",
" TSRMLS_CC )" => " )",
"TSRMLS_CC);" => ");",
"TSRMLS_CC)" => ")",
" TSRMLS_CC," => ",",
" TSRMLS_CC " => " ",
" TSRMLS_CC\n" => "\n",
" TSRMLS_CC\\n" => "\n",
" TSRMLS_DC)" => ")",
"TSRMLS_DC);" => ");",
"TSRMLS_DC)" => ")",
" TSRMLS_DC )" => " )",
" TSRMLS_DC," => ",",
" TSRMLS_DC " => " ",
" TSRMLS_DC" => "",
"TSRMLS_C)" => ")",
"TSRMLS_C )" => " )",
"TSRMLS_D)" => "void)",
"TSRMLS_D )" => "void )",
"\tTSRMLS_FETCH();\n" => "",
"TSRMLS_FETCH();\n" => "",
"TSRMLS_FETCH();\\n" => "",
);
$fileptr = array(
"*.c", "*.h", "*.cpp", "*.hpp", "*.y", "*.l", "*.skl", "*.re", "Makefile", "Makefile.frag", "*.patch", "zend_vm_gen.php"
);
$dont_touch_file = array(
"TSRM.h",
);
$dont_touch_dir = array(
"sapi" . DIRECTORY_SEPARATOR . "embed",
);
function get_file_list($path, $fileptr)
{
$dir_iterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
$ret = array();
foreach ($iterator as $file) {
if (!$file->isDir()) {
continue;
}
foreach ($fileptr as $ptr) {
$ret = array_merge((array)glob($file . DIRECTORY_SEPARATOR . $ptr), $ret);
}
}
foreach ($ret as $dummy => &$v) {
$v = realpath($v);
}
return $ret;
}
function is_dont_touch_dir($dir, $dirs)
{
if (!is_dir($dir)) {
$dir = dirname(realpath($dir));
}
foreach($dirs as $item) {
if (strstr($dir, $item) !== false) {
return true;
}
}
return false;
}
$lst = get_file_list($path, $fileptr);
foreach($lst as $src_fl) {
if (in_array(basename($src_fl), $dont_touch_file) || is_dont_touch_dir(dirname($src_fl), $dont_touch_dir)) {
continue;
}
$src = file_get_contents($src_fl);
$sum = md5($src);
$search = array_keys($map);
$replace = array_values($map);
$src = str_replace($search, $replace, $src);
if ($sum != md5($src)) {
file_put_contents($src_fl, $src);
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment