Skip to content

Instantly share code, notes, and snippets.

@aminin
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminin/afb5061d8d149f9aeadf to your computer and use it in GitHub Desktop.
Save aminin/afb5061d8d149f9aeadf to your computer and use it in GitHub Desktop.
Merge orm annotations to existing classes
<?php
// Run after $ php app/console doctrine:mapping:convert annotation
$propertyRegExp = '~( \/\*\*[^/]+?\*\/)\s+(?:private)|(?:protected) \$(\w+)\;~';
$classRegExp = '~(\/\*\*[\s\S]+?\*\/)\s+(class \w+)~';
$to = '';
$from = '';
$Regex = new RegexIterator(
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($from)),
'/^.+\.php$/i',
RecursiveRegexIterator::GET_MATCH
);
$i = 0;
foreach ($Regex as $file) {
$file = $file[0];
$baseName = basename($file);
echo $baseName, "\n";
$toFile = $to . '/' . $baseName;
$fileContents = file_get_contents($file);
$toFileContents = file_get_contents($toFile);
if (preg_match($classRegExp, $fileContents, $m)) {
$classComment = $m[0];
echo $classComment, "\n";
$toFileContents = preg_replace($classRegExp, $classComment, $toFileContents);
}
if (preg_match_all($propertyRegExp, $fileContents, $matches, PREG_SET_ORDER)) {
foreach ($matches as $mm) {
$re = str_replace('(\w+)', $mm[2], $propertyRegExp);
$toFileContents = preg_replace($re, $mm[0], $toFileContents);
echo $re, "\n";
}
}
file_put_contents($toFile, $toFileContents);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment