-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of
@scopedslot
& @endscopedslot
directives
- Loading branch information
1 parent
6fb0943
commit 0b5061d
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); ?>"; | ||
}); | ||
} | ||
} |