From 63fb79de81d1e34f375da37459f0d183cebc2a0a Mon Sep 17 00:00:00 2001 From: Hossein Zare <56504893+hossein-zare@users.noreply.github.com> Date: Thu, 24 Oct 2024 17:56:35 +0330 Subject: [PATCH] Add `$bind` parameter to `Blade::directive` (#53279) * Add `$bind` parameter to allow binding custom Blade directive handlers to the BladeCompiler instance. * add method --------- Co-authored-by: Taylor Otwell --- .../View/Compilers/BladeCompiler.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 7911462ebf32..396c21a45099 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; } /**