Skip to content

Commit

Permalink
Update:refactor ServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-fathi committed Aug 16, 2024
1 parent db713f9 commit c9e2072
Showing 1 changed file with 66 additions and 18 deletions.
84 changes: 66 additions & 18 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,56 +52,104 @@ private function mergeConfig()
/**
*
*/
private function registerBindings()
private function registerBindings(): void
{
$this->registerAllDependencies();

$this->commands([MakeEloquentFilter::class]);
}

/**
* @return void
*/
private function registerAllDependencies(): void
{
/* @var $queryFilterCoreFactory QueryFilterCoreFactory */
$queryFilterCoreFactory = app(QueryFilterCoreFactory::class);

$createQueryFilterBuilder = function ($requestData, QueryFilterCore $queryFilterCore) {
$requestFilter = app(RequestFilter::class, ['request' => $requestData]);
$responseFilter = app(ResponseFilter::class);
/* @var $mainQueryFilterBuilder MainQueryFilterBuilder */
$mainQueryFilterBuilder = $this->setMainQueryFilterBuilder();

return app(MainQueryFilterBuilder::class, [
'queryFilterCore' => $queryFilterCore,
'requestFilter' => $requestFilter,
'responseFilter' => $responseFilter,
]);
};
$this->attachFilterToQueryBuilder($mainQueryFilterBuilder, $queryFilterCoreFactory);

$this->attachResponseFilterToQueryBuilder($mainQueryFilterBuilder, $queryFilterCoreFactory);

\Illuminate\Database\Query\Builder::macro('filter', function ($request = null) use ($createQueryFilterBuilder, $queryFilterCoreFactory) {
$this->setEloquentFilter($mainQueryFilterBuilder, $queryFilterCoreFactory);
}

/**
* @param \Closure $mainQueryFilterBuilder
* @param \eloquentFilter\QueryFilter\Factory\QueryFilterCoreFactory $queryFilterCoreFactory
* @return void
*/
private function attachFilterToQueryBuilder(\Closure $mainQueryFilterBuilder, QueryFilterCoreFactory $queryFilterCoreFactory): void
{
\Illuminate\Database\Query\Builder::macro('filter', function ($request = null) use ($mainQueryFilterBuilder, $queryFilterCoreFactory) {

if (empty($request)) {
$request = request()->query();
}

app()->singleton(
'eloquentFilter',
function () use ($createQueryFilterBuilder, $queryFilterCoreFactory, $request) {
return $createQueryFilterBuilder($request, $queryFilterCoreFactory->createQueryFilterCoreDBQueryBuilder());
function () use ($mainQueryFilterBuilder, $queryFilterCoreFactory, $request) {
return $mainQueryFilterBuilder($request, $queryFilterCoreFactory->createQueryFilterCoreDBQueryBuilder());
}
);

/** @see MainQueryFilterBuilder::apply() */
return app('eloquentFilter')->apply(builder: $this, request: $request);
});
}

\Illuminate\Database\Query\Builder::macro('getResponseFilter', function ($callback = null) use ($createQueryFilterBuilder, $queryFilterCoreFactory) {
/**
* @param \Closure $mainQueryFilterBuilder
* @param \eloquentFilter\QueryFilter\Factory\QueryFilterCoreFactory $queryFilterCoreFactory
* @return void
*/
private function attachResponseFilterToQueryBuilder(\Closure $mainQueryFilterBuilder, QueryFilterCoreFactory $queryFilterCoreFactory): void
{
\Illuminate\Database\Query\Builder::macro('getResponseFilter', function ($callback = null) use ($mainQueryFilterBuilder, $queryFilterCoreFactory) {

if(!empty($callback)){
if (!empty($callback)) {
return call_user_func($callback, EloquentFilter::getResponse());
}

});
}

/**
* @param \Closure $mainQueryFilterBuilder
* @param \eloquentFilter\QueryFilter\Factory\QueryFilterCoreFactory $queryFilterCoreFactory
* @return void
*/
private function setEloquentFilter(\Closure $mainQueryFilterBuilder, QueryFilterCoreFactory $queryFilterCoreFactory): void
{
$this->app->singleton(
'eloquentFilter',
function () use ($createQueryFilterBuilder, $queryFilterCoreFactory) {
function () use ($mainQueryFilterBuilder, $queryFilterCoreFactory) {

/* @see MainQueryFilterBuilder */
return $createQueryFilterBuilder($this->app->get('request')->query(), $queryFilterCoreFactory->createQueryFilterCoreEloquentBuilder());
return $mainQueryFilterBuilder($this->app->get('request')->query(), $queryFilterCoreFactory->createQueryFilterCoreEloquentBuilder());
}
);
}

$this->commands([MakeEloquentFilter::class]);
/**
* @return \Closure
*/
private function setMainQueryFilterBuilder(): \Closure
{
$mainQueryFilterBuilder = function ($requestData, QueryFilterCore $queryFilterCore) {
$requestFilter = app(RequestFilter::class, ['request' => $requestData]);
$responseFilter = app(ResponseFilter::class);

return app(MainQueryFilterBuilder::class, [
'queryFilterCore' => $queryFilterCore,
'requestFilter' => $requestFilter,
'responseFilter' => $responseFilter,
]);
};
return $mainQueryFilterBuilder;
}
}

0 comments on commit c9e2072

Please sign in to comment.