Source of file SubsiteState.php
Size: 2,879 Bytes - Last Modified: 2021-12-23T10:35:18+00:00
/var/www/docs.ssmods.com/process/src/src/State/SubsiteState.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php namespace SilverStripe\Subsites\State; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Resettable; /** * SubsiteState provides static access to the current state for subsite related data during a request */ class SubsiteState implements Resettable { use Injectable; /** * @var int|null */ protected $subsiteId; /** * @var int|null */ protected $originalSubsiteId; /** * @var bool */ protected $useSessions; /** * Get the current subsite ID * * @return int|null */ public function getSubsiteId() { return $this->subsiteId; } /** * 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. * * @param int $id * @return $this */ public function setSubsiteId($id) { if (is_null($this->originalSubsiteId)) { $this->originalSubsiteId = (int) $id; } $this->subsiteId = (int) $id; return $this; } /** * Get whether to use sessions for storing the subsite ID * * @return bool */ public function getUseSessions() { return $this->useSessions; } /** * Set whether to use sessions for storing the subsite ID * * @param bool $useSessions * @return $this */ public function setUseSessions($useSessions) { $this->useSessions = $useSessions; return $this; } /** * Get whether the subsite ID has been changed during a request, based on the original and current IDs * * @return bool */ public function getSubsiteIdWasChanged() { return $this->originalSubsiteId !== $this->getSubsiteId(); } /** * 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 */ public function withState(callable $callback) { $newState = clone $this; try { Injector::inst()->registerService($newState); return $callback($newState); } finally { Injector::inst()->registerService($this); } } /** * Reset the local cache of the singleton */ public static function reset() { SubsiteState::singleton()->resetState(); } /** * Reset the local cache of this object */ public function resetState() { $this->originalSubsiteId = null; $this->subsiteId = null; $this->useSessions = null; } } |