1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Config; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
8
|
|
|
use Illuminate\Config\Repository as ConfigRepository; |
9
|
|
|
use Illuminate\Foundation\Application; |
10
|
|
|
use Sco\Admin\Column\Columns; |
11
|
|
|
use Sco\Admin\Contracts\Config as ConfigContract; |
12
|
|
|
use Sco\Attributes\HasOriginalAndAttributesTrait; |
13
|
|
|
|
14
|
|
|
class ConfigFactory implements ConfigContract, Arrayable, Jsonable, JsonSerializable |
15
|
|
|
{ |
16
|
|
|
use HasOriginalAndAttributesTrait; |
17
|
|
|
|
18
|
|
|
protected $app; |
19
|
|
|
protected $name; |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
protected $title; |
23
|
|
|
protected $permissions; |
24
|
|
|
protected $columns; |
25
|
|
|
protected $model; |
26
|
|
|
|
27
|
|
|
public function __construct(Application $app, $name) |
28
|
|
|
{ |
29
|
|
|
$this->app = $app; |
30
|
|
|
$this->name = $name; |
31
|
|
|
|
32
|
|
|
$this->config = new ConfigRepository( |
33
|
|
|
$this->app['files']->getRequire($this->getConfigFile()) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function getConfigFile() |
38
|
|
|
{ |
39
|
|
|
return $this->app['path.config'] |
40
|
|
|
. DIRECTORY_SEPARATOR . 'admin' |
41
|
|
|
. DIRECTORY_SEPARATOR . $this->name . '.php'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getConfigRepository() |
45
|
|
|
{ |
46
|
|
|
return $this->config; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function getTitle() |
51
|
|
|
{ |
52
|
|
|
if (!$this->title) { |
53
|
|
|
$this->title = $this->config->get('title'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->title; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return \Sco\Admin\Config\PermissionsConfig |
61
|
|
|
*/ |
62
|
|
|
public function getPermissions() |
63
|
|
|
{ |
64
|
|
|
if (!$this->permissions) { |
65
|
|
|
$config = $this->config->get('permissions'); |
66
|
|
|
|
67
|
|
|
$this->permissions = new PermissionsConfig($config); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->permissions; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getColumns() |
74
|
|
|
{ |
75
|
|
|
if (!$this->columns) { |
76
|
|
|
$config = $this->config->get('columns'); |
77
|
|
|
|
78
|
|
|
$this->columns = collect($config)->mapWithKeys(function ($item, $key) { |
79
|
|
|
$columnClass = config('admin.column'); |
80
|
|
|
return [$key => new $columnClass($key, $item)]; |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->columns; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getFields() |
88
|
|
|
{ |
89
|
|
|
$fields = $this->getAttribute('fields', collect()); |
90
|
|
|
if ($fields->isEmpty()) { |
91
|
|
|
$options = $this->getOriginal('fields'); |
92
|
|
|
|
93
|
|
|
foreach ($options as $option) { |
94
|
|
|
$fields->push($option); |
95
|
|
|
} |
96
|
|
|
$this->setAttribute('columns', $fields); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $fields; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \Sco\Admin\Config\ModelConfig |
104
|
|
|
*/ |
105
|
|
|
public function getModel() |
106
|
|
|
{ |
107
|
|
|
if (!$this->model) { |
108
|
|
|
$this->model = new ModelConfig($this->app, $this); |
109
|
|
|
} |
110
|
|
|
return $this->model; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getConfigs() |
114
|
|
|
{ |
115
|
|
|
$this->setAttribute([ |
116
|
|
|
'primaryKey' => $this->getModel()->getRepository()->getKeyName(), |
117
|
|
|
'title' => $this->getTitle(), |
118
|
|
|
'permissions' => $this->getPermissions(), |
119
|
|
|
'columns' => $this->getColumns()->values(), |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
return $this->getAttributes(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function __toString() |
127
|
|
|
{ |
128
|
|
|
return $this->toJson(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|