Skip to content

Commit 2788efd

Browse files
committed
feat: Artisan command to easily publish view files.
1 parent d552177 commit 2788efd

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

src/FormsServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,12 @@ public function register(): void
5656
$this->app->singleton(FormsDataBinder::class, fn () => new FormsDataBinder());
5757

5858
app('router')->aliasMiddleware('forms', OverrideFormsDefaults::class);
59+
60+
// Register custom artisan commands
61+
if ($this->app->runningInConsole()) {
62+
$this->commands([
63+
\Javaabu\Forms\Commands\PublishViewCommand::class,
64+
]);
65+
}
5966
}
6067
}

0 commit comments

Comments
 (0)