Skip to content

Commit

Permalink
Revert "Remove bakeryPaginate workaround (#128)" (#133)
Browse files Browse the repository at this point in the history
* Revert "🧹 Remove `bakeryPaginate` workaround (#128)"

This reverts commit 23dc13f.

* Ignore macros directory from analysis
  • Loading branch information
robertvansteen authored Dec 20, 2019
1 parent c91f880 commit b76bf3d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ parameters:
level: 0
paths:
- %currentWorkingDirectory%/src/
excludes_analyse:
- %currentWorkingDirectory%/src/macros/*
12 changes: 12 additions & 0 deletions src/BakeryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function register()

$this->registerBakery();

$this->registerMacros();

$this->commands([
Console\InstallCommand::class,
Console\ModelSchemaCommand::class,
Expand Down Expand Up @@ -93,4 +95,14 @@ protected function registerSecurityRules()
DocumentValidator::addRule(new DisableIntrospection());
}
}

/**
* Register the macros used by Bakery.
*
* @return void
*/
protected function registerMacros()
{
require_once __DIR__.'/macros/bakeryPaginate.php'; // TODO: Remove this once fixed upstream.
}
}
3 changes: 1 addition & 2 deletions src/Queries/EloquentCollectionQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public function resolve(Arguments $args, $root, $context, ResolveInfo $info): Le
$query = $this->applyOrderBy($query, $args->orderBy);
}

return $query->distinct($this->model->getQualifiedKeyName())
->paginate($count, ['*'], 'page', $page);
return $query->distinct()->bakeryPaginate($count, ['*'], 'page', $page);
}
}
32 changes: 32 additions & 0 deletions src/macros/bakeryPaginate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;

/*
* Paginate the given query.
*
* @param int $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
*/
Builder::macro('bakeryPaginate', function ($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) {
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();

$countColumns = $this->query->distinct ? [$this->model->getQualifiedKeyName()] : ['*'];

$results = ($total = $this->toBase()->getCountForPagination($countColumns))
? $this->forPage($page, $perPage)->get($columns)
: $this->model->newCollection();

return $this->paginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
});

0 comments on commit b76bf3d

Please sign in to comment.