filename); $newfile = (object) array( 'filepath' => dirname($file->filepath) . '/' . strtr($filename_target, array('@name' => $module)), 'filename' => strtr($filename_target, array('@name' => $module)), ); echo "$file->filepath => $newfile->filepath\n"; // Fetch the source file content. $content = file_get_contents($file->filepath); // Retrieve code to move. $newcontent = ''; $remove = array(); foreach ($preg_hooks as $hook) { $s = '@^/\*\*(?(?!function).)+\*/\s+function ' . $module . '_' . $hook . '\(.*?\).+?\n}\s+@sm'; preg_match($s, $content, $matches); if (!empty($matches[0])) { $remove[] = $matches[0]; $newcontent .= $matches[0]; } } if ($newcontent) { // Write target file. if (file_exists($newfile->filepath)) { $flags = FILE_APPEND; } else { $flags = 0; $newcontent = strtr($target_header, array('@title' => ucfirst($module))) . $newcontent; } file_put_contents($newfile->filepath, $newcontent, $flags); // Remove code from source file. $content = str_replace($remove, '', $content); file_put_contents($file->filepath, $content); // Add target file to .info file. $infofile = dirname($file->filepath) . '/' . $module . '.info'; if (file_exists($infofile)) { $info_content = file_get_contents($infofile); if (strpos($info_content, $newfile->filename) === FALSE) { file_put_contents($infofile, 'files[] = ' . $newfile->filename . "\n", FILE_APPEND); } } // Add target file to CVS. $CVS = dirname($file->filepath) . '/CVS/Entries'; if (file_exists($CVS)) { $CVS_content = file_get_contents($CVS); if (strpos($CVS_content, $newfile->filename) === FALSE) { file_put_contents($CVS, '/' . $newfile->filename . "/0/Initial import.//\n", FILE_APPEND); } } } }