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