Changed how Blade directives are registered to prevent conflicts
This commit is contained in:
1 parent
023efce5fd
commit
5dc2d138b8
1 file changed
+21
-15
+21
-15
@@ -5,6 +5,7 @@ namespace Collective\Html;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\Compilers\BladeCompiler;
|
||||
|
||||
class HtmlServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -37,8 +38,7 @@ class HtmlServiceProvider extends ServiceProvider
|
||||
$this->app->alias('html', HtmlBuilder::class);
|
||||
$this->app->alias('form', FormBuilder::class);
|
||||
|
||||
$this->setBladeDirectives('Html', get_class_methods(HtmlBuilder::class));
|
||||
$this->setBladeDirectives('Form', get_class_methods(FormBuilder::class));
|
||||
$this->registerBladeExtensions();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,25 +68,31 @@ class HtmlServiceProvider extends ServiceProvider
|
||||
}
|
||||
|
||||
/**
|
||||
* Set blade directives
|
||||
*
|
||||
* @param string $namespace
|
||||
* @param array $methods
|
||||
* Register Blade directives.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setBladeDirectives($namespace, $methods)
|
||||
protected function registerBladeExtensions()
|
||||
{
|
||||
foreach ($methods as $method) {
|
||||
if (in_array($method, $this->directives)) {
|
||||
$snakeMethod = Str::snake($method);
|
||||
$directive = strtolower($namespace).'_'.$snakeMethod;
|
||||
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
|
||||
$namespaces = [
|
||||
'Html' => get_class_methods(HtmlBuilder::class),
|
||||
'Form' => get_class_methods(FormBuilder::class),
|
||||
];
|
||||
|
||||
Blade::directive($directive, function ($expression) use ($namespace, $method) {
|
||||
return "<?php echo $namespace::$method($expression); ?>";
|
||||
});
|
||||
foreach ($namespaces as $namespace => $methods) {
|
||||
foreach ($methods as $method) {
|
||||
if (in_array($method, $this->directives)) {
|
||||
$snakeMethod = Str::snake($method);
|
||||
$directive = strtolower($namespace).'_'.$snakeMethod;
|
||||
|
||||
$bladeCompiler->directive($directive, function ($expression) use ($namespace, $method) {
|
||||
return "<?php echo $namespace::$method($expression); ?>";
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user