netgescon-master/app/Debug/Commands/FindDuplicateViews.php

152 lines
5.1 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class FindDuplicateViews extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'netgescon:find-duplicates {--action=list : list, move, or delete duplicate views}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Trova e gestisce le view duplicate che causano confusione nel sistema';
/**
* Execute the console command.
*/
public function handle()
{
$action = $this->option('action');
$this->info("🔍 Ricerca view duplicate in tutto il sistema...");
$this->newLine();
// Directory principali da controllare
$searchPaths = [
base_path('resources/views'),
base_path('_BACKUP_OLD_netgescon-laravel_INACTIVE'),
base_path('_BACKUP_OLD_netgescon_INACTIVE'),
];
$viewFiles = [];
$duplicates = [];
// Raccoglie tutti i file .blade.php
foreach ($searchPaths as $basePath) {
if (!File::isDirectory($basePath)) continue;
$this->line("📂 Scansione: " . str_replace(base_path(), '', $basePath));
$files = File::allFiles($basePath);
foreach ($files as $file) {
if ($file->getExtension() === 'php' && str_ends_with($file->getFilename(), '.blade.php')) {
$relativePath = str_replace($basePath, '', $file->getPathname());
$filename = $file->getFilename();
if (!isset($viewFiles[$filename])) {
$viewFiles[$filename] = [];
}
$viewFiles[$filename][] = [
'full_path' => $file->getPathname(),
'relative_path' => $relativePath,
'base_path' => str_replace(base_path() . '/', '', $basePath),
'size' => $file->getSize(),
'modified' => filemtime($file->getPathname())
];
}
}
}
// Trova duplicati
foreach ($viewFiles as $filename => $locations) {
if (count($locations) > 1) {
$duplicates[$filename] = $locations;
}
}
if (empty($duplicates)) {
$this->info("✅ Nessuna view duplicata trovata!");
return;
}
$this->warn("⚠️ Trovate " . count($duplicates) . " view duplicate:");
$this->newLine();
foreach ($duplicates as $filename => $locations) {
$this->line("🔄 <comment>{$filename}</comment> (" . count($locations) . " copie):");
// Ordina per data di modifica (più recente prima)
usort($locations, function ($a, $b) {
return $b['modified'] - $a['modified'];
});
foreach ($locations as $index => $location) {
$status = $index === 0 ? '✅ ATTIVA (più recente)' : '❌ DUPLICATA';
$date = date('d/m/Y H:i', $location['modified']);
$size = number_format($location['size'] / 1024, 2) . ' KB';
$this->line(" • <info>{$location['base_path']}</info>{$location['relative_path']}");
$this->line(" {$status} | {$date} | {$size}");
if ($action === 'move' && $index > 0) {
$this->moveDuplicateFile($location);
} elseif ($action === 'delete' && $index > 0) {
$this->deleteDuplicateFile($location);
}
}
$this->newLine();
}
if ($action === 'list') {
$this->newLine();
$this->info("💡 Opzioni disponibili:");
$this->line(" • <comment>--action=move</comment> - Sposta i duplicati in una directory _DUPLICATES");
$this->line(" • <comment>--action=delete</comment> - Elimina i duplicati (ATTENZIONE!)");
}
}
private function moveDuplicateFile($location)
{
$duplicatesDir = base_path('_DUPLICATES_MOVED');
if (!File::isDirectory($duplicatesDir)) {
File::makeDirectory($duplicatesDir, 0755, true);
}
$targetPath = $duplicatesDir . '/' . $location['base_path'] . $location['relative_path'];
$targetDir = dirname($targetPath);
if (!File::isDirectory($targetDir)) {
File::makeDirectory($targetDir, 0755, true);
}
try {
File::move($location['full_path'], $targetPath);
$this->line(" ➜ Spostato in: _DUPLICATES_MOVED/{$location['base_path']}{$location['relative_path']}");
} catch (\Exception $e) {
$this->error(" ❌ Errore spostamento: " . $e->getMessage());
}
}
private function deleteDuplicateFile($location)
{
try {
File::delete($location['full_path']);
$this->line(" 🗑️ Eliminato");
} catch (\Exception $e) {
$this->error(" ❌ Errore eliminazione: " . $e->getMessage());
}
}
}