Skip to content

Commit

Permalink
Merge pull request #644 from tractorcow-farm/fix/state-mutation
Browse files Browse the repository at this point in the history
Wrap middleware state mutations inside state helpers
  • Loading branch information
robbieaverill authored Aug 27, 2020
2 parents 994b85f + b7e40f7 commit d87e752
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
48 changes: 25 additions & 23 deletions src/Middleware/DetectLocaleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,31 @@ class DetectLocaleMiddleware implements HTTPMiddleware
*/
public function process(HTTPRequest $request, callable $delegate)
{
$state = FluentState::singleton();
$locale = $state->getLocale();

if (!$locale) {
$locale = $this->getLocale($request);
$state->setLocale($locale);
}

// Validate the user is allowed to access this locale
$this->validateAllowedLocale($state);

if ($locale && $state->getIsFrontend()) {
i18n::set_locale($state->getLocale());
}

// Persist the current locale if it has a value.
// Distinguishes null from empty strings in order to unset locales.
$newLocale = $state->getLocale();
if (!is_null($newLocale)) {
$this->setPersistLocale($request, $newLocale);
}

return $delegate($request);
return FluentState::singleton()
->withState(function ($state) use ($delegate, $request) {
$locale = $state->getLocale();

if (!$locale) {
$locale = $this->getLocale($request);
$state->setLocale($locale);
}

// Validate the user is allowed to access this locale
$this->validateAllowedLocale($state);

if ($locale && $state->getIsFrontend()) {
i18n::set_locale($state->getLocale());
}

// Persist the current locale if it has a value.
// Distinguishes null from empty strings in order to unset locales.
$newLocale = $state->getLocale();
if (!is_null($newLocale)) {
$this->setPersistLocale($request, $newLocale);
}

return $delegate($request);
});
}

/**
Expand Down
42 changes: 20 additions & 22 deletions src/Middleware/InitStateMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use TractorCow\Fluent\Extension\FluentDirectorExtension;
use TractorCow\Fluent\Model\Domain;
use TractorCow\Fluent\State\FluentState;
Expand All @@ -33,31 +32,30 @@ class InitStateMiddleware implements HTTPMiddleware

public function process(HTTPRequest $request, callable $delegate)
{
$state = FluentState::create();
Injector::inst()->registerService($state);

// Detect frontend
$isFrontend = $this->getIsFrontend($request);

// Only set domain mode on the frontend
$isDomainMode = $isFrontend ? $this->getIsDomainMode($request) : false;

// Don't set domain unless in domain mode
$domain = $isDomainMode ? Director::host($request) : null;

// Update state
$state
->setIsFrontend($isFrontend)
->setIsDomainMode($isDomainMode)
->setDomain($domain);

return $delegate($request);
return FluentState::singleton()
->withState(function ($state) use ($delegate, $request) {
// Detect frontend
$isFrontend = $this->getIsFrontend($request);

// Only set domain mode on the frontend
$isDomainMode = $isFrontend ? $this->getIsDomainMode($request) : false;

// Don't set domain unless in domain mode
$domain = $isDomainMode ? Director::host($request) : null;
// Update state
$state
->setIsFrontend($isFrontend)
->setIsDomainMode($isDomainMode)
->setDomain($domain);

return $delegate($request);
});
}

/**
* Determine whether the website is being viewed from the frontend or not
*
* @param HTTPRequest $request
* @param HTTPRequest $request
* @return bool
*/
public function getIsFrontend(HTTPRequest $request)
Expand All @@ -83,7 +81,7 @@ public function getIsFrontend(HTTPRequest $request)
/**
* Determine whether the website is running in domain segmentation mode
*
* @param HTTPRequest $request
* @param HTTPRequest $request
* @return bool
*/
public function getIsDomainMode(HTTPRequest $request)
Expand Down
3 changes: 3 additions & 0 deletions src/State/FluentState.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\i18n\i18n;

/**
* Stores the current fluent state
Expand Down Expand Up @@ -145,11 +146,13 @@ public function setIsFrontend($isFrontend)
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);
}
}
}

0 comments on commit d87e752

Please sign in to comment.