Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/shared-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
15 changes: 10 additions & 5 deletions src/Providers/SharedDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,10 +16,6 @@ class SharedDataServiceProvider extends ServiceProvider implements DeferrablePro
*/
public function boot()
{
Blade::directive('shared', function () {
return '<?php echo app(\Coderello\SharedData\SharedData::class)->render(); ?>';
});

$this->publishes([
__DIR__.'/../../config/shared-data.php' => config_path('shared-data.php'),
], 'shared-data-config');
Expand All @@ -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 '<?php echo app(\\'.SharedData::class.'::class)->render(); ?>';
});

return $bladeCompiler;
});
}

/**
Expand All @@ -51,6 +55,7 @@ public function provides()
{
return [
SharedData::class,
'blade.compiler'
];
}
}
18 changes: 17 additions & 1 deletion tests/SharedDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Coderello\SharedData\Tests;

use Coderello\SharedData\Providers\SharedDataServiceProvider;
use Coderello\SharedData\SharedData;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;
Expand Down Expand Up @@ -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()
);
}
}
1 change: 1 addition & 0 deletions tests/views/shared-custom.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@shared_custom