1
|
|
|
<?php namespace Anomaly\SettingsModule\Setting; |
2
|
|
|
|
3
|
|
|
use Anomaly\SettingsModule\Setting\Command\GetDefaultValue; |
4
|
|
|
use Anomaly\SettingsModule\Setting\Contract\SettingInterface; |
5
|
|
|
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface; |
6
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeCollection; |
7
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter; |
8
|
|
|
use Anomaly\Streams\Platform\Entry\EntryRepository; |
9
|
|
|
use Illuminate\Config\Repository; |
10
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SettingRepositoryInterface |
14
|
|
|
* |
15
|
|
|
* @link http://pyrocms.com/ |
16
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
17
|
|
|
* @author Ryan Thompson <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class SettingRepository extends EntryRepository implements SettingRepositoryInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use DispatchesJobs; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The setting model. |
26
|
|
|
* |
27
|
|
|
* @var SettingModel |
28
|
|
|
*/ |
29
|
|
|
protected $model; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The settings collection. |
33
|
|
|
* |
34
|
|
|
* @var SettingCollection |
35
|
|
|
*/ |
36
|
|
|
protected $settings; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new SettingRepositoryInterface instance. |
40
|
|
|
* |
41
|
|
|
* @param SettingModel $model |
42
|
|
|
* @param Repository $config |
|
|
|
|
43
|
|
|
* @param FieldTypeCollection $fieldTypes |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function __construct(SettingModel $model) |
46
|
|
|
{ |
47
|
|
|
$this->model = $model; |
48
|
|
|
|
49
|
|
|
$this->settings = $this->model->all(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return if the key exists or not. |
54
|
|
|
* |
55
|
|
|
* @param $key |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function has($key) |
59
|
|
|
{ |
60
|
|
|
return $this->settings->has($key); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a setting. |
65
|
|
|
* |
66
|
|
|
* @param $key |
67
|
|
|
* @param null $default |
68
|
|
|
* @return null|SettingInterface|SettingModel |
69
|
|
|
*/ |
70
|
|
|
public function get($key, $default = null) |
71
|
|
|
{ |
72
|
|
|
$setting = $this->settings->get($key); |
73
|
|
|
|
74
|
|
|
return ($setting) ? $setting : $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set a settings value. |
79
|
|
|
* |
80
|
|
|
* @param $key |
81
|
|
|
* @param $value |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function set($key, $value) |
85
|
|
|
{ |
86
|
|
|
$setting = $this->findByKeyOrNew($key); |
87
|
|
|
|
88
|
|
|
$setting->setValue($value); |
89
|
|
|
|
90
|
|
|
return $this->save($setting); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get a setting value. |
95
|
|
|
* |
96
|
|
|
* @param $key |
97
|
|
|
* @param null $default |
98
|
|
|
* @return mixed|null |
99
|
|
|
*/ |
100
|
|
|
public function value($key, $default = null) |
101
|
|
|
{ |
102
|
|
|
$setting = $this->get($key); |
103
|
|
|
|
104
|
|
|
if ($setting) { |
105
|
|
|
return $setting->getValue(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!$setting && !$default) { |
109
|
|
|
return $this->dispatch(new GetDefaultValue($key)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $default; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return the field type |
117
|
|
|
* presenter for a setting. |
118
|
|
|
* |
119
|
|
|
* @param $key |
120
|
|
|
* @return FieldTypePresenter|null |
121
|
|
|
*/ |
122
|
|
|
public function presenter($key) |
123
|
|
|
{ |
124
|
|
|
if ($setting = $this->get($key)) { |
125
|
|
|
return $setting->getFieldTypePresenter('value'); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Find a setting by it's key |
133
|
|
|
* or return a new instance. |
134
|
|
|
* |
135
|
|
|
* @param $key |
136
|
|
|
* @return SettingInterface |
137
|
|
|
*/ |
138
|
|
|
public function findByKeyOrNew($key) |
139
|
|
|
{ |
140
|
|
|
if (!$setting = $this->model->where('key', $key)->first()) { |
141
|
|
|
|
142
|
|
|
$setting = $this->model->newInstance(); |
143
|
|
|
|
144
|
|
|
$setting->setKey($key); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $setting; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Find all settings with namespace. |
152
|
|
|
* |
153
|
|
|
* @param $namespace |
154
|
|
|
* @return SettingCollection |
155
|
|
|
*/ |
156
|
|
|
public function findAllByNamespace($namespace) |
157
|
|
|
{ |
158
|
|
|
return $this->model->where('key', 'LIKE', $namespace . '%')->get(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.