|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
|
|
|
|
|
9
|
|
|
use Terranet\Administrator\Contracts\Module; |
|
10
|
|
|
use Terranet\Administrator\Contracts\Services\Finder as FinderContract; |
|
11
|
|
|
use Terranet\Administrator\Exception; |
|
12
|
|
|
use Terranet\Administrator\Filters\Assembler; |
|
13
|
|
|
|
|
14
|
|
|
class Finder implements FinderContract |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Module */ |
|
17
|
|
|
protected $module; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Model */ |
|
20
|
|
|
protected $model; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Builder */ |
|
23
|
|
|
protected $query; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Assembler */ |
|
26
|
|
|
protected $assembler; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Request */ |
|
29
|
|
|
protected $request; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Finder constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Module $module |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Module $module) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->module = $module; |
|
39
|
|
|
$this->model = $module->model(); |
|
40
|
|
|
$this->request = $module->request(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Fetch all items from repository. |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
* @throws Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
public function fetchAll() |
|
50
|
|
|
{ |
|
51
|
|
|
if ($builder = $this->getQuery()) { |
|
52
|
|
|
return $builder->paginate($this->perPage()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return new LengthAwarePaginator([], 0, 10, 1); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Build Scaffolding Index page query. |
|
60
|
|
|
* |
|
61
|
|
|
* @return Builder |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getQuery(): Builder |
|
65
|
|
|
{ |
|
66
|
|
|
// prevent duplicated execution |
|
67
|
|
|
if (null === $this->query && $this->model) { |
|
68
|
|
|
$this->initQuery() |
|
69
|
|
|
->applyFilters() |
|
70
|
|
|
->applyRelationalFilters() |
|
71
|
|
|
->applySorting(); |
|
72
|
|
|
|
|
73
|
|
|
$this->query = $this->assembler()->getQuery(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->query; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Find a record by id or fail. |
|
81
|
|
|
* |
|
82
|
|
|
* @param $key |
|
83
|
|
|
* @param array $columns |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function find($key, $columns = ['*']) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->model = once(function () use ($key, $columns) { |
|
89
|
|
|
return $this->model->newQueryWithoutScopes()->findOrFail($key, $columns); |
|
90
|
|
|
}); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the query assembler object. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Assembler |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function assembler(): Assembler |
|
99
|
|
|
{ |
|
100
|
|
|
if (null === $this->assembler) { |
|
101
|
|
|
$this->assembler = (new Assembler($this->model)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->assembler; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function initQuery(): FinderContract |
|
111
|
|
|
{ |
|
112
|
|
|
if (method_exists($this->module, 'query')) { |
|
113
|
|
|
$this->assembler()->applyQueryCallback([$this->module, 'query']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Apply query string filters. |
|
121
|
|
|
* |
|
122
|
|
|
* @return FinderContract |
|
123
|
|
|
* @throws Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function applyFilters(): FinderContract |
|
126
|
|
|
{ |
|
127
|
|
|
if (!$filter = $this->module->filter()) { |
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if ($filters = $filter->filters()) { |
|
132
|
|
|
$this->assembler()->filters($filters); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (empty($scopes = $filter->scopes())) { |
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$scope = $filter->scope(); |
|
|
|
|
|
|
140
|
|
|
if ($scope && $found = $scopes->find($scope)) { |
|
141
|
|
|
$this->assembler()->scope($found); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Apply relation filters: cross-links to other |
|
149
|
|
|
* |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function applyRelationalFilters(): FinderContract |
|
153
|
|
|
{ |
|
154
|
|
|
if (($relation = request('viaResource')) && ($value = request('viaResourceId'))) { |
|
|
|
|
|
|
155
|
|
|
$this->assembler()->relations($relation, $value); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Extend query with Order By Statement. |
|
163
|
|
|
* |
|
164
|
|
|
* @return FinderContract |
|
165
|
|
|
* @throws \Exception |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function applySorting(): FinderContract |
|
168
|
|
|
{ |
|
169
|
|
|
/** @var Sorter $sortable */ |
|
170
|
|
|
$sortable = $this->module->sortableManager(); |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
$element = $sortable->element(); |
|
173
|
|
|
$direction = $sortable->direction(); |
|
174
|
|
|
|
|
175
|
|
|
if ($element && $direction && is_string($element)) { |
|
176
|
|
|
$this->assembler()->sort($element, $direction); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Items per page. |
|
184
|
|
|
* |
|
185
|
|
|
* @return int |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function perPage(): int |
|
188
|
|
|
{ |
|
189
|
|
|
return method_exists($this->module, 'perPage') |
|
190
|
|
|
? $this->module->perPage() |
|
191
|
|
|
: 20; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths