You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Listener/ModelPrepareListener.php

86 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Listener;
use App\Helper\Macro\WhereHasIn;
use App\Helper\Macro\WhereHasMorphIn;
use App\Helper\Macro\WhereHasNotIn;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\Relation;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
/**
* @Listener
*/
class ModelPrepareListener implements ListenerInterface
{
public function listen(): array
{
return [
BootApplication::class,
];
}
public function process(object $event)
{
$this->morphMapRelation();
$this->macroWhereHasIn();
}
private function morphMapRelation()
{
Relation::morphMap([
]);
}
private function macroWhereHasIn()
{
Builder::macro('whereHasIn', function ($relationName, $callable = null) {
return (new WhereHasIn($this, $relationName, function ($nextRelation, $builder) use ($callable) {
if ($nextRelation) {
return $builder->whereHasIn($nextRelation, $callable);
}
if ($callable) {
return $builder->callScope($callable);
}
return $builder;
}))->execute();
});
Builder::macro('orWhereHasIn', function ($relationName, $callable = null) {
return $this->orWhere(function ($query) use ($relationName, $callable) {
return $query->whereHasIn($relationName, $callable);
});
});
Builder::macro('whereHasNotIn', function ($relationName, $callable = null) {
return (new WhereHasNotIn($this, $relationName, function ($nextRelation, $builder) use ($callable) {
if ($nextRelation) {
return $builder->whereHasNotIn($nextRelation, $callable);
}
if ($callable) {
return $builder->callScope($callable);
}
return $builder;
}))->execute();
});
Builder::macro('orWhereHasNotIn', function ($relationName, $callable = null) {
return $this->orWhere(function ($query) use ($relationName, $callable) {
return $query->whereHasNotIn($relationName, $callable);
});
});
Builder::macro('whereHasMorphIn', WhereHasMorphIn::make());
Builder::macro('orWhereHasMorphIn', function ($relation, $types, $callback = null) {
return $this->whereHasMorphIn($relation, $types, $callback, 'or');
});
}
}