Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexandr-parkhomenko/34347a8c78e13bd5202c98357d97e369 to your computer and use it in GitHub Desktop.
Save alexandr-parkhomenko/34347a8c78e13bd5202c98357d97e369 to your computer and use it in GitHub Desktop.
<?php
$path = realpath('package/');
$directoryIterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directoryIterator);
$regexIterator = new RegexIterator(
$iterator,
'/^\/home\/ocz\/PhpstormProjects\/dev\/package\/.+\/Form\/Type.+Type.php$/',
RecursiveRegexIterator::GET_MATCH,
RecursiveRegexIterator::USE_KEY
);
$getBlockPrefixFunctionStr =<<<EOF
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return %s;
}
EOF;
$codeRegexGetName = "/\s{4}public\sfunction\sgetName\(\)\n\s{4}\{\s+return\s(\'[0-9a-z_]+\'|self::NAME|static::NAME);\s+}/s";
$codeRegexBlockPrefix = "/\s{4}public\sfunction\sgetBlockPrefix\(\)/s";
$callbackFilterIterator = new CallbackFilterIterator($regexIterator,
function ($current, $filename, $iterator) use ($codeRegexGetName, $codeRegexBlockPrefix) {
$handler = fopen($filename, 'r');
$fileContent = fread($handler, filesize($filename));
fclose($handler);
$matchResultGetName = preg_match($codeRegexGetName, $fileContent);
$matchResultGetBlockPrefix = preg_match($codeRegexBlockPrefix, $fileContent);
return $matchResultGetName === 1 && $matchResultGetBlockPrefix === 0;
}
);
$getContentWithIncorrectEncodingCommand = 'cat -vt %s';
$restoreEncodingCommand = 'cat -vt %s | tail -c +%s | awk \'{ print > "%s" }\'';
if (!iterator_count($callbackFilterIterator)) {
echo 'Nothing to do.';
die();
}
foreach ($callbackFilterIterator as $filename => $current) {
echo 'Working on file - ' . $filename . PHP_EOL;
$handler = fopen($filename, 'r+');
$fileContent = fread($handler, filesize($filename));
$matches = [];
$matchResultGetName = preg_match($codeRegexGetName, $fileContent, $matches);
list($originalFunc, $parameter) = $matches;
$newFileContent = prepareNewFileContent($fileContent, $originalFunc, $parameter);
ftruncate($handler, 0);
fwrite($handler, $newFileContent);
fclose($handler);
$contentWithIncorrectEncoding = '';
exec(sprintf($getContentWithIncorrectEncodingCommand, $filename, $filename), $contentWithIncorrectEncoding);
$position = strrpos($contentWithIncorrectEncoding[0], '^@');
if (false !== $position) {
exec(sprintf($restoreEncodingCommand, $filename, $position + 3, $filename));
}
echo 'Converted file - ' . $filename . PHP_EOL;
}
echo 'Work successfully done !';
/**
* @param string $fileContent
* @param string $originalFunc
* @param string $parameter
*/
function prepareNewFileContent($fileContent, $originalFunc, $parameter)
{
global $getBlockPrefixFunctionStr;
$newCode = str_replace($parameter, '$this->getBlockPrefix()', $originalFunc) .
sprintf($getBlockPrefixFunctionStr, $parameter);
return str_replace($originalFunc, $newCode, $fileContent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment