Source of file FluentState.php
Size: 3,372 Bytes - Last Modified: 2021-12-23T10:53:31+00:00
/var/www/docs.ssmods.com/process/src/src/State/FluentState.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 | <?php namespace TractorCow\Fluent\State; use InvalidArgumentException; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use SilverStripe\i18n\i18n; /** * Stores the current fluent state */ class FluentState { use Injectable; /** * Current locale * * @var string */ protected $locale; /** * Current domain, if set * * @var string|null */ protected $domain; /** * Whether the website is running in domain segmentation mode * * @var bool */ protected $isDomainMode; /** * Whether the request is for the frontend website * * @var bool */ protected $isFrontend; /** * Get the currently active locale code * * @return string */ public function getLocale() { return $this->locale; } /** * Set the currently active locale code * * @param string $locale * @return $this */ public function setLocale($locale) { if ($locale && !is_string($locale)) { throw new InvalidArgumentException("Invalid locale"); } $this->locale = $locale; return $this; } /** * Get the current domain code * * @return string|null */ public function getDomain() { return $this->domain; } /** * Set the current domain code * * @param string|null $domain * @return $this */ public function setDomain($domain) { if ($domain && !is_string($domain)) { throw new InvalidArgumentException("Invalid domain"); } $this->domain = $domain; return $this; } /** * Get whether the website is in domain segmentation mode * * @return bool */ public function getIsDomainMode() { return $this->isDomainMode; } /** * Set whether the website is in domain segmentation mode * * @param bool $isDomainMode * @return $this */ public function setIsDomainMode($isDomainMode) { $this->isDomainMode = $isDomainMode; return $this; } /** * Get whether a request is for the frontend website or not * * @return bool */ public function getIsFrontend() { return $this->isFrontend; } /** * Set whether a request is for the frontend website or not * * @param bool $isFrontend * @return $this */ public function setIsFrontend($isFrontend) { $this->isFrontend = $isFrontend; return $this; } /** * Perform the given operation in an isolated state. * On return, the state will be restored, so any modifications are temporary. * * @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; $oldLocale = i18n::get_locale(); // Backup locale in case the callback modifies this try { Injector::inst()->registerService($newState); return $callback($newState); } finally { Injector::inst()->registerService($this); i18n::set_locale($oldLocale); } } } |