|
| 1 | +<?php |
| 2 | + |
| 3 | +// Define the directory to search in and an array of replacements |
| 4 | +$directory = dirname(__DIR__, 2); |
| 5 | +$replacements = [ |
| 6 | + 'fixme-app-name' => 'FIXME', // ex: app-name |
| 7 | + 'fixme-project-name' => 'FIXME', // ex: Client Name |
| 8 | + 'fixme-domain' => 'FIXME' // ex: www.clientdomain.org (do not include the protocal) |
| 9 | + // Add more pairs as needed |
| 10 | +]; |
| 11 | + |
| 12 | +$exclusions = [ |
| 13 | + 'scripts/composer/ReplaceFixme.php', // Relative path from project root |
| 14 | + '.env.example', |
| 15 | + '.lando.example.yml' |
| 16 | +]; |
| 17 | + |
| 18 | +$excludedDirectories = [ |
| 19 | + ".git", |
| 20 | + ".vscode", |
| 21 | + "scripts", |
| 22 | + "web/wp-content", |
| 23 | + "tests" |
| 24 | +]; |
| 25 | + |
| 26 | +// Function to recursively get all files in a directory, skipping excluded directories |
| 27 | +function getFiles($dir, $excludedDirectories) { |
| 28 | + $files = []; |
| 29 | + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); |
| 30 | + foreach ($iterator as $file) { |
| 31 | + $filePath = $file->getPathname(); |
| 32 | + |
| 33 | + // Check if file is in an excluded directory |
| 34 | + foreach ($excludedDirectories as $excludedDir) { |
| 35 | + $excludedPath = dirname(__DIR__, 2) . '/' . $excludedDir; |
| 36 | + if (strpos($filePath, $excludedPath) === 0) { |
| 37 | + continue 2; // Skip files in this directory |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if ($file->isFile()) { |
| 42 | + $files[] = $filePath; |
| 43 | + } |
| 44 | + } |
| 45 | + return $files; |
| 46 | +} |
| 47 | + |
| 48 | +// Function to perform replacements |
| 49 | +function replaceTextInFiles($files, $replacements, $exclusions) { |
| 50 | + foreach ($files as $file) { |
| 51 | + // Convert file path to a format relative to the project root for comparison |
| 52 | + $relativePath = str_replace(dirname(__DIR__, 2) . '/', '', $file); |
| 53 | + |
| 54 | + // Skip files listed in exclusions |
| 55 | + if (in_array($relativePath, $exclusions)) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + $content = file_get_contents($file); |
| 59 | + $updatedContent = str_replace(array_keys($replacements), array_values($replacements), $content); |
| 60 | + |
| 61 | + // Only write if changes were made |
| 62 | + if ($content !== $updatedContent) { |
| 63 | + file_put_contents($file, $updatedContent); |
| 64 | + echo "Updated: $file\n"; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Get all files and perform replacements |
| 70 | +$files = getFiles($directory, $excludedDirectories); |
| 71 | +echo implode("\n", $files) . "\n"; |
| 72 | +replaceTextInFiles($files, $replacements, $exclusions); |
| 73 | + |
| 74 | +echo "Replacement complete.\n"; |
0 commit comments