Skip to content

Commit

Permalink
Implementation of @scopedslot & @endscopedslot directives
Browse files Browse the repository at this point in the history
  • Loading branch information
konradkalemba committed Jul 10, 2019
1 parent 6fb0943 commit 0b5061d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/BladeComponentsScopedSlotsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace KonradKalemba\BladeComponentsScopedSlots;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class BladeComponentsScopedSlotsServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Blade::directive('scopedslot', function ($expression) {
// Split the expression by `top-level` commas (not in parentheses)
$directiveArguments = preg_split("/,(?![^\(\(]*[\)\)])/", $expression);
$directiveArguments = array_map('trim', $directiveArguments);

// Ensure that the directive's arguments array has 3 elements - otherwise fill with `null`
$directiveArguments = array_pad($directiveArguments, 3, null);

// Extract values from the directive's arguments array
[$name, $functionArguments, $functionUses] = $directiveArguments;

// Connect the arguments to form a correct function declaration
if ($functionArguments) $functionArguments = "function {$functionArguments}";
if ($functionUses) $functionUses = " use {$functionUses}";

return "<?php \$__env->slot({$name}, {$functionArguments}{$functionUses} { ?>";
});

Blade::directive('endscopedslot', function () {
return "<?php }); ?>";
});
}
}

0 comments on commit 0b5061d

Please sign in to comment.