1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Config; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as ConfigRepository; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
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\Config as ConfigContract; |
12
|
|
|
use Sco\Admin\Contracts\Repository as RepositoryContract; |
13
|
|
|
use Sco\Attributes\HasAttributesTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ModelConfig |
17
|
|
|
* |
18
|
|
|
* @method static \Illuminate\Database\Eloquent\Model|Model getKeyName() |
19
|
|
|
*/ |
20
|
|
|
class ModelConfig implements Arrayable, Jsonable, JsonSerializable |
21
|
|
|
{ |
22
|
|
|
use HasAttributesTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Illuminate\Foundation\Application |
26
|
|
|
*/ |
27
|
|
|
protected $app; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Sco\Admin\Contracts\Config |
31
|
|
|
*/ |
32
|
|
|
protected $configFactory; |
33
|
|
|
/** |
34
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
35
|
|
|
*/ |
36
|
|
|
//protected $model; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var mixed|\Sco\Admin\Contracts\Repository |
40
|
|
|
*/ |
41
|
|
|
protected $repository; |
42
|
|
|
|
43
|
|
|
protected $config; |
44
|
|
|
|
45
|
|
|
public function __construct(Application $app, ConfigContract $factory) |
46
|
|
|
{ |
47
|
|
|
$this->app = $app; |
48
|
|
|
$this->configFactory = $factory; |
49
|
|
|
//$this->model = $model; |
|
|
|
|
50
|
|
|
$this->config = new ConfigRepository( |
51
|
|
|
$this->getModelConfig() |
|
|
|
|
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->repository = $this->app->make(RepositoryContract::class); |
55
|
|
|
|
56
|
|
|
$this->repository->setClass( |
57
|
|
|
$factory->getConfigRepository()->get('model') |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getRepository() |
62
|
|
|
{ |
63
|
|
|
return $this->repository; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function paginate($perPage = null) |
67
|
|
|
{ |
68
|
|
|
$data = $this->model->paginate($perPage); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$data->setCollection($this->parseRows($data->items())); |
71
|
|
|
|
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function parseRows($rows) |
76
|
|
|
{ |
77
|
|
|
$data = collect(); |
78
|
|
|
if ($rows) { |
79
|
|
|
foreach ($rows as $row) { |
80
|
|
|
$newRow = collect(); |
81
|
|
|
foreach ($this->configFactory->getColumns() as $column) { |
82
|
|
|
$newRow->put($column->getName(), $column->setModel($row)->render()); |
83
|
|
|
} |
84
|
|
|
$data->push($newRow); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function get() |
91
|
|
|
{ |
92
|
|
|
$data = $this->model->get(); |
93
|
|
|
return $data; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function delete($id) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Handle dynamic method calls into the model. |
103
|
|
|
* |
104
|
|
|
* @param string $method |
105
|
|
|
* @param array $parameters |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
/*public function __call($method, $parameters) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
if (in_array($method, ['getKeyName'])) { |
111
|
|
|
return $this->model->$method(...$parameters); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->model = $this->model->$method(...$parameters); |
115
|
|
|
return $this; |
116
|
|
|
}*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Handle dynamic static method calls into the method. |
120
|
|
|
* |
121
|
|
|
* @param string $method |
122
|
|
|
* @param array $parameters |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
/*public static function __callStatic($method, $parameters) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
return (new static)->$method(...$parameters); |
128
|
|
|
}*/ |
129
|
|
|
} |
130
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.