268 lines
8.1 KiB
PHP
Executable File
268 lines
8.1 KiB
PHP
Executable File
<?php
|
||
|
||
/**
|
||
* Script di correzione automatica layout NetGescon
|
||
* Corregge automaticamente i problemi di compatibilità layout
|
||
*
|
||
* @author Michele Barone
|
||
* @date 27/07/2025
|
||
*/
|
||
|
||
echo "🔧 NetGescon Layout Auto-Fix\n";
|
||
echo "============================\n\n";
|
||
|
||
$basePath = '/var/www/netgescon/resources/views';
|
||
$backupPath = '/var/www/netgescon/LAYOUT_BACKUP_' . date('Y-m-d_H-i-s');
|
||
$fixed = 0;
|
||
$errors = 0;
|
||
|
||
// Crea directory di backup
|
||
if (!is_dir($backupPath)) {
|
||
mkdir($backupPath, 0755, true);
|
||
echo "📁 Backup creato in: " . $backupPath . "\n\n";
|
||
}
|
||
|
||
/**
|
||
* Trova i file blade ricorsivamente
|
||
*/
|
||
function findBladeFiles($dir)
|
||
{
|
||
$files = [];
|
||
$iterator = new RecursiveIteratorIterator(
|
||
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS)
|
||
);
|
||
|
||
foreach ($iterator as $file) {
|
||
if ($file->getExtension() === 'php' && strpos($file->getFilename(), '.blade.') !== false) {
|
||
$files[] = $file->getPathname();
|
||
}
|
||
}
|
||
|
||
return $files;
|
||
}
|
||
|
||
/**
|
||
* Effettua il backup del file
|
||
*/
|
||
function backupFile($filePath, $backupPath, $basePath)
|
||
{
|
||
$relativePath = str_replace($basePath . '/', '', $filePath);
|
||
$backupFilePath = $backupPath . '/' . $relativePath;
|
||
|
||
// Crea le directory necessarie
|
||
$backupDir = dirname($backupFilePath);
|
||
if (!is_dir($backupDir)) {
|
||
mkdir($backupDir, 0755, true);
|
||
}
|
||
|
||
return copy($filePath, $backupFilePath);
|
||
}
|
||
|
||
/**
|
||
* Applica le correzioni a un file
|
||
*/
|
||
function fixFile($filePath)
|
||
{
|
||
$content = file_get_contents($filePath);
|
||
$originalContent = $content;
|
||
$changes = [];
|
||
|
||
// 1. Fix <x-app-layout> → @extends('admin.layouts.app')
|
||
if (preg_match('/<x-app-layout[^>]*>/', $content)) {
|
||
$content = preg_replace('/<x-app-layout[^>]*>/', '', $content);
|
||
$content = preg_replace('/<\/x-app-layout>/', '', $content);
|
||
|
||
// Aggiungi @extends all'inizio
|
||
if (!preg_match('/@extends\(/', $content)) {
|
||
$content = "@extends('admin.layouts.app')\n\n@section('title', 'Pagina Admin')\n\n@section('content')\n" . $content . "\n@endsection";
|
||
$changes[] = 'Sostituito <x-app-layout> con @extends';
|
||
}
|
||
}
|
||
|
||
// 2. Fix <x-slot name="header"> → sezioni Blade
|
||
if (preg_match('/<x-slot name="header"[^>]*>(.*?)<\/x-slot>/s', $content, $matches)) {
|
||
$headerContent = trim($matches[1]);
|
||
$content = preg_replace('/<x-slot name="header"[^>]*>.*?<\/x-slot>/s', '', $content);
|
||
|
||
// Estrai il titolo se presente
|
||
if (preg_match('/<h2[^>]*>(.*?)<\/h2>/s', $headerContent, $titleMatch)) {
|
||
$title = strip_tags($titleMatch[1]);
|
||
$content = preg_replace('/@section\([\'"]title[\'"],\s*[\'"][^\'"]*[\'"]/', "@section('title', '" . addslashes($title) . "'", $content);
|
||
}
|
||
|
||
$changes[] = 'Convertito <x-slot> header';
|
||
}
|
||
|
||
// 3. Aggiungi @section('title') se mancante
|
||
if (preg_match('/@extends\(/', $content) && !preg_match('/@section\([\'"]title[\'"]/', $content)) {
|
||
$content = preg_replace('/(@extends\([^\)]+\))/', "$1\n\n@section('title', 'Pagina Admin')", $content);
|
||
$changes[] = 'Aggiunto @section(\'title\')';
|
||
}
|
||
|
||
// 4. Fix classi Tailwind comuni → Bootstrap
|
||
$tailwindToBootstrap = [
|
||
// Layout
|
||
'py-12' => 'py-5',
|
||
'px-4' => 'px-3',
|
||
'px-6' => 'px-4',
|
||
'py-4' => 'py-3',
|
||
'py-6' => 'py-4',
|
||
'max-w-7xl' => 'container-fluid',
|
||
'mx-auto' => 'mx-auto',
|
||
|
||
// Colori di sfondo
|
||
'bg-white' => 'bg-white',
|
||
'bg-gray-800' => 'bg-dark',
|
||
'bg-gray-50' => 'bg-light',
|
||
'bg-blue-500' => 'bg-primary',
|
||
'bg-green-500' => 'bg-success',
|
||
'bg-red-500' => 'bg-danger',
|
||
'bg-yellow-500' => 'bg-warning',
|
||
|
||
// Colori del testo
|
||
'text-gray-800' => 'text-dark',
|
||
'text-gray-600' => 'text-muted',
|
||
'text-gray-500' => 'text-muted',
|
||
'text-white' => 'text-white',
|
||
'text-blue-600' => 'text-primary',
|
||
'text-green-600' => 'text-success',
|
||
'text-red-600' => 'text-danger',
|
||
|
||
// Flex
|
||
'flex' => 'd-flex',
|
||
'items-center' => 'align-items-center',
|
||
'justify-between' => 'justify-content-between',
|
||
'justify-center' => 'justify-content-center',
|
||
|
||
// Grid
|
||
'grid-cols-1' => 'row',
|
||
'grid-cols-2' => 'row',
|
||
'grid-cols-3' => 'row',
|
||
'col-span-1' => 'col-md-12',
|
||
|
||
// Spacing
|
||
'space-x-2' => 'gap-2',
|
||
'space-x-3' => 'gap-3',
|
||
'space-y-4' => 'gap-4',
|
||
|
||
// Buttons
|
||
'font-bold' => 'fw-bold',
|
||
'py-2' => 'py-2',
|
||
'px-4' => 'px-3',
|
||
'rounded' => 'rounded',
|
||
'hover:bg-blue-700' => '',
|
||
|
||
// Tables
|
||
'min-w-full' => 'table',
|
||
'divide-y' => 'table-bordered',
|
||
'divide-gray-200' => '',
|
||
'whitespace-nowrap' => 'text-nowrap',
|
||
|
||
// Forms
|
||
'border-gray-300' => 'form-control',
|
||
'rounded-md' => 'rounded',
|
||
'shadow-sm' => '',
|
||
|
||
// Utilities
|
||
'overflow-hidden' => 'overflow-hidden',
|
||
'shadow-sm' => 'shadow-sm',
|
||
'sm:rounded-lg' => 'rounded',
|
||
'dark:bg-gray-800' => '',
|
||
'dark:text-gray-200' => '',
|
||
'dark:border-gray-700' => '',
|
||
];
|
||
|
||
foreach ($tailwindToBootstrap as $tailwind => $bootstrap) {
|
||
if (strpos($content, $tailwind) !== false) {
|
||
$content = str_replace($tailwind, $bootstrap, $content);
|
||
$changes[] = "Convertito $tailwind → $bootstrap";
|
||
}
|
||
}
|
||
|
||
// 5. Fix div con classi specifiche
|
||
$content = preg_replace('/<div class="py-12">/', '<div class="container-fluid py-4">', $content);
|
||
$content = preg_replace('/<div class="max-w-7xl mx-auto[^"]*">/', '<div class="container-fluid">', $content);
|
||
|
||
// Salva solo se ci sono stati cambiamenti
|
||
if ($content !== $originalContent) {
|
||
file_put_contents($filePath, $content);
|
||
return $changes;
|
||
}
|
||
|
||
return [];
|
||
}
|
||
|
||
// Trova tutti i file blade che necessitano correzione
|
||
$bladeFiles = findBladeFiles($basePath);
|
||
|
||
// Filtra solo i file che hanno problemi di layout obsoleto
|
||
$problematicFiles = [];
|
||
foreach ($bladeFiles as $file) {
|
||
$content = file_get_contents($file);
|
||
if (
|
||
preg_match('/<x-app-layout[^>]*>/', $content) ||
|
||
preg_match('/@extends\([\'"]admin\.layouts\.main[\'"]/', $content)
|
||
) {
|
||
$problematicFiles[] = $file;
|
||
}
|
||
}
|
||
|
||
echo "🎯 Trovati " . count($problematicFiles) . " file da correggere\n\n";
|
||
|
||
// Processa ogni file
|
||
foreach ($problematicFiles as $file) {
|
||
$relativePath = str_replace($basePath . '/', '', $file);
|
||
|
||
try {
|
||
// Backup del file
|
||
if (backupFile($file, $backupPath, $basePath)) {
|
||
echo "💾 Backup: $relativePath\n";
|
||
|
||
// Applica le correzioni
|
||
$changes = fixFile($file);
|
||
|
||
if (!empty($changes)) {
|
||
echo "✅ Corretto: $relativePath\n";
|
||
foreach ($changes as $change) {
|
||
echo " └─ $change\n";
|
||
}
|
||
$fixed++;
|
||
} else {
|
||
echo "ℹ️ Nessuna modifica: $relativePath\n";
|
||
}
|
||
} else {
|
||
echo "❌ Errore backup: $relativePath\n";
|
||
$errors++;
|
||
}
|
||
} catch (Exception $e) {
|
||
echo "❌ Errore: $relativePath - " . $e->getMessage() . "\n";
|
||
$errors++;
|
||
}
|
||
|
||
echo "\n";
|
||
}
|
||
|
||
// Riepilogo finale
|
||
echo str_repeat("=", 50) . "\n";
|
||
echo "📊 RIEPILOGO CORREZIONI\n";
|
||
echo str_repeat("=", 50) . "\n";
|
||
echo "✅ File corretti: $fixed\n";
|
||
echo "❌ Errori: $errors\n";
|
||
echo "📁 Backup salvato in: $backupPath\n\n";
|
||
|
||
if ($fixed > 0) {
|
||
echo "🎉 Correzioni applicate con successo!\n";
|
||
echo "📋 Passi successivi:\n";
|
||
echo "1. Verifica che le pagine funzionino correttamente\n";
|
||
echo "2. Testa la navigazione e i form\n";
|
||
echo "3. Se tutto è ok, rimuovi la cartella di backup\n";
|
||
echo "4. Se ci sono problemi, ripristina dal backup\n\n";
|
||
|
||
echo "🔧 Per ripristinare dal backup:\n";
|
||
echo "cp -r $backupPath/* $basePath/\n\n";
|
||
} else {
|
||
echo "ℹ️ Nessun file necessitava correzioni automatiche.\n";
|
||
}
|
||
|
||
echo "✨ Processo completato!\n";
|