1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Config; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as ConfigRepository; |
6
|
|
|
use Illuminate\Contracts\Validation\Factory; |
7
|
|
|
use Illuminate\Foundation\Application; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
11
|
|
|
use Sco\Admin\Contracts\ConfigFactoryInterface; |
12
|
|
|
use Sco\Admin\Contracts\ConfigManagerInterface; |
13
|
|
|
use Sco\Admin\Contracts\ModelFactoryInterface; |
14
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
15
|
|
|
|
16
|
|
|
class ModelFactory implements ModelFactoryInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Illuminate\Foundation\Application |
20
|
|
|
*/ |
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Sco\Admin\Contracts\ConfigManagerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $configManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
30
|
|
|
*/ |
31
|
|
|
//protected $model; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed|\Sco\Admin\Repositories\Repository |
35
|
|
|
*/ |
36
|
|
|
protected $repository; |
37
|
|
|
|
38
|
|
|
protected $config; |
39
|
|
|
|
40
|
|
|
public function __construct(Application $app, ConfigManagerInterface $configManager) |
41
|
|
|
{ |
42
|
|
|
$this->app = $app; |
43
|
|
|
$this->configManager = $configManager; |
44
|
|
|
$this->config = new ConfigRepository( |
45
|
|
|
$this->getConfigValues() |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
49
|
|
|
|
50
|
|
|
$this->repository->setClass( |
51
|
|
|
$this->config->get('class') |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getRepository() |
59
|
|
|
{ |
60
|
|
|
return $this->repository; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getConfigManager() |
67
|
|
|
{ |
68
|
|
|
return $this->configManager; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function get() |
72
|
|
|
{ |
73
|
|
|
$repository = $this->getRepository(); |
74
|
|
|
$orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']); |
75
|
|
|
$query = $repository->orderBy($orderBy[0], $orderBy[1]); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if ($repository->isRestorable()) { |
78
|
|
|
$query = $query->withTrashed(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($this->usePagination()) { |
82
|
|
|
$data = $query->paginate($this->config->get('perPage')); |
83
|
|
|
|
84
|
|
|
$data->setCollection($this->parseRows($data->items())); |
85
|
|
|
} else { |
86
|
|
|
$data = $this->parseRows($query->get()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $data; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function store() |
93
|
|
|
{ |
94
|
|
|
$this->validate(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function update() |
98
|
|
|
{ |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function delete($id) |
102
|
|
|
{ |
103
|
|
|
$this->getRepository()->findOrFail($id)->delete(); |
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function forceDelete($id) |
108
|
|
|
{ |
109
|
|
|
$this->getRepository()->forceDelete($id); |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function restore($id) |
114
|
|
|
{ |
115
|
|
|
$this->getRepository()->restore($id); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function parseRows($rows) |
119
|
|
|
{ |
120
|
|
|
if ($rows) { |
121
|
|
|
return $rows->map(function ($row) { |
122
|
|
|
$newRow = $this->configManager->getColumns()->mapWithKeys(function ($column) use ($row) { |
123
|
|
|
return [$column->getName() => $column->setModel($row)->render()]; |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
// whether this row has been soft deleted |
127
|
|
|
if ($this->getRepository()->isRestorable()) { |
128
|
|
|
$newRow->put('_deleted', $row->trashed() ? 1 : 0); |
129
|
|
|
} |
130
|
|
|
return $newRow; |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
return collect(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function usePagination() |
137
|
|
|
{ |
138
|
|
|
return $this->config->get('perPage') > 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function getConfigValues() |
142
|
|
|
{ |
143
|
|
|
$config = $this->configManager->getConfigRepository()->get('model'); |
144
|
|
|
if (is_string($config)) { |
145
|
|
|
$config = [ |
146
|
|
|
'class' => $config |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
$config = array_merge([ |
150
|
|
|
//'orderBy' => [], |
|
|
|
|
151
|
|
|
'perPage' => 10, |
152
|
|
|
], $config); |
153
|
|
|
return $config; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function validate() |
157
|
|
|
{ |
158
|
|
|
$this->app->make(Factory::class)->validate($data, $rules, $messages, $customAttributes); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.