Skip to content

Instantly share code, notes, and snippets.

@khattaksd
Created November 23, 2024 19:58
Show Gist options
  • Save khattaksd/a06ae762178af9e0e1e67b538177754e to your computer and use it in GitHub Desktop.
Save khattaksd/a06ae762178af9e0e1e67b538177754e to your computer and use it in GitHub Desktop.
PHP Script to fix namespaces in PHP files based on directory structure, with an option to ignore specific folders.
<?php
/**
* Script to fix namespaces in PHP files based on directory structure,
* with an option to ignore specific folders.
* How It Works:
* Ignored Folders:
* The $ignoredFolders array contains the names of folders to be skipped.
* The script checks if the current directory matches one of the ignored folder names and skips processing if it does.
* Customizable:
* You can easily add or remove folders from the $ignoredFolders array.
* Example:
* Given the following directory structure:
* src/
* ├── Controllers/
* │ └── HomeController.php
* ├── Models/
* │ └── User.php
* ├── vendor/
* │ └── autoload.php
* And $ignoredFolders = ['vendor'], the script will:
*
* Process Controllers and Models directories.
* Skip the vendor folder entirely.
* Run the script:
* php fixNamespaces.php
* This ensures the script ignores specific folders (e.g., vendor, cache)
* while processing the rest of the directories.
*/
function fixNamespaces($directory, $baseNamespace, $baseDir, $ignoredFolders = [])
{
$files = scandir($directory);
foreach ($files as $file) {
// Skip current and parent directory references
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $directory . DIRECTORY_SEPARATOR . $file;
// Check if the folder is in the ignored list
if (is_dir($filePath) && in_array($file, $ignoredFolders)) {
echo "Ignoring folder: $filePath\n";
continue;
}
if (is_dir($filePath)) {
// Recursively process subdirectories
fixNamespaces($filePath, $baseNamespace, $baseDir, $ignoredFolders);
} elseif (pathinfo($filePath, PATHINFO_EXTENSION) === 'php') {
// Process PHP files
fixNamespaceInFile($filePath, $baseNamespace, $baseDir);
}
}
}
function fixNamespaceInFile($filePath, $baseNamespace, $baseDir)
{
$relativePath = str_replace($baseDir, '', dirname($filePath));
$relativeNamespace = trim(str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath), '\\');
$newNamespace = $baseNamespace . ($relativeNamespace ? '\\' . $relativeNamespace : '');
$fileContents = file_get_contents($filePath);
if (preg_match('/^namespace\s+(.+);/m', $fileContents, $matches)) {
$oldNamespace = $matches[1];
if ($oldNamespace !== $newNamespace) {
// Replace the namespace
$fileContents = preg_replace(
'/^namespace\s+.+;/m',
"namespace $newNamespace;",
$fileContents
);
file_put_contents($filePath, $fileContents);
echo "Updated namespace in $filePath from '$oldNamespace' to '$newNamespace'.\n";
} else {
echo "Namespace in $filePath is already correct ('$oldNamespace').\n";
}
} else {
// Add namespace if none exists
$fileContents = "<?php\n\nnamespace $newNamespace;\n\n" . ltrim($fileContents, "<?php\n");
file_put_contents($filePath, $fileContents);
echo "Added namespace '$newNamespace' to $filePath.\n";
}
}
// Usage
$baseDir = __DIR__ . '/src'; // Replace with your base directory path
$baseNamespace = 'App'; // Replace with your base namespace
$ignoredFolders = ['vendor', 'cache']; // Add folder names to ignore
if (is_dir($baseDir)) {
fixNamespaces($baseDir, $baseNamespace, $baseDir, $ignoredFolders);
echo "Namespace fixing completed.\n";
} else {
echo "Error: Base directory '$baseDir' does not exist.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment