Skip to content

Commit

Permalink
Merge branch '4.5' into 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Oct 21, 2020
2 parents c1cec13 + 964175f commit fbabf52
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
42 changes: 22 additions & 20 deletions src/Middleware/DetectLocaleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,28 @@ 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);
}

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);
}

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
35 changes: 17 additions & 18 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,31 @@ class InitStateMiddleware implements HTTPMiddleware

public function process(HTTPRequest $request, callable $delegate)
{
$state = FluentState::create();
Injector::inst()->registerService($state);
return FluentState::singleton()
->withState(function ($state) use ($delegate, $request) {
// Detect frontend
$isFrontend = $this->getIsFrontend($request);

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

// 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;

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

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

return $delegate($request);
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 +82,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 fbabf52

Please sign in to comment.