Merge pull request #331 from FrittenKeeZ/blade-directives-registration

Changed how Blade directives are registered to prevent conflicts
This commit is contained in:
Matt Lantz authored and GitHub committed 2017-05-17 02:02:27 -04:00
commit 14e0e0130a
1 file changed
+14 -8
+14 -8
View File
@@ -5,6 +5,7 @@ namespace Collective\Html;
use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
class HtmlServiceProvider extends ServiceProvider class HtmlServiceProvider extends ServiceProvider
{ {
@@ -37,8 +38,7 @@ class HtmlServiceProvider extends ServiceProvider
$this->app->alias('html', HtmlBuilder::class); $this->app->alias('html', HtmlBuilder::class);
$this->app->alias('form', FormBuilder::class); $this->app->alias('form', FormBuilder::class);
$this->setBladeDirectives('Html', get_class_methods(HtmlBuilder::class)); $this->registerBladeExtensions();
$this->setBladeDirectives('Form', get_class_methods(FormBuilder::class));
} }
/** /**
@@ -68,26 +68,32 @@ class HtmlServiceProvider extends ServiceProvider
} }
/** /**
* Set blade directives * Register Blade directives.
*
* @param string $namespace
* @param array $methods
* *
* @return void * @return void
*/ */
protected function setBladeDirectives($namespace, $methods) protected function registerBladeExtensions()
{ {
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$namespaces = [
'Html' => get_class_methods(HtmlBuilder::class),
'Form' => get_class_methods(FormBuilder::class),
];
foreach ($namespaces as $namespace => $methods) {
foreach ($methods as $method) { foreach ($methods as $method) {
if (in_array($method, $this->directives)) { if (in_array($method, $this->directives)) {
$snakeMethod = Str::snake($method); $snakeMethod = Str::snake($method);
$directive = strtolower($namespace).'_'.$snakeMethod; $directive = strtolower($namespace).'_'.$snakeMethod;
Blade::directive($directive, function ($expression) use ($namespace, $method) { $bladeCompiler->directive($directive, function ($expression) use ($namespace, $method) {
return "<?php echo $namespace::$method($expression); ?>"; return "<?php echo $namespace::$method($expression); ?>";
}); });
} }
} }
} }
});
}
/** /**
* Get the services provided by the provider. * Get the services provided by the provider.