Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add mixin support to Eloquent builder #30978

Merged
merged 1 commit into from Jan 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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