-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
BladeUIKitServiceProvider.php
120 lines (98 loc) · 3.64 KB
/
BladeUIKitServiceProvider.php
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
<?php
declare(strict_types=1);
namespace BladeUIKit;
use BladeUIKit\Components\BladeComponent;
use BladeUIKit\Components\LivewireComponent;
use BladeUIKit\Console\PublishCommand;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
use Livewire\Livewire;
final class BladeUIKitServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/blade-ui-kit.php', 'blade-ui-kit');
if ($this->app->runningInConsole()) {
$this->commands([
PublishCommand::class,
]);
}
}
public function boot(): void
{
$this->bootResources();
$this->bootBladeComponents();
$this->bootLivewireComponents();
$this->bootDirectives();
$this->bootPublishing();
}
private function bootResources(): void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'blade-ui-kit');
}
private function bootBladeComponents(): void
{
$this->callAfterResolving(BladeCompiler::class, function (BladeCompiler $blade) {
$prefix = config('blade-ui-kit.prefix', '');
$assets = config('blade-ui-kit.assets', []);
/** @var BladeComponent $component */
foreach (config('blade-ui-kit.components', []) as $alias => $component) {
$blade->component($component, $alias, $prefix);
$this->registerAssets($component, $assets);
}
});
}
private function bootLivewireComponents(): void
{
// Skip if Livewire isn't installed.
if (! class_exists(Livewire::class)) {
return;
}
$prefix = config('blade-ui-kit.prefix', '');
$assets = config('blade-ui-kit.assets', []);
/** @var LivewireComponent $component */
foreach (config('blade-ui-kit.livewire', []) as $alias => $component) {
$alias = $prefix ? "$prefix-$alias" : $alias;
Livewire::component($alias, $component);
$this->registerAssets($component, $assets);
}
}
private function registerAssets($component, array $assets): void
{
foreach ($component::assets() as $asset) {
$files = (array)($assets[$asset] ?? []);
collect($files)->filter(function (string $file) {
return Str::endsWith($file, '.css');
})->each(function (string $style) {
BladeUIKit::addStyle($style);
});
collect($files)->filter(function (string $file) {
return Str::endsWith($file, '.js');
})->each(function (string $script) {
BladeUIKit::addScript($script);
});
}
}
private function bootDirectives(): void
{
Blade::directive('bukStyles', function (string $expression) {
return "<?php echo BladeUIKit\\BladeUIKit::outputStyles($expression); ?>";
});
Blade::directive('bukScripts', function (string $expression) {
return "<?php echo BladeUIKit\\BladeUIKit::outputScripts($expression); ?>";
});
}
private function bootPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/blade-ui-kit.php' => $this->app->configPath('blade-ui-kit.php'),
], 'blade-ui-kit-config');
$this->publishes([
__DIR__ . '/../resources/views' => $this->app->resourcePath('views/vendor/blade-ui-kit'),
], 'blade-ui-kit-views');
}
}
}