refactor: remove DirectiveServiceProvider and its directives from application bootstrap
This commit is contained in:
parent
bdffb30b12
commit
7d6f6010f4
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\{Arr, ServiceProvider};
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
|
||||
final class DirectiveServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerScopeDirective();
|
||||
$this->registerInitialsDirective();
|
||||
}
|
||||
|
||||
private function registerInitialsDirective(): void
|
||||
{
|
||||
Blade::directive('initials', fn ($expression) => "<?php
|
||||
echo implode('', array_map(function(\$w) {
|
||||
return strtoupper(\$w[0] ?? '');
|
||||
}, explode(' ', $expression)));
|
||||
?>");
|
||||
}
|
||||
|
||||
private function registerScopeDirective(): void
|
||||
{
|
||||
/**
|
||||
* All credits from this blade directive goes to Konrad Kalemba.
|
||||
* Just copied and modified for my very specific use case.
|
||||
*
|
||||
* https://github.com/konradkalemba/blade-components-scoped-slots
|
||||
*/
|
||||
Blade::directive('scope', function ($expression) {
|
||||
// Split the expression by `top-level` commas (not in parentheses)
|
||||
$directiveArguments = preg_split("/,(?![^\(\(]*[\)\)])/", $expression);
|
||||
$directiveArguments = array_map('trim', $directiveArguments);
|
||||
|
||||
// PHP 8.5 Safe Unpacking (Prevents "Undefined array key" crashes)
|
||||
$name = $directiveArguments[0] ?? "''";
|
||||
$functionArguments = $directiveArguments[1] ?? '';
|
||||
|
||||
// Build function "uses" to inject extra external variables
|
||||
$uses = Arr::except(array_flip($directiveArguments), [$name, $functionArguments]);
|
||||
$uses = array_flip($uses);
|
||||
$uses[] = '$__env';
|
||||
$uses[] = '$__bladeCompiler';
|
||||
$uses = implode(',', $uses);
|
||||
|
||||
/**
|
||||
* Slot names can`t contains dot , eg: `user.city`.
|
||||
* So we convert `user.city` to `user___city`
|
||||
*
|
||||
* Later, on component it will be replaced back.
|
||||
*/
|
||||
$name = str_replace('.', '___', $name);
|
||||
|
||||
// PHP 8.5 Safe Loop Check
|
||||
return "<?php \$__bladeCompiler = \$__bladeCompiler ?? null; \$__env->slot({$name}, function({$functionArguments}) use ({$uses}) { \$loop = !empty(\$__env->getLoopStack()) ? (object) \$__env->getLoopStack()[0] : null; ?>";
|
||||
});
|
||||
|
||||
Blade::directive('endscope', fn () => '<?php }); ?>');
|
||||
}
|
||||
}
|
||||
@ -5,5 +5,4 @@
|
||||
return [
|
||||
App\Providers\AppServiceProvider::class,
|
||||
App\Providers\FortifyServiceProvider::class,
|
||||
App\Providers\DirectiveServiceProvider::class,
|
||||
];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user