|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * NOTICE OF LICENSE |
| 5 | + * |
| 6 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 7 | + * that is bundled with this package in the file LICENSE.txt. |
| 8 | + * It is also available through the world-wide-web at this URL: |
| 9 | + * https://opensource.org/licenses/OSL-3.0 |
| 10 | + * If you did not receive a copy of the license and are unable to |
| 11 | + * obtain it through the world-wide-web, please send an email |
| 12 | + * to contact@h-hennes.fr so we can send you a copy immediately. |
| 13 | + * |
| 14 | + * @author Hennes Hervé <contact@h-hennes.fr> |
| 15 | + * @copyright since 2016 Hennes Hervé |
| 16 | + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 17 | + * https://github.com/nenes25/prestashop_console |
| 18 | + * https://www.h-hennes.fr/blog/ |
| 19 | + */ |
| 20 | + |
| 21 | +$licenseFile = __DIR__ . '/licence.txt'; |
| 22 | +$projectRoot = dirname(__DIR__); |
| 23 | + |
| 24 | +if (!file_exists($licenseFile)) { |
| 25 | + echo "Error: licence.txt not found at $licenseFile\n"; |
| 26 | + exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +// Read the new license block (already formatted with /** */) |
| 30 | +$newLicenseBlock = file_get_contents($licenseFile); |
| 31 | +$newLicenseBlock = trim($newLicenseBlock); |
| 32 | + |
| 33 | +// Function to find PHP files |
| 34 | +function findPhpFiles($dir) { |
| 35 | + $excludeDirs = ['vendor', 'phar', '.git', 'node_modules']; |
| 36 | + $phpFiles = []; |
| 37 | + |
| 38 | + $iterator = new RecursiveIteratorIterator( |
| 39 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
| 40 | + RecursiveIteratorIterator::SELF_FIRST |
| 41 | + ); |
| 42 | + |
| 43 | + foreach ($iterator as $file) { |
| 44 | + if ($file->isFile() && $file->getExtension() === 'php') { |
| 45 | + $path = $file->getPathname(); |
| 46 | + $excluded = false; |
| 47 | + foreach ($excludeDirs as $excludeDir) { |
| 48 | + if (strpos($path, DIRECTORY_SEPARATOR . $excludeDir . DIRECTORY_SEPARATOR) !== false) { |
| 49 | + $excluded = true; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + if (!$excluded) { |
| 54 | + $phpFiles[] = $path; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return $phpFiles; |
| 60 | +} |
| 61 | + |
| 62 | +// Function to replace license header |
| 63 | +function updateLicenseHeader($filePath, $newLicenseBlock) { |
| 64 | + $content = file_get_contents($filePath); |
| 65 | + $originalContent = $content; |
| 66 | + |
| 67 | + // Check if file starts with shebang |
| 68 | + $hasShebang = strpos($content, '#!') === 0; |
| 69 | + $shebang = ''; |
| 70 | + |
| 71 | + if ($hasShebang) { |
| 72 | + $lines = explode("\n", $content, 2); |
| 73 | + $shebang = $lines[0] . "\n"; |
| 74 | + $content = isset($lines[1]) ? $lines[1] : ''; |
| 75 | + } |
| 76 | + |
| 77 | + // Remove <?php tag temporarily |
| 78 | + $hasPhpTag = preg_match('/^<\?php\s*\n?/s', $content); |
| 79 | + if ($hasPhpTag) { |
| 80 | + $content = preg_replace('/^<\?php\s*\n?/s', '', $content); |
| 81 | + } |
| 82 | + |
| 83 | + // Remove all existing comment blocks at the start |
| 84 | + // This handles /** */ and /* */ style comments |
| 85 | + while (preg_match('/^\s*\/\*/', $content)) { |
| 86 | + $content = preg_replace('/^\s*\/\*.*?\*\/\s*/s', '', $content, 1); |
| 87 | + } |
| 88 | + |
| 89 | + // Remove leading whitespace |
| 90 | + $content = ltrim($content); |
| 91 | + |
| 92 | + // Build new content |
| 93 | + $newContent = $shebang; |
| 94 | + $newContent .= "<?php\n"; |
| 95 | + $newContent .= $newLicenseBlock . "\n\n"; |
| 96 | + $newContent .= $content; |
| 97 | + |
| 98 | + return $newContent; |
| 99 | +} |
| 100 | + |
| 101 | +// Main execution |
| 102 | +echo "Starting license header update...\n\n"; |
| 103 | + |
| 104 | +$phpFiles = findPhpFiles($projectRoot); |
| 105 | +echo "Found " . count($phpFiles) . " PHP files to process\n\n"; |
| 106 | + |
| 107 | +$updated = 0; |
| 108 | +$errors = 0; |
| 109 | + |
| 110 | +foreach ($phpFiles as $file) { |
| 111 | + try { |
| 112 | + $relativePath = str_replace($projectRoot . DIRECTORY_SEPARATOR, '', $file); |
| 113 | + echo "Processing: $relativePath ... "; |
| 114 | + |
| 115 | + $newContent = updateLicenseHeader($file, $newLicenseBlock); |
| 116 | + file_put_contents($file, $newContent); |
| 117 | + |
| 118 | + echo "✓\n"; |
| 119 | + $updated++; |
| 120 | + } catch (Exception $e) { |
| 121 | + echo "✗ Error: " . $e->getMessage() . "\n"; |
| 122 | + $errors++; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +echo "\n" . str_repeat('=', 60) . "\n"; |
| 127 | +echo "Update complete!\n"; |
| 128 | +echo "Files updated: $updated\n"; |
| 129 | +echo "Errors: $errors\n"; |
| 130 | +echo str_repeat('=', 60) . "\n"; |
| 131 | + |
| 132 | +exit($errors > 0 ? 1 : 0); |
0 commit comments