diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 50cc3b8..940307b 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -52,23 +52,39 @@ 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(); @@ -76,32 +92,64 @@ private function registerBindings() 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; } }