1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Subsites\State; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Core\Injector\Injectable; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SubsiteState provides static access to the current state for subsite related data during a request |
11
|
|
|
*/ |
12
|
|
|
class SubsiteState |
13
|
|
|
{ |
14
|
|
|
use Injectable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int|null |
18
|
|
|
*/ |
19
|
|
|
protected $subsiteId; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $useSessions; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $sessionWasChanged = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the current subsite ID |
33
|
|
|
* |
34
|
|
|
* @return int|null |
35
|
|
|
*/ |
36
|
|
|
public function getSubsiteId() |
37
|
|
|
{ |
38
|
|
|
return $this->subsiteId; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the current subsite ID |
43
|
|
|
* |
44
|
|
|
* @param int $id |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setSubsiteId($id) |
48
|
|
|
{ |
49
|
|
|
$this->subsiteId = (int) $id; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get whether to use sessions for storing the subsite ID |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function getUseSessions() |
60
|
|
|
{ |
61
|
|
|
return $this->useSessions; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set whether to use sessions for storing the subsite ID |
66
|
|
|
* |
67
|
|
|
* @param bool $useSessions |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setUseSessions($useSessions) |
71
|
|
|
{ |
72
|
|
|
$this->useSessions = $useSessions; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getSessionWasChanged() |
78
|
|
|
{ |
79
|
|
|
return $this->sessionWasChanged; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setSessionWasChanged($changed = true) |
83
|
|
|
{ |
84
|
|
|
$this->sessionWasChanged = (bool) $changed; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Perform a given action within the context of a new, isolated state. Modifications are temporary |
91
|
|
|
* and the existing state will be restored afterwards. |
92
|
|
|
* |
93
|
|
|
* @param callable $callback Callback to run. Will be passed the nested state as a parameter |
94
|
|
|
* @return mixed Result of callback |
95
|
|
|
*/ |
96
|
|
|
public function withState(callable $callback) |
97
|
|
|
{ |
98
|
|
|
$newState = clone $this; |
99
|
|
|
try { |
100
|
|
|
Injector::inst()->registerService($newState); |
101
|
|
|
return $callback($newState); |
102
|
|
|
} finally { |
103
|
|
|
Injector::inst()->registerService($this); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|