|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Services; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Support\Collection; |
|
15
|
|
|
use Tinyissue\Model\Setting; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* SettingsManager singleton class to maintain a collection of all settings. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @property int $id |
|
23
|
|
|
* @property string $name |
|
24
|
|
|
* @property string $value |
|
25
|
|
|
* @property string $key |
|
26
|
|
|
*/ |
|
27
|
|
|
class SettingsManager extends Collection |
|
28
|
|
|
{ |
|
29
|
59 |
|
public function __construct($items = []) |
|
30
|
|
|
{ |
|
31
|
59 |
|
parent::__construct($items); |
|
32
|
59 |
|
$this->load(); |
|
33
|
59 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Load all settings. |
|
37
|
|
|
* |
|
38
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|bool |
|
39
|
|
|
*/ |
|
40
|
59 |
|
protected function load() |
|
41
|
|
|
{ |
|
42
|
59 |
|
if ($this->count() === 0) { |
|
43
|
|
|
// Skip exception if table does not exists |
|
44
|
|
|
// This method called from RouteServiceProvider, which is called within command line 'artisan migrate' |
|
45
|
|
|
// before the table exists. |
|
46
|
|
|
try { |
|
47
|
59 |
|
$items = Setting::all(); |
|
48
|
59 |
|
$this->items = is_array($items) ? $items : $this->getArrayableItems($items); |
|
49
|
|
|
} catch (\Exception $e) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
59 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns a setting value. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $name |
|
61
|
|
|
* @param mixed|null $default |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
59 |
|
public function get($name, $default = null) |
|
66
|
|
|
{ |
|
67
|
59 |
|
if ($this->load()) { |
|
68
|
59 |
|
foreach ($this->all() as $setting) { |
|
69
|
59 |
|
if ($setting->key === $name) { |
|
70
|
59 |
|
return $setting->value; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $default; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Whether or not the public projects enabled. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
59 |
|
public function isPublicProjectsEnabled() |
|
84
|
|
|
{ |
|
85
|
59 |
|
return (boolean) $this->get('enable_public_projects') === true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns date format. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
29 |
|
public function getDateFormat() |
|
94
|
|
|
{ |
|
95
|
29 |
|
return (string) $this->get('date_format'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Save a collection of settings. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $values |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
5 |
|
public function save($values) |
|
106
|
|
|
{ |
|
107
|
5 |
|
foreach ($values as $name => $value) { |
|
108
|
5 |
|
$settings = new Setting(); |
|
109
|
5 |
|
$setting = $settings->where('key', '=', $name)->first(); |
|
|
|
|
|
|
110
|
5 |
|
if ($setting) { |
|
111
|
5 |
|
$setting->value = $value; |
|
112
|
5 |
|
$setting->save(); |
|
113
|
|
|
} |
|
114
|
5 |
|
unset($settings, $setting); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Reload items |
|
118
|
5 |
|
$this->items = []; |
|
119
|
5 |
|
$this->load(); |
|
120
|
|
|
|
|
121
|
5 |
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: