Skip to content

Commit

Permalink
Add mixin support to Eloquent Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
imliam committed Dec 30, 2019
1 parent 0ba0569 commit 7cb1ff6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Expand Up @@ -13,6 +13,8 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use ReflectionClass;
use ReflectionMethod;

/**
* @property-read HigherOrderBuilderProxy $orWhere
Expand Down Expand Up @@ -1372,6 +1374,24 @@ public static function __callStatic($method, $parameters)
return;
}

if ($method === 'mixin') {
$mixin = $parameters[0];
$replace = $parameters[1] ?? true;

$methods = (new ReflectionClass($mixin))->getMethods(
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
);

foreach ($methods as $method) {
if ($replace || ! static::hasMacro($method->name)) {
$method->setAccessible(true);
static::macro($method->name, $method->invoke($mixin));
}
}

return;
}

if (! static::hasGlobalMacro($method)) {
static::throwBadMethodCallException($method);
}
Expand Down

0 comments on commit 7cb1ff6

Please sign in to comment.