50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace AcornPlugin\Providers;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Livewire\Livewire;
|
|
|
|
/**
|
|
* Registers this plugin's views, Livewire component and shortcode against the
|
|
* shared Acorn container. Auto-discovered via composer.json extra.acorn.providers,
|
|
* so it is registered by whichever plugin boots Acorn. Everything is namespaced,
|
|
* so the plugin coexists with other Acorn plugins in the same container.
|
|
*
|
|
* Extends Illuminate\Support\ServiceProvider (not Roots\Acorn\ServiceProvider,
|
|
* which is `final` in Acorn v6 and exists only to expose defaultProviders()).
|
|
* Acorn's own providers (Sage, Assets, View …) all extend the Illuminate base,
|
|
* which carries the protected loadViewsFrom() helper this provider relies on.
|
|
*/
|
|
class PluginServiceProvider extends ServiceProvider
|
|
{
|
|
/** Shared kebab prefix for this plugin's Blade + Livewire namespaces. */
|
|
private const NAMESPACE = 'acorn-starter';
|
|
|
|
public function boot(): void
|
|
{
|
|
$this->loadViewsFrom($this->resourcePath('views'), self::NAMESPACE);
|
|
$this->registerLivewireComponents();
|
|
$this->registerShortcodes();
|
|
}
|
|
|
|
private function registerLivewireComponents(): void
|
|
{
|
|
if (! class_exists(Livewire::class)) {
|
|
return;
|
|
}
|
|
|
|
Livewire::addNamespace(self::NAMESPACE, $this->resourcePath('views/components'));
|
|
}
|
|
|
|
private function registerShortcodes(): void
|
|
{
|
|
add_shortcode('acorn_hello', fn () => view(self::NAMESPACE . '::hello-world')->render());
|
|
}
|
|
|
|
private function resourcePath(string $path = ''): string
|
|
{
|
|
return dirname(__DIR__, 2) . '/resources/' . ltrim($path, '/');
|
|
}
|
|
}
|