|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Forms\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Symfony\Component\Finder\Finder; |
| 8 | +use function Laravel\Prompts\multisearch; |
| 9 | +use function Laravel\Prompts\confirm; |
| 10 | + |
| 11 | +class PublishViewCommand extends Command |
| 12 | +{ |
| 13 | + protected $signature = 'forms:publish-view'; |
| 14 | + protected $description = 'Interactively publish one or more form view files to your application for customization.'; |
| 15 | + |
| 16 | + public function handle() |
| 17 | + { |
| 18 | + $sourcePath = __DIR__ . '/../../resources/views'; |
| 19 | + $targetBase = base_path('resources/views/vendor/forms'); |
| 20 | + $filesystem = new Filesystem(); |
| 21 | + $finder = new Finder(); |
| 22 | + $finder->files()->in($sourcePath); |
| 23 | + |
| 24 | + $files = []; |
| 25 | + $options = []; |
| 26 | + foreach ($finder as $file) { |
| 27 | + $relativePath = $file->getRelativePathname(); |
| 28 | + $files[$relativePath] = $file->getRealPath(); |
| 29 | + $targetPath = $targetBase . '/' . $relativePath; |
| 30 | + $label = $relativePath . ($filesystem->exists($targetPath) ? ' (published)' : ''); |
| 31 | + $options[$relativePath] = $label; |
| 32 | + } |
| 33 | + |
| 34 | + if (empty($files)) { |
| 35 | + $this->error('No view files found to publish.'); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + $selected = multisearch( |
| 40 | + label: 'Search and select view files to publish:', |
| 41 | + options: fn ($search) => array_filter( |
| 42 | + $options, |
| 43 | + fn ($label) => stripos($label, $search) !== false |
| 44 | + ), |
| 45 | + required: 'You must select at least one file to publish.' |
| 46 | + ); |
| 47 | + |
| 48 | + foreach ($selected as $relativePath) { |
| 49 | + $target = $targetBase . '/' . $relativePath; |
| 50 | + $filesystem->ensureDirectoryExists(dirname($target)); |
| 51 | + if ($filesystem->exists($target)) { |
| 52 | + if (!confirm( |
| 53 | + label: "{$relativePath} already exists. Overwrite?", |
| 54 | + default: false, |
| 55 | + yes: 'Overwrite', |
| 56 | + no: 'Skip', |
| 57 | + hint: 'If you skip, the existing file will be kept.' |
| 58 | + )) { |
| 59 | + $this->line("Skipped: {$relativePath}"); |
| 60 | + continue; |
| 61 | + } |
| 62 | + } |
| 63 | + $filesystem->copy($files[$relativePath], $target); |
| 64 | + $this->info("Published: {$relativePath}"); |
| 65 | + } |
| 66 | + |
| 67 | + $this->info('Selected view files published successfully.'); |
| 68 | + return 0; |
| 69 | + } |
| 70 | +} |
0 commit comments