1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Config; |
5
|
|
|
|
6
|
|
|
use AdminElement; |
7
|
|
|
use Illuminate\Contracts\Config\Repository; |
8
|
|
|
use Illuminate\Foundation\Application; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
12
|
|
|
use Sco\Admin\Contracts\ConfigManagerInterface; |
13
|
|
|
|
14
|
|
|
class ConfigManager implements ConfigManagerInterface, Arrayable, Jsonable, JsonSerializable |
15
|
|
|
{ |
16
|
|
|
protected $app; |
17
|
|
|
|
18
|
|
|
protected $name; |
19
|
|
|
protected $configRepository; |
20
|
|
|
|
21
|
|
|
protected $title; |
22
|
|
|
protected $permissions; |
23
|
|
|
protected $columns; |
24
|
|
|
protected $model; |
25
|
|
|
protected $elements; |
26
|
|
|
|
27
|
|
|
public function __construct($name, Application $app, Repository $repository) |
28
|
|
|
{ |
29
|
|
|
$this->name = $name; |
30
|
|
|
$this->app = $app; |
31
|
|
|
$this->configRepository = $repository; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getConfigRepository() |
35
|
|
|
{ |
36
|
|
|
return $this->configRepository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getTitle() |
40
|
|
|
{ |
41
|
|
|
if (!$this->title) { |
42
|
|
|
$this->title = $this->configRepository->get('title'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->title; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getPermissions() |
52
|
|
|
{ |
53
|
|
|
if (!$this->permissions) { |
54
|
|
|
$config = $this->configRepository->get('permissions'); |
55
|
|
|
|
56
|
|
|
$this->permissions = new PermissionsConfig($config); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->permissions; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getColumns() |
63
|
|
|
{ |
64
|
|
|
if (!$this->columns) { |
65
|
|
|
$config = $this->configRepository->get('columns'); |
66
|
|
|
|
67
|
|
|
$this->columns = collect($config)->mapWithKeys(function ($item, $key) { |
68
|
|
|
$columnClass = config('admin.column'); |
69
|
|
|
return [$key => new $columnClass($key, $item)]; |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->columns; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getElements() |
77
|
|
|
{ |
78
|
|
|
if (!$this->elements) { |
79
|
|
|
$config = $this->configRepository->get('elements'); |
80
|
|
|
$this->elements = collect($config)->mapWithKeys(function ($item, $key) { |
81
|
|
|
$type = isset($item['type']) ? $item['type'] : 'text'; |
82
|
|
|
return [$key => AdminElement::{$type}($key, $item['title'])]; |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->elements; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function getModel() |
93
|
|
|
{ |
94
|
|
|
if (!$this->model) { |
95
|
|
|
$this->model = new ModelFactory($this->app, $this); |
96
|
|
|
} |
97
|
|
|
return $this->model; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getRules() |
101
|
|
|
{ |
102
|
|
|
return $this->configRepository->get('rules'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getConfigs() |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
'primaryKey' => $this->getModel()->getRepository()->getKeyName(), |
112
|
|
|
'title' => $this->getTitle(), |
113
|
|
|
'permissions' => $this->getPermissions(), |
114
|
|
|
'columns' => $this->getColumns()->values(), |
115
|
|
|
'elements' => $this->getElements()->values(), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Convert the Object instance to an array. |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function toArray() |
125
|
|
|
{ |
126
|
|
|
return $this->getConfigs(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Convert the object into something JSON serializable. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function jsonSerialize() |
135
|
|
|
{ |
136
|
|
|
return $this->toArray(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Convert the Object instance to JSON. |
141
|
|
|
* |
142
|
|
|
* @param int $options |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function toJson($options = 0) |
147
|
|
|
{ |
148
|
|
|
return json_encode($this->jsonSerialize(), $options); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function __toString() |
152
|
|
|
{ |
153
|
|
|
return $this->toJson(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|