Repository navigation
Expand file tree
/
Copy pathWireFormsServiceProvider.php
More file actions
126 lines (114 loc) · 5.46 KB
/
Copy pathWireFormsServiceProvider.php
File metadata and controls
126 lines (114 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace NyonCode\WireForms;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Livewire\ComponentHookRegistry;
use NyonCode\LaravelPackageToolkit\Commands\InstallCommand;
use NyonCode\LaravelPackageToolkit\Packager;
use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\WireCore\Actions\Contracts\ModalFormFactory;
use NyonCode\WireCore\Foundation\Assets\Bundle;
use NyonCode\WireCore\Foundation\Icons\IconManager;
use NyonCode\WireForms\Components\Console\MakeFieldCommand;
use NyonCode\WireForms\Forms\Form;
use NyonCode\WireForms\Forms\Runtime\FieldPartialHook;
use NyonCode\WireForms\Forms\Support\FormModalFormFactory;
use NyonCode\WireForms\Integration\ActionMacros;
use NyonCode\WireForms\Support\Icons\FormsIconSet;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class WireFormsServiceProvider extends PackageServiceProvider
{
/** Absolute path to the pre-bundled, self-registering field assets. */
public const ASSETS_PATH = __DIR__.'/../dist';
/**
* @throws \Exception
*/
public function register(): void
{
parent::register();
// In the REGISTER phase, for the same reason wire-core registers its own:
// ComponentHookRegistry::boot() walks what is registered at boot time, so
// a hook added later is silently absent for the first component of the
// process.
ComponentHookRegistry::register(FieldPartialHook::class);
}
public function configure(Packager $packager): void
{
$packager
->name('WireForms')
->hasShortName('wire-forms')
->registeredPackage(function ($packager) {
$this->app->bind(Form::class, fn () => new Form);
// wire-core resolves this to build action-modal forms without
// naming Form (keeps the core → forms dependency inverted-free).
$this->app->singleton(ModalFormFactory::class, FormModalFormFactory::class);
})
->bootedPackage(function ($packager) {
Blade::componentNamespace('NyonCode\\WireForms\\Components', 'wire-forms');
ActionMacros::register();
Bundle::serve('wire-forms', self::ASSETS_PATH);
$this->registerTiptapRoute();
// The glyphs Heroicons has no answer for, as `forms:bold` and
// friends. Registering the set costs nothing until one is asked
// for — the bodies load on first use — and it is what keeps the
// editors and the rating field free of inline <svg>
// (AI_CODING_STANDARD.md, Rendering § Icons).
app(IconManager::class)->registerIconSet(new FormsIconSet, 'forms');
})
->hasConfig()
->hasViews()
// The custom-component generators and their templates, publishable
// with `vendor:publish --tag=wire-forms::stubs`; a published copy wins
// (Foundation\Console\Support\PublishedStubs says where it is looked for).
->hasCommand(MakeFieldCommand::class)
->hasStubs([
'../stubs/field.stub',
'../stubs/field-view.stub',
])
->hasAssets('dist', entries: [
Bundle::make('wire-forms-image.js'),
// The field controllers (date/time pickers, tags, rating, the
// editors, the colour picker, OTP, phone, signature). A
// registrar, so it ships with the document rather than being
// delivered per field — see architecture/assets.md.
Bundle::make('wire-forms-fields.js'),
])
->hasAssetFallback(Bundle::servedByRoute('wire-forms'))
->hasTranslations('resources/lang')
->hasAbout()
->hasInstallCommand(function (InstallCommand $command) {
// See WireCoreServiceProvider — publishing assets is a stack-wide
// delivery choice, not a per-package installer step.
$command
->publishConfig()
->publishViews()
->publishTranslations();
});
}
/**
* Serve the TipTap editor's code-split ESM bundle by filename.
*
* Not `Bundle::serve()`: that route takes a bundle id and answers with one
* IIFE, while these are ESM entries whose relative `import "./chunk-<hash>.js"`
* has to resolve against the same directory. Same delivery decision (ADR 0024),
* different shape.
*/
protected function registerTiptapRoute(): void
{
// The TipTap editor ships as an ESM code-split bundle (entry + shared core
// chunk + opt-in addon entry); serve any .js from dist/tiptap by filename so
// an entry's relative `import "./chunk-<hash>.js"` resolves. basename() bars
// path traversal; the hashed chunk name is its own cache key.
Route::get('/wire-forms/tiptap/{file}', function (string $file): BinaryFileResponse {
$path = self::ASSETS_PATH.'/tiptap/'.basename($file);
abort_unless(is_file($path) && str_ends_with($path, '.js'), 404);
return response()
->file($path, ['Content-Type' => 'application/javascript; charset=utf-8'])
->setPublic()
->setMaxAge(31536000);
})
->where('file', '[A-Za-z0-9_.-]+')
->name('wire-forms.tiptap');
}
}