Skip to content

Commit

Permalink
Merge branch '5.2' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Sep 23, 2024
2 parents 394677f + fa30672 commit 1cad4ff
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 96 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [Bakery] Added the server option to `assets:webpack` to run HMR server (`npm run webpack:server`) plus use new npm command syntax.
- [Bakery] `AbstractAggregateCommandEvent` construction is now optional. Added `addCommands` and `prependCommands`. All setters methods return `$this`.

## [5.1.2](https://github.com/userfrosting/sprinkle-core/compare/5.1.1...5.1.2)
- Replace `LocaleMiddleware` with `ServerRequestMiddleware`. A new class, `RequestContainer`, can be injected or retrieved from the container to get the server request. It will be `null` if the request is not defined (called before it is injected into the container by Middleware or if there's no request, e.g., a Bakery command).

## [5.1.2](https://github.com/userfrosting/sprinkle-core/compare/5.1.1...5.1.2)
- Fix [#1264](https://github.com/userfrosting/UserFrosting/issues/1264) - The browser locale is not applied automatically

Expand Down
4 changes: 2 additions & 2 deletions app/src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
use UserFrosting\Sprinkle\Core\Listeners\ResourceLocatorInitiated;
use UserFrosting\Sprinkle\Core\Listeners\SetRouteCaching;
use UserFrosting\Sprinkle\Core\Middlewares\FilePermissionMiddleware;
use UserFrosting\Sprinkle\Core\Middlewares\LocaleMiddleware;
use UserFrosting\Sprinkle\Core\Middlewares\ServerRequestMiddleware;
use UserFrosting\Sprinkle\Core\Middlewares\SessionMiddleware;
use UserFrosting\Sprinkle\Core\Middlewares\URIMiddleware;
use UserFrosting\Sprinkle\Core\Routes\AlertsRoutes;
Expand Down Expand Up @@ -236,7 +236,7 @@ public function getServices(): array
public function getMiddlewares(): array
{
return [
LocaleMiddleware::class,
ServerRequestMiddleware::class,
CsrfGuardMiddleware::class,
SessionMiddleware::class,
URIMiddleware::class,
Expand Down
36 changes: 11 additions & 25 deletions app/src/I18n/SiteLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@

namespace UserFrosting\Sprinkle\Core\I18n;

use Psr\Http\Message\ServerRequestInterface;
use UserFrosting\Config\Config;
use UserFrosting\I18n\Locale;
use UserFrosting\Sprinkle\Core\Util\RequestContainer;

/**
* Helper methods for the locale system.
*/
class SiteLocale implements SiteLocaleInterface
{
/**
* @var string|null
*/
protected ?string $browserLocale = null;

/**
* @param Config $config
* @param Config $config
* @param RequestContainer $request
*/
public function __construct(
protected Config $config,
protected RequestContainer $request,
) {
}

Expand Down Expand Up @@ -127,8 +124,6 @@ public function getDefaultLocale(string $fallback = 'en_US'): string
* Returns the locale identifier (ie. en_US) to use.
*
* @return string Locale identifier
*
* @todo This should accept the request service as argument, or null, in which case the `getBrowserLocale` method would be skipped
*/
public function getLocaleIdentifier(): string
{
Expand All @@ -150,21 +145,14 @@ public function getLocaleIdentifier(): string
*/
protected function getBrowserLocale(): ?string
{
return $this->browserLocale;
}
// Stop if there's no request
if (is_null($request = $this->request->getRequest())) {
return null;
}

/**
* Define the browser locale from the header present in the request.
*
* @param ServerRequestInterface $request
*/
public function defineBrowserLocale(ServerRequestInterface $request): void
{
// Stop if request doesn't have the header
if (!$request->hasHeader('Accept-Language')) {
$this->browserLocale = null;

return;
return null;
}

// Get available locales
Expand Down Expand Up @@ -207,9 +195,7 @@ public function defineBrowserLocale(ServerRequestInterface $request): void

// if no $foundLocales, return null
if (count($foundLocales) === 0) {
$this->browserLocale = null;

return;
return null;
}

// Sort by preference (value)
Expand All @@ -218,6 +204,6 @@ public function defineBrowserLocale(ServerRequestInterface $request): void
// Return first element
reset($foundLocales);

$this->browserLocale = key($foundLocales);
return key($foundLocales);
}
}
8 changes: 0 additions & 8 deletions app/src/I18n/SiteLocaleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace UserFrosting\Sprinkle\Core\I18n;

use Psr\Http\Message\ServerRequestInterface;
use UserFrosting\Config\Config;
use UserFrosting\I18n\Locale;

Expand Down Expand Up @@ -70,11 +69,4 @@ public function getDefaultLocale(): string;
* @todo This should accept the request service as argument, or null, in which case the `getBrowserLocale` method would be skipped
*/
public function getLocaleIdentifier(): string;

/**
* Define the browser locale from the header present in the request.
*
* @param ServerRequestInterface $request
*/
public function defineBrowserLocale(ServerRequestInterface $request): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use UserFrosting\Sprinkle\Core\I18n\SiteLocaleInterface;
use UserFrosting\Sprinkle\Core\Util\RequestContainer;

class LocaleMiddleware implements MiddlewareInterface
class ServerRequestMiddleware implements MiddlewareInterface
{
/**
* @param SiteLocaleInterface $siteLocale Inject SiteLocale service
* @param RequestContainer $requestContainer
*/
public function __construct(
protected SiteLocaleInterface $siteLocale,
protected RequestContainer $requestContainer,
) {
}

Expand All @@ -33,7 +33,7 @@ public function __construct(
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->siteLocale->defineBrowserLocale($request);
$this->requestContainer->setRequest($request);

return $handler->handle($request);
}
Expand Down
48 changes: 48 additions & 0 deletions app/src/Util/RequestContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* UserFrosting Core Sprinkle (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/sprinkle-core
* @copyright Copyright (c) 2013-2024 Alexander Weissman & Louis Charette
* @license https://github.com/userfrosting/sprinkle-core/blob/master/LICENSE.md (MIT License)
*/

namespace UserFrosting\Sprinkle\Core\Util;

use Psr\Http\Message\ServerRequestInterface;

/**
* Helper methods to store and retrieve the server request.
* The request can be null if it's not set yet (eg. before the request is saved
* by the middleware) or if there's no server request (eg. in a CLI command).
*/
class RequestContainer
{
/**
* @var ServerRequestInterface|null
*/
protected ?ServerRequestInterface $request = null;

/**
* Return the server request.
*
* @return ServerRequestInterface|null
*/
public function getRequest(): ?ServerRequestInterface
{
return $this->request;
}

/**
* Store the server request.
*
* @param ServerRequestInterface $request
*/
public function setRequest(ServerRequestInterface $request): void
{
$this->request = $request;
}
}
Loading

0 comments on commit 1cad4ff

Please sign in to comment.