Files
2026-04-28 21:14:25 +03:00

94 lines
2.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\DocumentManager\FolderCatalog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DocumentSyncFolders extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'document:sync-folders {--dry-run : Show operations without creating directories} {--root= : Override the documents root path}';
/**
* The console command description.
*/
protected $description = 'Synchronise the Document Manager folder structure using the standard catalog definition.';
public function handle(FolderCatalog $catalog): int
{
$rootPath = $this->option('root') ?: storage_path('documents');
$dryRun = (bool) $this->option('dry-run');
$this->info('Document Manager folder synchronisation');
$this->line('Root path: ' . $rootPath);
$this->line($dryRun ? 'Mode: Dry run (no directories will be created)' : 'Mode: Live');
$this->newLine();
if (! File::isDirectory($rootPath)) {
if ($dryRun) {
$this->warn('Documents directory does not exist. (dry-run)');
} else {
File::ensureDirectoryExists($rootPath);
$this->info('Created documents root directory.');
}
}
$results = $catalog->sync($rootPath, $dryRun);
if (! empty($results['created'])) {
$title = $dryRun ? 'Directories that would be created' : 'Directories created';
$this->section($title);
$this->displayList($results['created']);
$this->newLine();
}
if (! empty($results['existing'])) {
$this->section('Directories already present');
$this->displayList($results['existing']);
$this->newLine();
}
// Detect directories that are not part of the catalog
$extras = $catalog->detectExtra($rootPath);
if ($extras->isNotEmpty()) {
$this->section('Directories not defined in the catalog');
$sorted = $extras->sort()->values();
$this->displayList($sorted->all());
$this->newLine();
}
$this->info('Document Manager folder synchronisation completed.');
if ($dryRun) {
$this->comment('Dry run mode - no changes were applied.');
}
return self::SUCCESS;
}
protected function section(string $title): void
{
$this->line('<options=bold>' . $title . '</>');
}
protected function displayList(array $items, int $limit = 20): void
{
$items = array_values(array_unique($items));
$total = count($items);
$display = array_slice($items, 0, $limit);
foreach ($display as $item) {
$this->line(' - ' . $item);
}
if ($total > $limit) {
$this->line(' ... ' . ($total - $limit) . ' more');
}
}
}