Created
July 29, 2016 21:04
-
-
Save alexandr-parkhomenko/34347a8c78e13bd5202c98357d97e369 to your computer and use it in GitHub Desktop.
Revisions
-
alexandr-parkhomenko created this gist
Jul 29, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,85 @@ <?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); }