1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chuckbe\Chuckcms\Chuck\Accessors; |
4
|
|
|
|
5
|
|
|
use Chuckbe\Chuckcms\Chuck\ModuleRepository; |
6
|
|
|
use Chuckbe\Chuckcms\Chuck\SiteRepository; |
7
|
|
|
use Chuckbe\Chuckcms\Models\Site as SiteModel; |
8
|
|
|
|
9
|
|
|
class Site |
10
|
|
|
{ |
11
|
|
|
private $siteRepository; |
12
|
|
|
private $moduleRepository; |
13
|
|
|
private $currentSite; |
14
|
|
|
private $siteSettings; |
15
|
|
|
private $siteSupportedLocales; |
16
|
|
|
|
17
|
|
|
public function __construct(SiteModel $site, SiteRepository $siteRepository, ModuleRepository $moduleRepository) |
18
|
|
|
{ |
19
|
|
|
$this->currentSite = $this->getCurrentSite($site); |
20
|
|
|
$this->siteSettings = $this->getSiteSettings($site); |
21
|
|
|
$this->siteSupportedLocales = $this->getSiteSupportedLocales($site); |
22
|
|
|
$this->siteRepository = $siteRepository; |
23
|
|
|
$this->moduleRepository = $moduleRepository; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function forSite($site) |
27
|
|
|
{ |
28
|
|
|
return new static($site, \App::make(SiteRepository::class)); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function getSiteSettings(SiteModel $site) |
32
|
|
|
{ |
33
|
|
|
$settings = $site->settings; |
34
|
|
|
|
35
|
|
|
return $settings; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function getCurrentSite(SiteModel $site) |
39
|
|
|
{ |
40
|
|
|
return $site; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function getSiteSupportedLocales(SiteModel $site) |
44
|
|
|
{ |
45
|
|
|
$settings = $site->settings; |
46
|
|
|
$locales = []; |
47
|
|
|
if (is_array($settings)) { |
|
|
|
|
48
|
|
|
foreach (config('lang.allLocales') as $langKey => $langValue) { |
49
|
|
|
if (in_array($langKey, explode(',', $settings['lang']))) { |
50
|
|
|
$locales[$langKey] = $langValue; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} else { |
54
|
|
|
$allLocales = config('lang.allLocales'); |
55
|
|
|
$locales['en'] = $allLocales['en']; |
56
|
|
|
$locales['nl'] = $allLocales['nl']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $locales; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getSettings() |
63
|
|
|
{ |
64
|
|
|
$settings = $this->siteSettings; |
65
|
|
|
|
66
|
|
|
return $settings ? $settings : null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getSetting($var) |
70
|
|
|
{ |
71
|
|
|
$setting = $this->resolveSetting($var, $this->siteSettings); |
72
|
|
|
|
73
|
|
|
return !is_null($setting) ? $setting : null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getSite($var) |
77
|
|
|
{ |
78
|
|
|
$setting = $this->resolveSiteAttribute($var, $this->currentSite); |
79
|
|
|
|
80
|
|
|
return !is_null($setting) ? $setting : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getSupportedLocales() |
84
|
|
|
{ |
85
|
|
|
$supportedLocales = $this->siteSupportedLocales; |
86
|
|
|
|
87
|
|
|
return $supportedLocales ? $supportedLocales : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getFeaturedLocale() |
91
|
|
|
{ |
92
|
|
|
return config('lang.featuredLocale'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function resolveSetting($var, $settings) |
96
|
|
|
{ |
97
|
|
|
$split = explode('.', $var); |
98
|
|
|
foreach ($split as $value) { |
99
|
|
|
if (array_key_exists($value, $settings)) { |
100
|
|
|
$settings = $settings[$value]; |
101
|
|
|
} else { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $settings; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function resolveSiteAttribute($var, $currentSite) |
110
|
|
|
{ |
111
|
|
|
if ($var == 'domain') { |
112
|
|
|
return $currentSite->domain; |
113
|
|
|
} |
114
|
|
|
if ($var == 'name') { |
115
|
|
|
return $currentSite->name; |
116
|
|
|
} |
117
|
|
|
if ($var == 'slug') { |
118
|
|
|
return $currentSite->slug; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function module(string $slug) |
125
|
|
|
{ |
126
|
|
|
return $this->moduleRepository->get($slug); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.