Skip to content

Commit

Permalink
Rebind $this for static closure macros
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Aug 13, 2019
1 parent 11b6941 commit f3b4512
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Illuminate/Support/Traits/Macroable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use ReflectionClass;
use ReflectionMethod;
use ReflectionFunction;
use BadMethodCallException;

trait Macroable
Expand Down Expand Up @@ -80,11 +81,15 @@ public static function __callStatic($method, $parameters)
));
}

if (static::$macros[$method] instanceof Closure) {
return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
$macro = static::$macros[$method];

if ($macro instanceof Closure) {
$newThis = (new ReflectionFunction($macro))->getClosureThis();

return call_user_func_array($macro->bindTo($newThis), $parameters);
}

return call_user_func_array(static::$macros[$method], $parameters);
return call_user_func_array($macro, $parameters);
}

/**
Expand Down

0 comments on commit f3b4512

Please sign in to comment.