Skip to content

Commit

Permalink
Add $bind parameter to Blade::directive (#53279)
Browse files Browse the repository at this point in the history
* Add `$bind` parameter to allow binding custom Blade directive handlers to the BladeCompiler instance.

* add method

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
hossein-zare and taylorotwell authored Oct 24, 2024
1 parent 4b5ce19 commit 63fb79d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 63fb79d

Please sign in to comment.