Bläddra i källkod

update EloquentRepository

update
jqh 5 år sedan
förälder
incheckning
b55bc8767b
3 ändrade filer med 132 tillägg och 120 borttagningar
  1. 1 1
      src/Grid/Exporters/AbstractExporter.php
  2. 30 119
      src/Grid/Model.php
  3. 101 0
      src/Repositories/EloquentRepository.php

+ 1 - 1
src/Grid/Exporters/AbstractExporter.php

@@ -189,7 +189,7 @@ abstract class AbstractExporter implements ExporterInterface
         $array = $this->grid->processFilter(true);
 
         $model->reset();
-        $model->rejectQueries('forPage');
+        $model->rejectQuery('forPage');
 
         return $this->callBuilder($array);
     }

+ 30 - 119
src/Grid/Model.php

@@ -219,6 +219,14 @@ class Model
         $this->usePaginate = $use;
     }
 
+    /**
+     * @return bool
+     */
+    public function allowPagination()
+    {
+        return $this->usePaginate;
+    }
+
     /**
      * Get the query string variable used to store the per-page.
      *
@@ -450,9 +458,6 @@ class Model
             return $this->paginator->getCollection();
         }
 
-        $this->setSort();
-        $this->setPaginate();
-
         if ($this->builder && is_callable($this->builder)) {
             $results = call_user_func($this->builder, $this);
         } else {
@@ -544,61 +549,6 @@ class Model
         return $this->request->get($this->perPageName) ?: $this->perPage;
     }
 
-    /**
-     * Set the grid paginate.
-     *
-     * @return void
-     */
-    protected function setPaginate()
-    {
-        $paginate = $this->findQueryByMethod('paginate');
-
-        $this->queries = $this->queries->reject(function ($query) {
-            return $query['method'] == 'paginate';
-        });
-
-        if (! $this->usePaginate) {
-            $query = [
-                'method'    => 'get',
-                'arguments' => [],
-            ];
-        } else {
-            $query = [
-                'method'    => 'paginate',
-                'arguments' => $this->resolvePerPage($paginate),
-            ];
-        }
-
-        $this->queries->push($query);
-    }
-
-    /**
-     * Resolve perPage for pagination.
-     *
-     * @param array|null $paginate
-     *
-     * @return array
-     */
-    protected function resolvePerPage($paginate)
-    {
-        if ($perPage = app('request')->input($this->perPageName)) {
-            if (is_array($paginate)) {
-                $paginate['arguments'][0] = (int) $perPage;
-
-                return $paginate['arguments'];
-            }
-
-            $this->perPage = (int) $perPage;
-        }
-
-        return [
-            $this->perPage,
-            $this->repository->getGridColumns(),
-            $this->pageName,
-            $this->getCurrentPage(),
-        ];
-    }
-
     /**
      * Find query by method name.
      *
@@ -645,91 +595,52 @@ class Model
      */
     public function getSort()
     {
-        if (empty($this->sort['column']) || empty($this->sort['type'])) {
-            return [null, null];
+        if (empty($this->sort)) {
+            $this->sort = $this->request->get($this->getSortName());
         }
 
-        return [$this->sort['column'], $this->sort['type']];
-    }
-
-    /**
-     * Set the grid sort.
-     *
-     * @return void
-     */
-    protected function setSort()
-    {
-        $this->sort = $this->request->get($this->sortName, []);
-
         if (empty($this->sort['column']) || empty($this->sort['type'])) {
-            return;
+            return [null, null];
         }
 
-        if (Str::contains($this->sort['column'], '.')) {
-            $this->setRelationSort($this->sort['column']);
-        } else {
-            $this->resetOrderBy();
-
-            $this->queries->push([
-                'method'    => 'orderBy',
-                'arguments' => [$this->sort['column'], $this->sort['type']],
-            ]);
-        }
+        return [$this->sort['column'], $this->sort['type']];
     }
 
     /**
-     * Set relation sort.
-     *
-     * @param string $column
+     * @param string|array $method
      *
      * @return void
      */
-    protected function setRelationSort($column)
+    public function rejectQuery($method)
     {
-        [$relationName, $relationColumn] = explode('.', $column);
-
-        if ($this->queries->contains(function ($query) use ($relationName) {
-            return $query['method'] == 'with' && in_array($relationName, $query['arguments']);
-        })) {
-            $this->queries->push([
-                'method'    => 'select',
-                'arguments' => ['*'],
-            ]);
-
-            $this->resetOrderBy();
+        $this->queries = $this->queries->reject(function ($query) use ($method) {
+            if (is_callable($method)) {
+                return call_user_func($method, $query);
+            }
 
-            $this->queries->push([
-                'method'    => 'orderBy',
-                'arguments' => [
-                    $relationColumn,
-                    $this->sort['type'],
-                ],
-            ]);
-        }
+            return in_array($query['method'], (array) $method, true);
+        });
     }
 
     /**
-     * @param string|array $method
+     * Reset orderBy query.
      *
      * @return void
      */
-    public function rejectQueries($method)
+    public function resetOrderBy()
     {
-        $method = (array) $method;
-
-        $this->queries = $this->queries->reject(function ($query) use ($method) {
-            return in_array($query['method'], $method);
-        });
+        $this->rejectQuery(['orderBy', 'orderByDesc']);
     }
 
     /**
-     * Reset orderBy query.
+     * @param string $method
+     * @param array  $arguments
      *
-     * @return void
+     * @return $this
      */
-    public function resetOrderBy()
+    public function __call($method, $arguments)
     {
-        $this->rejectQueries(['orderBy', 'orderByDesc']);
+        return $this->addQuery($method, $arguments);
     }
 
     /**
@@ -738,7 +649,7 @@ class Model
      *
      * @return $this
      */
-    public function __call($method, $arguments)
+    public function addQuery(string $method, array $arguments = [])
     {
         $this->queries->push([
             'method'    => $method,
@@ -781,7 +692,7 @@ class Model
             $this->eagerLoads[] = $relations;
         }
 
-        return $this->__call('with', (array) $relations);
+        return $this->addQuery('with', (array) $relations);
     }
 
     /**

+ 101 - 0
src/Repositories/EloquentRepository.php

@@ -144,6 +144,9 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function get(Grid\Model $model)
     {
+        $this->setSort($model);
+        $this->setPaginate($model);
+
         $query = $this->newQuery();
 
         if ($this->relations) {
@@ -163,6 +166,104 @@ class EloquentRepository extends Repository implements TreeRepository
         return $query;
     }
 
+    /**
+     * Set the grid sort.
+     *
+     * @param Grid\Model $model
+     *
+     * @return void
+     */
+    protected function setSort(Grid\Model $model)
+    {
+        list($column, $type) = $model->getSort();
+
+        if (empty($column) || empty($type)) {
+            return;
+        }
+
+        if (Str::contains($column, '.')) {
+            $this->setRelationSort($model, $column, $type);
+        } else {
+            $model->resetOrderBy();
+
+            $model->addQuery('orderBy', [$column, $type]);
+        }
+    }
+
+    /**
+     * Set relation sort.
+     *
+     * @param Grid\Model $model
+     * @param string     $column
+     * @param string     $type
+     *
+     * @return void
+     */
+    protected function setRelationSort(Grid\Model $model, $column, $type)
+    {
+        [$relationName, $relationColumn] = explode('.', $column);
+
+        if ($model->getQueries()->contains(function ($query) use ($relationName) {
+            return $query['method'] == 'with' && in_array($relationName, $query['arguments']);
+        })) {
+            $model->addQuery('select', [$this->getGridColumns()]);
+
+            $model->resetOrderBy();
+
+            $model->addQuery('orderBy', [
+                $relationColumn,
+                $type,
+            ]);
+        }
+    }
+
+
+    /**
+     * Set the grid paginate.
+     *
+     * @param Grid\Model $model
+     *
+     * @return void
+     */
+    protected function setPaginate(Grid\Model $model)
+    {
+        $paginate = $model->findQueryByMethod('paginate');
+
+        $model->rejectQuery(['paginate']);
+
+        if (! $model->allowPagination()) {
+            $model->addQuery('get', [$this->getGridColumns()]);
+        } else {
+            $model->addQuery('paginate', $this->resolvePerPage($model, $paginate));
+        }
+    }
+
+    /**
+     * Resolve perPage for pagination.
+     *
+     * @param Grid\Model $model
+     * @param array|null $paginate
+     *
+     * @return array
+     */
+    protected function resolvePerPage(Grid\Model $model, $paginate)
+    {
+        if ($paginate && is_array($paginate)) {
+            if ($perPage = request()->input($model->getPerPageName())) {
+                $paginate['arguments'][0] = (int) $perPage;
+            }
+
+            return $paginate['arguments'];
+        }
+
+        return [
+            $model->getPerPage(),
+            $this->getGridColumns(),
+            $model->getPageName(),
+            $model->getCurrentPage(),
+        ];
+    }
+
     /**
      * Get data to build edit form.
      *