Skip to content

Commit 63fb79d

Browse files
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 <[email protected]>
1 parent 4b5ce19 commit 63fb79d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,22 +934,37 @@ public function aliasInclude($path, $alias = null)
934934
});
935935
}
936936

937+
/**
938+
* Register a handler for custom directives, binding the handler to the compiler.
939+
*
940+
* @param string $name
941+
* @param callable $handler
942+
* @return void
943+
*
944+
* @throws \InvalidArgumentException
945+
*/
946+
public function bindDirective($name, callable $handler)
947+
{
948+
$this->directive($name, $handler, bind: true);
949+
}
950+
937951
/**
938952
* Register a handler for custom directives.
939953
*
940954
* @param string $name
941955
* @param callable $handler
956+
* @param bool $bind
942957
* @return void
943958
*
944959
* @throws \InvalidArgumentException
945960
*/
946-
public function directive($name, callable $handler)
961+
public function directive($name, callable $handler, bool $bind = false)
947962
{
948963
if (! preg_match('/^\w+(?:::\w+)?$/x', $name)) {
949964
throw new InvalidArgumentException("The directive name [{$name}] is not valid. Directive names must only contain alphanumeric characters and underscores.");
950965
}
951966

952-
$this->customDirectives[$name] = $handler;
967+
$this->customDirectives[$name] = $bind ? $handler->bindTo($this, BladeCompiler::class) : $handler;
953968
}
954969

955970
/**

0 commit comments

Comments
 (0)