diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 7911462ebf3..396c21a4509 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -934,22 +934,37 @@ public function aliasInclude($path, $alias = null) }); } + /** + * Register a handler for custom directives, binding the handler to the compiler. + * + * @param string $name + * @param callable $handler + * @return void + * + * @throws \InvalidArgumentException + */ + public function bindDirective($name, callable $handler) + { + $this->directive($name, $handler, bind: true); + } + /** * Register a handler for custom directives. * * @param string $name * @param callable $handler + * @param bool $bind * @return void * * @throws \InvalidArgumentException */ - public function directive($name, callable $handler) + public function directive($name, callable $handler, bool $bind = false) { if (! preg_match('/^\w+(?:::\w+)?$/x', $name)) { throw new InvalidArgumentException("The directive name [{$name}] is not valid. Directive names must only contain alphanumeric characters and underscores."); } - $this->customDirectives[$name] = $handler; + $this->customDirectives[$name] = $bind ? $handler->bindTo($this, BladeCompiler::class) : $handler; } /**