Created
April 25, 2009 20:52
-
-
Save prophile/101750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function apply_transform ( $source, $transform, $params ) | |
{ | |
$tokens = token_get_all($source); | |
$newContent = ''; | |
foreach ($tokens as $token) | |
{ | |
$newTok = $transform($token, $params); | |
if (is_string($newTok)) | |
{ | |
$newContent .= $newTok; | |
} | |
else | |
{ | |
$newContent .= $newTok[1]; | |
} | |
} | |
return $newContent; | |
} | |
function parse_arguments ( $argv, $aliases = array() ) | |
{ | |
$progname = array_shift($argv); | |
$arguments = array(); | |
$positional_arguments = array(); | |
$noMoreArgs = false; | |
while (!empty($argv)) | |
{ | |
$nextArg = array_shift($argv); | |
if (!$noMoreArgs && $nextArg[0] == '-') | |
{ | |
if (strlen($nextArg) == 1) | |
$positional_arguments[] = $nextArg; | |
elseif ($nextArg == '--') | |
$noMoreArgs = true; | |
else | |
{ | |
$parts = explode('=', substr($nextArg, 1)); | |
if (count($parts) == 1) | |
{ | |
$param = substr($nextArg, 1); | |
if (isset($aliases[$param])) | |
$param = $aliases[$param]; | |
$arguments[$param] = true; | |
} | |
else | |
{ | |
$param = array_shift($parts); | |
if (isset($aliases[$param])) | |
$param = $aliases[$param]; | |
$arguments[$param] = implode('=', $parts); | |
} | |
} | |
} | |
else | |
{ | |
$positional_arguments[] = $nextArg; | |
} | |
} | |
return array('program' => $progname, 'options' => $arguments, 'arguments' => $positional_arguments); | |
} | |
function transform_rename ( $token, $params ) | |
{ | |
if (!$params['variable'] && is_array($token) && $token[0] == T_STRING && $token[1] == $params['old']) | |
$token[1] = $params['new']; | |
elseif ($params['variable'] && is_array($token) && $token[0] == T_VARIABLE && $token[1] == '$' . $params['old']) | |
$token[1] = '$' . $params['new']; | |
return $token; | |
} | |
$args = parse_arguments($argv, array('o' => 'output', 'old' => 'old-name', 'new' => 'new-name', 'r' => 'recursive', 'q' => 'quiet')); | |
extract($args); | |
$old = $options['old-name']; | |
$new = $options['new-name']; | |
$files = $arguments; | |
$transform_opts = array('old' => $old, 'new' => $new, 'variable' => isset($options['variable'])); | |
while (!empty($files)) | |
{ | |
$file = array_shift($files); | |
if (isset($options['recursive']) && is_dir($file)) | |
{ | |
$content = scandir($file); | |
foreach ($content as $obj) | |
{ | |
if (fnmatch('*.php', $obj)) | |
{ | |
$files[] = "$file/$obj"; | |
} | |
elseif (is_dir("$file/$obj") && $obj[0] != '.') | |
{ | |
$files[] = "$file/$obj"; | |
} | |
} | |
} | |
else | |
{ | |
$content = file_get_contents($file); | |
$newContent = apply_transform($content, 'transform_rename', $transform_opts); | |
if ($content != $newContent) | |
{ | |
file_put_contents($file, $newContent); | |
if (!isset($options['quiet'])) | |
{ | |
echo "Refactored file $file\n"; | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment