1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Mvc\Sections; |
4
|
|
|
|
5
|
|
|
use Nip\Collections\AbstractCollection; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SectionsManager |
10
|
|
|
* @package Nip\Mvc\Sections |
11
|
|
|
*/ |
12
|
|
|
class SectionsManager |
13
|
|
|
{ |
14
|
|
|
protected $currentKey = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var SectionsCollection |
18
|
|
|
*/ |
19
|
|
|
protected $sections = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $section |
23
|
|
|
* @param $url |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public static function transformUrl($section, $url) |
27
|
|
|
{ |
28
|
|
|
return static::instance()->getOne($section)->getURL($url); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $section |
33
|
|
|
* @param $url |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public static function assembleURL($section, $url, $params = []) |
37
|
|
|
{ |
38
|
|
|
return static::instance()->getOne($section)->assembleURL($url, $params); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $key |
43
|
|
|
* @return Section |
44
|
|
|
*/ |
45
|
|
|
public function getOne($key) |
46
|
|
|
{ |
47
|
|
|
return $this->getSections()->get($key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Section |
52
|
|
|
*/ |
53
|
|
|
public function getCurrent() |
54
|
|
|
{ |
55
|
|
|
return $this->getSections()->get($this->getCurrentKey()); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return null |
60
|
|
|
*/ |
61
|
|
|
public function getCurrentKey() |
62
|
|
|
{ |
63
|
|
|
if ($this->currentKey === null) { |
64
|
|
|
$this->setCurrentKey(SectionDetector::run($this->sections)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->currentKey; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $key |
72
|
|
|
*/ |
73
|
|
|
public function setCurrentKey($key) |
74
|
|
|
{ |
75
|
|
|
$this->currentKey = $key; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return SectionsCollection |
80
|
|
|
*/ |
81
|
2 |
|
public function getSections(): SectionsCollection |
82
|
|
|
{ |
83
|
2 |
|
if ($this->sections === null) { |
84
|
2 |
|
$this->sections = new SectionsCollection(); |
85
|
2 |
|
$this->loadFromConfig(); |
86
|
|
|
} |
87
|
2 |
|
return $this->sections; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param SectionsCollection $sections |
92
|
|
|
*/ |
93
|
|
|
public function setSections(SectionsCollection $sections): void |
94
|
|
|
{ |
95
|
|
|
$this->sections = $sections; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return void |
100
|
|
|
* @deprecated no need to call init |
101
|
|
|
*/ |
102
|
|
|
public function init() |
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $place |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
1 |
|
public function visibleIn($place) |
111
|
|
|
{ |
112
|
|
|
return $this->getSections()->filter(function ($item) use ($place) { |
113
|
|
|
/** @var Section $item */ |
114
|
1 |
|
return $item->visibleIn($place); |
115
|
1 |
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
protected function loadFromConfig() |
119
|
|
|
{ |
120
|
2 |
|
if (function_exists('config')) { |
121
|
2 |
|
$data = config('mvc.sections', []); |
122
|
2 |
|
foreach ($data as $key => $row) { |
123
|
2 |
|
$this->sections->set($key, $this->newSection($row->toArray())); |
124
|
|
|
} |
125
|
|
|
} |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $data |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
2 |
|
protected function newSection($data) |
133
|
|
|
{ |
134
|
2 |
|
$section = app(Section::class); |
135
|
2 |
|
$section->writeData($data); |
136
|
2 |
|
return $section; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Singleton |
141
|
|
|
* @return SectionsManager |
142
|
|
|
*/ |
143
|
|
|
public static function instance() |
144
|
|
|
{ |
145
|
|
|
static $instance; |
146
|
|
|
if (!($instance instanceof SectionsManager)) { |
147
|
|
|
$instance = app('mvc.sections'); |
148
|
|
|
} |
149
|
|
|
return $instance; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.