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\Database\Eloquent; |
15
|
|
|
use Illuminate\Support\Collection; |
16
|
|
|
use Tinyissue\Model\Setting; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SettingsManager singleton class to maintain a collection of all settings |
20
|
|
|
* |
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @property int $id |
24
|
|
|
* @property string $name |
25
|
|
|
* @property string $value |
26
|
|
|
* @property string $key |
27
|
|
|
*/ |
28
|
|
|
class SettingsManager extends Collection |
29
|
|
|
{ |
30
|
56 |
|
public function __construct($items = []) |
31
|
|
|
{ |
32
|
56 |
|
parent::__construct($items); |
33
|
56 |
|
$this->load(); |
34
|
56 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Load all settings |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|boolean |
40
|
|
|
*/ |
41
|
56 |
|
protected function load() |
42
|
|
|
{ |
43
|
56 |
|
if ($this->count() === 0) { |
44
|
|
|
// Skip exception if table does not exists |
45
|
|
|
// This method called from RouteServiceProvider, which is called within command line 'artisan migrate' |
46
|
|
|
// before the table exists. |
47
|
|
|
try { |
48
|
56 |
|
$items = Setting::all(); |
49
|
56 |
|
$this->items = is_array($items) ? $items : $this->getArrayableItems($items); |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
56 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns a setting value |
60
|
|
|
* |
61
|
|
|
* @param $name |
62
|
|
|
* @param mixed|null $default |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
56 |
|
public function get($name, $default = null) |
66
|
|
|
{ |
67
|
56 |
|
if ($this->load()) { |
68
|
56 |
|
foreach ($this->all() as $setting) { |
69
|
56 |
|
if ($setting->key === $name) { |
70
|
56 |
|
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
|
56 |
|
public function isPublicProjectsEnabled() |
84
|
|
|
{ |
85
|
56 |
|
return (boolean)$this->get('enable_public_projects') === true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Save a collection of settings |
90
|
|
|
* |
91
|
|
|
* @param $values |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
3 |
|
public function save($values) |
95
|
|
|
{ |
96
|
3 |
|
foreach ($values as $name => $value) { |
97
|
3 |
|
$settings = new Setting(); |
98
|
3 |
|
$setting = $settings->where('key', '=', $name)->first(); |
|
|
|
|
99
|
3 |
|
if ($setting) { |
100
|
3 |
|
$setting->value = $value; |
101
|
3 |
|
$setting->save(); |
102
|
|
|
} |
103
|
3 |
|
unset($settings, $setting); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Reload items |
107
|
3 |
|
$this->items = []; |
108
|
3 |
|
$this->load(); |
109
|
|
|
|
110
|
3 |
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: