2017-08-30 01:06:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Subsites\State;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-10-05 06:52:53 +02:00
|
|
|
use SilverStripe\Core\Resettable;
|
2023-01-25 22:39:07 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2017-08-30 01:06:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SubsiteState provides static access to the current state for subsite related data during a request
|
|
|
|
*/
|
2017-10-05 06:52:53 +02:00
|
|
|
class SubsiteState implements Resettable
|
2017-08-30 01:06:07 +02:00
|
|
|
{
|
|
|
|
use Injectable;
|
|
|
|
|
2023-01-26 21:55:04 +01:00
|
|
|
protected ?int $subsiteId = null;
|
2017-09-05 03:48:28 +02:00
|
|
|
|
2023-01-26 21:55:04 +01:00
|
|
|
protected ?int $originalSubsiteId = null;
|
2017-08-30 05:29:13 +02:00
|
|
|
|
2023-01-26 21:55:04 +01:00
|
|
|
protected ?bool $useSessions = null;
|
2017-09-04 01:45:21 +02:00
|
|
|
|
2017-08-30 01:06:07 +02:00
|
|
|
/**
|
|
|
|
* Get the current subsite ID
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function getSubsiteId(): ?int
|
2017-08-30 01:06:07 +02:00
|
|
|
{
|
|
|
|
return $this->subsiteId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-05 03:48:28 +02:00
|
|
|
* Set the current subsite ID, and track the first subsite ID set as the "original". This is used to check
|
|
|
|
* whether the ID has been changed through a request.
|
2017-08-30 01:06:07 +02:00
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function setSubsiteId(?int $id): static
|
2017-08-30 01:06:07 +02:00
|
|
|
{
|
2017-09-05 03:48:28 +02:00
|
|
|
if (is_null($this->originalSubsiteId)) {
|
|
|
|
$this->originalSubsiteId = (int) $id;
|
|
|
|
}
|
|
|
|
|
2017-08-30 01:06:07 +02:00
|
|
|
$this->subsiteId = (int) $id;
|
|
|
|
|
2017-08-30 05:29:13 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get whether to use sessions for storing the subsite ID
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function getUseSessions(): ?bool
|
2017-08-30 05:29:13 +02:00
|
|
|
{
|
|
|
|
return $this->useSessions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether to use sessions for storing the subsite ID
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function setUseSessions(?bool $useSessions): static
|
2017-08-30 05:29:13 +02:00
|
|
|
{
|
|
|
|
$this->useSessions = $useSessions;
|
2017-09-04 01:45:21 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:48:28 +02:00
|
|
|
/**
|
|
|
|
* Get whether the subsite ID has been changed during a request, based on the original and current IDs
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function getSubsiteIdWasChanged(): bool
|
2017-09-04 01:45:21 +02:00
|
|
|
{
|
2017-09-05 03:48:28 +02:00
|
|
|
return $this->originalSubsiteId !== $this->getSubsiteId();
|
2017-08-30 01:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a given action within the context of a new, isolated state. Modifications are temporary
|
|
|
|
* and the existing state will be restored afterwards.
|
|
|
|
*
|
|
|
|
* @param callable $callback Callback to run. Will be passed the nested state as a parameter
|
|
|
|
* @return mixed Result of callback
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function withState(callable $callback): mixed
|
2017-08-30 01:06:07 +02:00
|
|
|
{
|
|
|
|
$newState = clone $this;
|
|
|
|
try {
|
|
|
|
Injector::inst()->registerService($newState);
|
|
|
|
return $callback($newState);
|
|
|
|
} finally {
|
|
|
|
Injector::inst()->registerService($this);
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 06:52:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the local cache of the singleton
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public static function reset(): void
|
2017-10-05 06:52:53 +02:00
|
|
|
{
|
|
|
|
SubsiteState::singleton()->resetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the local cache of this object
|
|
|
|
*/
|
2023-01-26 21:55:04 +01:00
|
|
|
public function resetState(): void
|
2017-10-05 06:52:53 +02:00
|
|
|
{
|
|
|
|
$this->originalSubsiteId = null;
|
|
|
|
$this->subsiteId = null;
|
|
|
|
$this->useSessions = null;
|
|
|
|
}
|
2017-08-30 01:06:07 +02:00
|
|
|
}
|