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
+21 -15
+21 -15
View File
@@ -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); ?>";
});
}
}
}
}
});
}
/**