diff --git a/config/shared-data.php b/config/shared-data.php index 3af81a5..b90bb41 100644 --- a/config/shared-data.php +++ b/config/shared-data.php @@ -33,4 +33,12 @@ 'name' => 'shared', ], + /* + * Blade directive name. + * + * By default the Blade directive named 'shared'. + * + * It means that the shared data rendering will be available in view files via `@shared` + */ + 'blade_directive' => 'shared', ]; diff --git a/src/Providers/SharedDataServiceProvider.php b/src/Providers/SharedDataServiceProvider.php index 31fa7df..91b283f 100644 --- a/src/Providers/SharedDataServiceProvider.php +++ b/src/Providers/SharedDataServiceProvider.php @@ -3,8 +3,8 @@ namespace Coderello\SharedData\Providers; use Coderello\SharedData\SharedData; -use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; +use Illuminate\View\Compilers\BladeCompiler; use Illuminate\Contracts\Support\DeferrableProvider; class SharedDataServiceProvider extends ServiceProvider implements DeferrableProvider @@ -16,10 +16,6 @@ class SharedDataServiceProvider extends ServiceProvider implements DeferrablePro */ public function boot() { - Blade::directive('shared', function () { - return 'render(); ?>'; - }); - $this->publishes([ __DIR__.'/../../config/shared-data.php' => config_path('shared-data.php'), ], 'shared-data-config'); @@ -40,6 +36,14 @@ public function register() $this->app->singleton(SharedData::class, function () { return new SharedData($this->app['config']['shared-data']); }); + + $this->app->extend('blade.compiler', function (BladeCompiler $bladeCompiler) { + $bladeCompiler->directive($this->app['config']['shared-data']['blade_directive'], function () { + return 'render(); ?>'; + }); + + return $bladeCompiler; + }); } /** @@ -51,6 +55,7 @@ public function provides() { return [ SharedData::class, + 'blade.compiler' ]; } } diff --git a/tests/SharedDataTest.php b/tests/SharedDataTest.php index d884346..e6e6d75 100644 --- a/tests/SharedDataTest.php +++ b/tests/SharedDataTest.php @@ -2,6 +2,7 @@ namespace Coderello\SharedData\Tests; +use Coderello\SharedData\Providers\SharedDataServiceProvider; use Coderello\SharedData\SharedData; use Illuminate\Contracts\Support\Arrayable; use JsonSerializable; @@ -293,11 +294,26 @@ public function testForget() $this->assertSame([], $this->sharedData->get()); } - public function testDirective() + public function testBladeDirective() { $this->assertEquals( shared()->render(), view('shared')->render() ); } + + /** + * @depends testBladeDirective + */ + public function testBladeDirectiveCustom() + { + $this->app->make('config')->set('shared-data.blade_directive', 'shared_custom'); + + $this->app->register(SharedDataServiceProvider::class); + + $this->assertEquals( + shared()->render(), + view('shared-custom')->render() + ); + } } diff --git a/tests/views/shared-custom.blade.php b/tests/views/shared-custom.blade.php new file mode 100644 index 0000000..07ab184 --- /dev/null +++ b/tests/views/shared-custom.blade.php @@ -0,0 +1 @@ +@shared_custom