From 5dc2d138b8399e75d774ca61db262914f1fef724 Mon Sep 17 00:00:00 2001 From: Frederik Sauer Date: Tue, 16 May 2017 23:57:30 +0200 Subject: [PATCH] Changed how Blade directives are registered to prevent conflicts --- src/HtmlServiceProvider.php | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index e160df6..c74d822 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -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 ""; - }); + 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 ""; + }); + } + } } - } + }); } /**