-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathVoltServiceProvider.php
More file actions
135 lines (114 loc) · 4.01 KB
/
VoltServiceProvider.php
File metadata and controls
135 lines (114 loc) · 4.01 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
127
128
129
130
131
132
133
134
135
<?php
namespace Livewire\Volt;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;
use Livewire\Livewire;
use Livewire\LivewireManager as BaseLivewireManager;
use Livewire\LivewireServiceProvider;
use Livewire\Volt\Precompilers\ExtractFragments;
use Livewire\Volt\Precompilers\ExtractTemplate;
class VoltServiceProvider extends ServiceProvider
{
/**
* Register any package services.
*/
public function register(): void
{
$this->app->singleton(ComponentFactory::class);
$this->app->singleton(ComponentResolver::class);
$this->app->singleton(ExtractFragments::class);
$this->app->singleton(ExtractTemplate::class);
$this->app->singleton(MountedDirectories::class);
$this->app->singleton(VoltManager::class);
$this->app->when(ExtractFragments::class)
->needs('$compiledViewPath')
->give(fn () => config('view.compiled'));
}
/**
* Bootstrap any package services.
*/
public function boot(): void
{
$this->registerCommands();
$this->registerPublishing();
$this->registerTestingMacros();
Blade::prepareStringsForCompilationUsing(function (string $value) {
foreach ([ExtractFragments::class, ExtractTemplate::class] as $precompiler) {
$value = $this->app->make($precompiler)->__invoke($value);
}
return $value;
});
$this->app->booted(function () {
$this->bindLivewireManager();
$this->ensureMissingLivewireComponentsCanBeResolved();
});
}
/**
* Bind the custom Volt Livewire manager in the container.
*/
protected function bindLivewireManager(): void
{
$this->app->singleton(LivewireManager::class);
$this->app->alias(LivewireManager::class, 'livewire');
$this->app->alias(LivewireManager::class, BaseLivewireManager::class);
Facade::clearResolvedInstance('livewire');
Facade::clearResolvedInstance(BaseLivewireManager::class);
$this->app->get(LivewireManager::class)->setProvider(
$this->app->getProvider(LivewireServiceProvider::class),
);
}
/**
* Ensure that any missing Livewire components can be resolved.
*/
protected function ensureMissingLivewireComponentsCanBeResolved(): void
{
Livewire::resolveMissingComponent(fn (string $name) => app(ComponentResolver::class)->resolve(
$name, collect(app(MountedDirectories::class)->paths())->pluck('path')->all(),
));
}
/**
* Register the package's commands.
*/
protected function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Console\InstallCommand::class,
Console\MakeCommand::class,
]);
}
}
/**
* Register the package's publishable resources.
*/
private function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../stubs/VoltServiceProvider.stub' => app_path('Providers/VoltServiceProvider.php'),
], 'volt-provider');
}
}
/**
* Register the package's testing macros.
*/
protected function registerTestingMacros(): void
{
TestResponse::macro('assertSeeVolt', function ($component) {
Volt::ensureViewsAreCached();
if (FragmentMap::has($component)) {
$component = FragmentMap::get($component);
}
return $this->assertSeeLivewire($component);
});
TestResponse::macro('assertDontSeeVolt', function ($component) {
Volt::ensureViewsAreCached();
if (FragmentMap::has($component)) {
$component = FragmentMap::get($component);
}
return $this->assertDontSeeLivewire($component);
});
}
}